Update mastodonapi.php
This commit is contained in:
parent
b2711fe7eb
commit
5420ec8f50
1 changed files with 46 additions and 35 deletions
|
|
@ -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(["<br>", "<br />", "<br/>", "</p>"], ["\n", "\n", "\n", "</p>\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<img src=\"/media/images/" . $filename . "\" alt=\"" . $alt . "\"/>\n";
|
||||
}
|
||||
|
||||
$imageMarkdown = '';
|
||||
foreach ($images as $i => $filename) {
|
||||
$alt = isset($alts[$i]) ? htmlspecialchars($alts[$i], ENT_QUOTES) : '';
|
||||
$imageMarkdown .= "\n<img src=\"/media/images/" . $filename . "\" alt=\"" . $alt . "\"/>\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> — <a target=\"_blank\" href=\"$origUrl\">$htmlSafeAuthorName</a>";
|
||||
|
||||
$blockquote = "> $content";
|
||||
if (!empty($hashtagString)) {
|
||||
$blockquote .= "\n>\n> " . $hashtagString;
|
||||
}
|
||||
$blockquote .= "\n>\n> — <a target=\"_blank\" href=\"$origUrl\">$htmlSafeAuthorName</a>";
|
||||
|
||||
return <<<MD
|
||||
return <<<MD
|
||||
---
|
||||
Title: $title
|
||||
TitleSlug: $slug
|
||||
|
|
|
|||
Loading…
Reference in a new issue