chore: complete update
This commit is contained in:
62
src/Framework/Markdown/MarkdownConverter.php
Normal file
62
src/Framework/Markdown/MarkdownConverter.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Markdown;
|
||||
|
||||
final readonly class MarkdownConverter
|
||||
{
|
||||
public function toHtml(string $markdown): string
|
||||
{
|
||||
// Einfacher Markdown-zu-HTML Converter
|
||||
$html = $markdown;
|
||||
|
||||
// Headers
|
||||
$html = preg_replace('/^# (.+)$/m', '<h1>$1</h1>', $html);
|
||||
$html = preg_replace('/^## (.+)$/m', '<h2>$1</h2>', $html);
|
||||
$html = preg_replace('/^### (.+)$/m', '<h3>$1</h3>', $html);
|
||||
|
||||
// Bold und Italic
|
||||
$html = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $html);
|
||||
$html = preg_replace('/\*(.+?)\*/', '<em>$1</em>', $html);
|
||||
|
||||
// Code Blöcke
|
||||
$html = preg_replace('/```(\w+)?\n(.*?)\n```/s', '<pre><code class="language-$1">$2</code></pre>', $html);
|
||||
$html = preg_replace('/`(.+?)`/', '<code>$1</code>', $html);
|
||||
|
||||
// Links
|
||||
$html = preg_replace('/\[(.+?)\]\((.+?)\)/', '<a href="$2">$1</a>', $html);
|
||||
|
||||
// Listen
|
||||
$html = preg_replace('/^- (.+)$/m', '<li>$1</li>', $html);
|
||||
$html = preg_replace('/(<li>.*<\/li>)/s', '<ul>$1</ul>', $html);
|
||||
|
||||
// Blockquotes
|
||||
$html = preg_replace('/^> (.+)$/m', '<blockquote>$1</blockquote>', $html);
|
||||
|
||||
// Paragraphen
|
||||
$html = preg_replace('/\n\n/', '</p><p>', $html);
|
||||
$html = '<p>' . $html . '</p>';
|
||||
|
||||
// Zeilenumbrüche
|
||||
$html = preg_replace('/\n/', '<br>', $html);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function stripMarkdown(string $markdown): string
|
||||
{
|
||||
$text = $markdown;
|
||||
|
||||
// Entferne Markdown-Syntax
|
||||
$text = preg_replace('/^#+\s/', '', $text); // Headers
|
||||
$text = preg_replace('/\*\*(.+?)\*\*/', '$1', $text); // Bold
|
||||
$text = preg_replace('/\*(.+?)\*/', '$1', $text); // Italic
|
||||
$text = preg_replace('/```.*?\n(.*?)\n```/s', '$1', $text); // Code Blöcke
|
||||
$text = preg_replace('/`(.+?)`/', '$1', $text); // Inline Code
|
||||
$text = preg_replace('/\[(.+?)\]\((.+?)\)/', '$1 ($2)', $text); // Links
|
||||
$text = preg_replace('/^[\-\*] /', '', $text); // Listen
|
||||
$text = preg_replace('/^> /', '', $text); // Blockquotes
|
||||
|
||||
return trim($text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user