[UTIL][UI] Change how plaintext notes are rendered to be split into paragraphs. Remove span around tags

This commit is contained in:
Hugo Sales 2021-09-16 16:56:34 +01:00
parent 51c7e10483
commit 04174bc56d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 19 additions and 13 deletions

View File

@ -258,26 +258,32 @@ abstract class Formatting
*/ */
public static function renderPlainText(string $text): string public static function renderPlainText(string $text): string
{ {
$text = self::removeUnicodeFormattingCodes($text); $text = self::quoteAndRemoveControlCodes($text);
$text = nl2br(htmlspecialchars($text, flags: ENT_QUOTES | ENT_SUBSTITUTE, double_encode: false), use_xhtml: false);
// Remove ASCII control codes // Split \n\n into paragraphs, process each paragrah and merge
$text = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $text); $text = implode("\n", F\map(explode("\n\n", $text), function (string $paragraph) {
$text = self::replaceURLs($text, [self::class, 'linkify']); $paragraph = nl2br($paragraph, use_xhtml: false);
$text = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u', $paragraph = self::replaceURLs($paragraph, [self::class, 'linkify']);
fn ($m) => "{$m[1]}#" . self::tagLink($m[2]), $text); $paragraph = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)(#[\pL\pN_\-\.]{1,64})/u',
fn ($m) => "{$m[1]}" . self::tagLink($m[2]), $paragraph);
return HTML::html(['p' => [$paragraph]], options: ['raw' => true]);
}));
return $text; return $text;
} }
/** /**
* Strip Unicode text formatting/direction codes. This is can be * Quote HTML special chars and strip Unicode text
* pretty dangerous for visualisation of text or be used for * formatting/direction codes. This is can be pretty dangerous for
* mischief * visualisation of text or be used for mischief
*/ */
public static function removeUnicodeFormattingCodes(string $text): string public static function quoteAndRemoveControlCodes(string $text): string
{ {
return preg_replace('/[\\x{200b}-\\x{200f}\\x{202a}-\\x{202e}]/u', '', $text); // Quote special chars
$text = htmlspecialchars($text, flags: ENT_QUOTES | ENT_SUBSTITUTE, double_encode: false);
// Normalize newlines to strictly \n and remove ASCII control codes
return preg_replace(['/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}\x{200b}-\x{200f}\x{202a}-\x{202e}]/u', '/\R/u'], ['', "\n"], $text);
} }
const URL_SCHEME_COLON_DOUBLE_SLASH = 1; const URL_SCHEME_COLON_DOUBLE_SLASH = 1;
@ -488,7 +494,7 @@ abstract class Formatting
{ {
$canonical = self::canonicalTag($tag); $canonical = self::canonicalTag($tag);
$url = Router::url('tag', ['tag' => $canonical]); $url = Router::url('tag', ['tag' => $canonical]);
return HTML::html(['span' => ['a' => ['attrs' => ['href' => $url, 'rel' => 'tag']]]]); return HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => $tag, 'rel' => 'tag'], $tag]]);
} }
public static function canonicalTag(string $tag): string public static function canonicalTag(string $tag): string