From 5420ec8f50c2443ea446ad448b945f6721435c9f Mon Sep 17 00:00:00 2001 From: simonpipe Date: Thu, 1 Jan 2026 18:36:49 +0100 Subject: [PATCH] Update mastodonapi.php --- mastodonapi.php | 81 ++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/mastodonapi.php b/mastodonapi.php index 3547aa5..b6dae49 100644 --- a/mastodonapi.php +++ b/mastodonapi.php @@ -218,50 +218,61 @@ class YellowMastodonapi { } private function generateMarkdown($title, $slug, $dateTime, $html, $origAuthor, $origUrl, $yellowAuthor, $yellowTag, $images = [], $alts = [], $status = "public") { - $date = new DateTime($dateTime); - $formattedDate = $date->format('Y-m-d'); - $formattedTime = $date->format('H:i:s'); + $date = new DateTime($dateTime); + $formattedDate = $date->format('Y-m-d'); + $formattedTime = $date->format('H:i:s'); - $content = strip_tags($html); - $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + // 1. Schritt: HTML-Umbrüche in Newlines umwandeln + $content = str_replace(["
", "
", "
", "

"], ["\n", "\n", "\n", "

\n"], $html); - $hashtagString = ''; - if (preg_match('/(\s*(#\w+))+$/u', $content, $matches)) { - $content = substr($content, 0, -strlen($matches[0])); - if (preg_match_all('/#(\w+)/u', $matches[0], $tags)) { - $safeHashtags = array_map(function($tag) { - return str_replace('#', '#', $tag); - }, $tags[0]); - $hashtagString = implode(' ', $safeHashtags); - } + // 2. Schritt: Tags entfernen + $content = strip_tags($content); + + // 3. Schritt: HTML-Entities dekodieren + $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + + $hashtagString = ''; + if (preg_match('/(\s*(#\w+))+$/u', $content, $matches)) { + $content = substr($content, 0, -strlen($matches[0])); + if (preg_match_all('/#(\w+)/u', $matches[0], $tags)) { + $safeHashtags = array_map(function($tag) { + return str_replace('#', '#', $tag); + }, $tags[0]); + $hashtagString = implode(' ', $safeHashtags); } + } + + // WICHTIG: Nur mehrfache LEERZEICHEN kürzen, aber NEWLINES (\n) behalten! + $content = trim(preg_replace('/[ \t]+/', ' ', $content)); + + // Jetzt die Zeilenumbrüche für das Zitat-Layout vorbereiten + $content = str_replace("\n", "\n> ", $content); - $content = trim(preg_replace('/\s+/', ' ', $content)); + $imageMarkdown = ''; + foreach ($images as $i => $filename) { + $alt = isset($alts[$i]) ? htmlspecialchars($alts[$i], ENT_QUOTES) : ''; + $imageMarkdown .= "\n\""\n"; + } - $imageMarkdown = ''; - foreach ($images as $i => $filename) { - $alt = isset($alts[$i]) ? htmlspecialchars($alts[$i], ENT_QUOTES) : ''; - $imageMarkdown .= "\n\""\n"; + $featuredImage = ''; + $featuredImageAlt = ''; + if (!empty($images)) { + $featuredImage = "Image: $images[0]\n"; + if (isset($alts[0]) && !empty($alts[0])) { + $featuredImageAlt = "ImageAlt: " . htmlspecialchars($alts[0], ENT_QUOTES) . "\n"; } + } - $featuredImage = ''; - $featuredImageAlt = ''; - if (!empty($images)) { - $featuredImage = "Image: " . $images[0] . "\n"; - if (isset($alts[0]) && !empty($alts[0])) { - $featuredImageAlt = "ImageAlt: " . htmlspecialchars($alts[0], ENT_QUOTES) . "\n"; - } - } + $htmlSafeAuthorName = str_replace('@', '@', $origAuthor); - $htmlSafeAuthorName = str_replace('@', '@', $origAuthor); + // Blockquote final zusammenbauen + $blockquote = "> $content"; + if (!empty($hashtagString)) { + $blockquote .= "\n>\n> " . $hashtagString; + } + $blockquote .= "\n>\n> — $htmlSafeAuthorName"; - $blockquote = "> $content"; - if (!empty($hashtagString)) { - $blockquote .= "\n>\n> " . $hashtagString; - } - $blockquote .= "\n>\n> — $htmlSafeAuthorName"; - - return <<