diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index 9b1717d..aa832bb 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -62,20 +62,20 @@ private function renderNode(NodeInterface $node): string { return match (true) { $node instanceof HeadingNode => $this->renderHeading($node), - $node instanceof ParagraphNode => '

' . $this->renderChildren($node->children) . '

', - $node instanceof BlockquoteNode => '
' . $this->renderChildren($node->children) . '
', + $node instanceof ParagraphNode => '

' . $this->renderChildren($node->children) . "

\n", + $node instanceof BlockquoteNode => "
\n" . $this->renderChildren($node->children) . "
\n", $node instanceof ListNode => $this->renderList($node), $node instanceof ListItemNode => $this->renderListItem($node), $node instanceof FencedCodeNode => $this->renderFencedCode($node), - $node instanceof IndentedCodeNode => '
' . $this->esc($node->content) . '
', - $node instanceof HorizontalRuleNode => '
', + $node instanceof IndentedCodeNode => '
' . $this->esc($node->content) . "
\n", + $node instanceof HorizontalRuleNode => "
\n", $node instanceof ColumnsNode => $this->renderColumns($node), // Raw HTML blocks: sanitize if allowRawHtml, else escape (XSS-safe default). $node instanceof RawHtmlBlockNode => $this->allowRawHtml - ? $this->sanitizer->sanitize($node->content) - : $this->esc($node->content), - $node instanceof HardBreakNode => '
', + ? $this->sanitizer->sanitize($node->content) . "\n" + : $this->esc($node->content) . "\n", + $node instanceof HardBreakNode => "
\n", // HTML entities pass through verbatim — validated by InlineParser, no esc() needed. $node instanceof HtmlEntityNode => $node->entity, $node instanceof TextNode => $this->esc($node->text), @@ -112,7 +112,7 @@ private function renderChildren(array $nodes): string private function renderHeading(HeadingNode $node): string { $tag = 'h' . $node->level; - return '<' . $tag . '>' . $this->renderChildren($node->children) . ''; + return '<' . $tag . '>' . $this->renderChildren($node->children) . '\n"; } private function renderList(ListNode $node): string @@ -122,7 +122,7 @@ private function renderList(ListNode $node): string foreach ($node->children as $item) { $inner .= $this->renderListItem($item, $node->loose); } - return '<' . $tag . '>' . $inner . ''; + return '<' . $tag . ">\n" . $inner . '\n"; } private function renderListItem(ListItemNode $node, bool $loose = false): string @@ -130,10 +130,13 @@ private function renderListItem(ListItemNode $node, bool $loose = false): string $content = $this->renderListItemContent($node->children, $loose); if ($node->checked === null) { - return '
  • ' . $content . '
  • '; + if ($loose) { + return "
  • \n" . $content . "
  • \n"; + } + return '
  • ' . $content . "
  • \n"; } $checkbox = 'checked ? ' checked' : '') . '>'; - return '
  • ' . $checkbox . ' ' . $content . '
  • '; + return '
  • ' . $checkbox . ' ' . $content . "
  • \n"; } /** @@ -161,7 +164,7 @@ private function renderListItemContent(array $children, bool $loose): string $html = ''; if ($inlinePart !== []) { - $html .= '

    ' . $this->renderChildren($inlinePart) . '

    '; + $html .= '

    ' . $this->renderChildren($inlinePart) . "

    \n"; } foreach ($blockPart as $block) { $html .= $this->renderNode($block); @@ -187,7 +190,7 @@ private function renderFencedCode(FencedCodeNode $node): string $classAttr = ($lang ?? '') !== '' ? ' class="' . $this->esc('language-' . (string) $lang) . '"' : ''; - return '
    ' . $this->esc($node->content) . '
    '; + return '
    ' . $this->esc($node->content) . "
    \n"; } private function renderLink(LinkNode $node): string @@ -205,7 +208,7 @@ private function renderImage(ImageNode $node): string $titleAttr = $node->title !== null ? ' title="' . $this->esc($node->title) . '"' : ''; - return '' . $this->esc($node->alt) . ''; + return '' . $this->esc($node->alt) . ''; } private function renderAutolink(AutolinkNode $node): string diff --git a/tests/Integration/MarkdownParserTest.php b/tests/Integration/MarkdownParserTest.php index 7f0cf96..8070614 100644 --- a/tests/Integration/MarkdownParserTest.php +++ b/tests/Integration/MarkdownParserTest.php @@ -74,7 +74,7 @@ public function testFullDocumentPipeline(): void $this->assertStringContainsString('bold', $html); $this->assertMatchesRegularExpression('/