' . $this->renderInline($text) . '

'; $paragraph = []; }; $flushList = function () use (&$listType, &$html): void { if ($listType !== null) { $html[] = ''; $listType = null; } }; $flushTable = function () use (&$table, &$html): void { if ($table === null) { return; } $html[] = '' . implode('', array_map(fn (string $cell): string => '', $table['headers'])) . ''; foreach ($table['rows'] as $row) { $html[] = '' . implode('', array_map(fn (string $cell): string => '', $row)) . ''; } $html[] = '
' . $this->renderInline($cell) . '
' . $this->renderInline($cell) . '
'; $table = null; }; foreach ($lines as $line) { if (preg_match('/^```/', $line)) { $flushParagraph(); $flushList(); $flushTable(); if ($inCodeBlock) { $html[] = '
' . htmlspecialchars(implode("\n", $codeLines), ENT_QUOTES, 'UTF-8') . '
'; $codeLines = []; $inCodeBlock = false; } else { $inCodeBlock = true; } continue; } if ($inCodeBlock) { $codeLines[] = $line; continue; } $trimmed = trim($line); if ($trimmed === '') { $flushParagraph(); $flushList(); $flushTable(); continue; } if (preg_match('/^(#{1,6})\s+(.+)$/', $trimmed, $matches)) { $flushParagraph(); $flushList(); $flushTable(); $level = strlen($matches[1]); $html[] = sprintf('%s', $level, $this->renderInline($matches[2]), $level); continue; } if (preg_match('/^>\s?(.+)$/', $trimmed, $matches)) { $flushParagraph(); $flushList(); $flushTable(); $html[] = '
' . $this->renderInline($matches[1]) . '
'; continue; } if (preg_match('/^---+$/', $trimmed)) { $flushParagraph(); $flushList(); $flushTable(); $html[] = '
'; continue; } if ($this->isTableDelimiter($trimmed) && $table !== null) { continue; } if (str_contains($trimmed, '|')) { $cells = $this->tableCells($trimmed); if (count($cells) >= 2) { $flushParagraph(); $flushList(); if ($table === null) { $table = ['headers' => $cells, 'rows' => []]; } else { $table['rows'][] = $cells; } continue; } } if (preg_match('/^[-*]\s+(.+)$/', $trimmed, $matches)) { $flushParagraph(); $flushTable(); if ($listType !== 'ul') { $flushList(); $listType = 'ul'; $html[] = '