Backup #1549
| ID | 1549 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Infrastructure/Formatting/ChatMessageFormatter.php |
| Version | 2 |
| Typ |
modified |
| Größe | 2.3 KB |
| Hash | 0cf13036fc97b27c1c703090ba81fb92fb76d213855e3351c6337fb795847f6a |
| Datum | 2025-12-26 20:06:44 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
declare(strict_types=1);
namespace Infrastructure\Formatting;
// @responsibility: Formatiert Chat-Nachrichten (Markdown → HTML)
class ChatMessageFormatter
{
public function formatAnswer(string $text): string
{
// Escape HTML first
$text = htmlspecialchars($text);
// Headers
$text = preg_replace('/^### (.+)$/m', '<h4>$1</h4>', $text);
$text = preg_replace('/^## (.+)$/m', '<h3>$1</h3>', $text);
$text = preg_replace('/^# (.+)$/m', '<h3>$1</h3>', $text);
// Bold
$text = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $text);
// Italic
$text = preg_replace('/\*(.+?)\*/', '<em>$1</em>', $text);
// Code
$text = preg_replace('/`(.+?)`/', '<code>$1</code>', $text);
// Blockquotes
$text = preg_replace('/^> (.+)$/m', '<blockquote>$1</blockquote>', $text);
// Lists: Process line by line to correctly wrap consecutive list items
$text = $this->formatLists($text);
// Line breaks
$text = nl2br($text);
// Clean up multiple <br> tags
$text = preg_replace('/(<br\s*\/?>\s*){3,}/', '<br><br>', $text);
return $text;
}
private function formatLists(string $text): string
{
$lines = explode("\n", $text);
$result = [];
$inList = false;
foreach ($lines as $line) {
// Check if line is a list item (- or * or numbered)
if (preg_match('/^[-*] (.+)$/', $line, $matches)) {
if (!$inList) {
$result[] = '<ul>';
$inList = true;
}
$result[] = '<li>' . $matches[1] . '</li>';
} elseif (preg_match('/^\d+\. (.+)$/', $line, $matches)) {
// Numbered list item
if (!$inList) {
$result[] = '<ul>';
$inList = true;
}
$result[] = '<li>' . $matches[1] . '</li>';
} else {
// Not a list item
if ($inList) {
$result[] = '</ul>';
$inList = false;
}
$result[] = $line;
}
}
// Close any open list
if ($inList) {
$result[] = '</ul>';
}
return implode("\n", $result);
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 1549 |
2 |
modified |
2.3 KB |
2025-12-26 20:06 |
| 793 |
1 |
modified |
2.3 KB |
2025-12-23 08:06 |
← Zurück zur Übersicht