Attept for rendering markdown spoilers

This commit is contained in:
sørenpeter 2024-12-06 17:21:17 +01:00
parent ee7e6558b2
commit 52c7078bad

View file

@ -70,7 +70,7 @@ function getSingleParameter($keyToFind, $string) {
}
$pattern = '/\s*(?<!\S)' . $keyToFind . '\s*=\s*([^#\n]+)/';
// Fix: not machting with nick as in: `# follow = dbucklin@www.davebucklin.com https://www.davebucklin.com/twtxt.txt?nick=dbucklin`
// Fix: not machting with nick as in: `# follow = dbucklin@www.davebucklin.com https://www.davebucklin.com/twtxt.txt?nick=dbucklin`
//$pattern = '/\s*' . $keyToFind . '\s*=\s*([^#\n]+)/';
//$pattern = '/\s*' . $keyToFind . '\s*=\s*([^\s#]+)/'; // Only matches the first word
preg_match($pattern, $string, $matches);
@ -224,6 +224,15 @@ function replaceTagsFromTwt(string $twtString) {
return $result;
}
function replaceMarkdownSpoilersFromTwt(string $twtString) {
$pattern = '/>!( ?.+?)((\r?\n\r?\n\w)|\Z)/';
$replacement = '<details><summary>$1<summary>$2</details>';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
}
function embedYoutubeFromTwt(string $twtString) {
// original regex source: https://gist.github.com/afeld/1254889#gistcomment-1253992
@ -436,6 +445,8 @@ function getTwtsFromTwtxtString($url) {
//$twtContent = str_replace("\u{2028}", "\n<br>\n", $twtContent);
$twtContent = str_replace("\u{2028}", "\n", $twtContent);
$twtContent = replaceMarkdownSpoilersFromTwt($twtContent); // Testing for: https://darch.dk/timeline/conv/kwxr5aq
$twtContent = embedYoutubeFromTwt($twtContent);
// Get and remove the hash