' . nl2br($this->renderInline(implode("\n", $paragraph))) . '
'; $paragraph = []; } if ($listType !== null) { $class = $listType === 'ul' ? 'list-disc' : 'list-decimal'; $items = array_map(fn(string $item): string => ''; $quote = []; } }; $fence = null; $codeLines = []; foreach (preg_split('/\R/', $markdown) ?: [] as $line) { if ($fence !== null) { if (preg_match('/^ {0,3}(`{3,}|~{3,})[ \t]*$/', $line, $matches) && $matches[1][0] === $fence[0] && strlen($matches[1]) >= strlen($fence)) { $blocks[] = '' . nl2br($this->renderInline(implode("\n", $quote))) . '
' . $this->escape(implode("\n", $codeLines)) . '';
$fence = null;
$codeLines = [];
continue;
}
$codeLines[] = $line;
continue;
}
if (preg_match('/^ {0,3}(`{3,}|~{3,})(.*)$/', $line, $matches)) {
$flush();
$fence = $matches[1];
$codeLines = [];
continue;
}
if (trim($line) === '') {
$flush();
continue;
}
if (preg_match('/^ {0,3}(#{1,6})[ \t]+(.+?)[ \t]*#*[ \t]*$/', $line, $matches)) {
$flush();
$level = strlen($matches[1]);
$class = [
1 => 'text-3xl font-bold tracking-tight',
2 => 'text-2xl font-bold tracking-tight',
3 => 'text-xl font-semibold',
4 => 'text-lg font-semibold',
5 => 'text-base font-semibold',
6 => 'text-sm font-semibold',
][$level];
$blocks[] = '' . $this->escape(implode("\n", $codeLines)) . '';
}
return implode("\n", $blocks);
}
private function renderInline(string $text): string
{
$parts = preg_split('/(`[^`\r\n]+`|!\[[^\]\r\n]*\]\([^\s)\r\n]+\)|\[[^\]\r\n]+\]\([^\s)\r\n]+\)|\*\*[^*\r\n]+\*\*|__[^_\r\n]+__|\*[^*\r\n]+\*|_[^_\r\n]+_)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [$text];
$html = '';
foreach ($parts as $part) {
if ($part === '') {
continue;
}
if ($part[0] === '`') {
$html .= '' . $this->escape(substr($part, 1, -1)) . '';
} elseif (preg_match('/^!\[([^\]]*)\]\(([^)]+)\)$/', $part, $matches)) {
$altText = html_entity_decode($matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$html .= $this->isSafeImageUrl($matches[2])
? '