public function filter($value)
    {
        if (!is_string($value)) {
            return $value;
        }

        // Strip unicode bombs, and make sure all newlines are UNIX newlines.
        $value = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $value);
        $value = preg_replace('{\r\n?}', "\n", $value);

        $lambda = function($text) {
            $text = htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
            /** @noinspection NotOptimalRegularExpressionsInspection */
            $text = preg_replace('/--/', '&mdash;', $text);
            $text = nl2br($text, false);
            $text = sprintf('<p>%s</p>', $text);

            return $text;
        };

        $paragraphs = preg_split('/\n{2,}/', $value, -1, PREG_SPLIT_NO_EMPTY);
        $paragraphs = array_map($lambda, $paragraphs);

        return implode("\n\n", $paragraphs);
    }