Repository navigation
fix(render): keep blank lines around markdown inside raw html #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| ## Input | ||
|
|
||
| ```md | ||
| <div class="a"> | ||
|
|
||
| First Paragraph | ||
|
|
||
| Second **bold** | ||
|
|
||
| </div> | ||
| ``` | ||
|
|
||
| ## AST | ||
|
|
||
| ```json | ||
| { | ||
| "frontmatter": {}, | ||
| "meta": {}, | ||
| "nodes": [ | ||
| [ | ||
| "div", | ||
| { | ||
| "$": { | ||
| "html": 1, | ||
| "block": 1 | ||
| }, | ||
| "class": "a" | ||
| }, | ||
| [ | ||
| "p", | ||
| {}, | ||
| "First Paragraph" | ||
| ], | ||
| [ | ||
| "p", | ||
| {}, | ||
| "Second ", | ||
| [ | ||
| "strong", | ||
| {}, | ||
| "bold" | ||
| ] | ||
| ] | ||
| ] | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ## HTML | ||
|
|
||
| ```html | ||
| <div class="a"> | ||
| <p>First Paragraph</p> | ||
| <p>Second <strong>bold</strong></p> | ||
| </div> | ||
| ``` | ||
|
|
||
| ## Markdown | ||
|
|
||
| ```md | ||
| <div class="a"> | ||
|
|
||
| First Paragraph | ||
|
|
||
| Second **bold** | ||
|
|
||
| </div> | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,10 @@ Explain | |
|
|
||
| ```md | ||
| <details> | ||
|
|
||
| <summary>Hello</summary> | ||
|
|
||
| Explain | ||
|
|
||
| </details> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| ## Input | ||
|
|
||
| ```md | ||
| <details> | ||
| <summary>Sum</summary> | ||
|
|
||
| - Body **b** | ||
| - two | ||
|
|
||
| </details> | ||
| ``` | ||
|
|
||
| ## AST | ||
|
|
||
| ```json | ||
| { | ||
| "frontmatter": {}, | ||
| "meta": {}, | ||
| "nodes": [ | ||
| [ | ||
| "details", | ||
| { | ||
| "$": { | ||
| "html": 1, | ||
| "block": 1 | ||
| } | ||
| }, | ||
| [ | ||
| "summary", | ||
| { | ||
| "$": { | ||
| "html": 1, | ||
| "block": 0 | ||
| } | ||
| }, | ||
| "Sum" | ||
| ], | ||
| [ | ||
| "ul", | ||
| {}, | ||
| [ | ||
| "li", | ||
| {}, | ||
| "Body ", | ||
| [ | ||
| "strong", | ||
| {}, | ||
| "b" | ||
| ] | ||
| ], | ||
| [ | ||
| "li", | ||
| {}, | ||
| "two" | ||
| ] | ||
| ] | ||
| ] | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ## HTML | ||
|
|
||
| ```html | ||
| <details> | ||
| <summary>Sum</summary> | ||
| <ul> | ||
| <li>Body <strong>b</strong></li> | ||
| <li>two</li> | ||
| </ul> | ||
| </details> | ||
| ``` | ||
|
|
||
| ## Markdown | ||
|
|
||
| ```md | ||
| <details> | ||
|
|
||
| <summary>Sum</summary> | ||
|
|
||
| - Body **b** | ||
| - two | ||
|
|
||
| </details> | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,9 +62,12 @@ Explain | |
|
|
||
| ```md | ||
| <details> | ||
|
|
||
| <summary> | ||
| Hello | ||
| </summary> | ||
|
|
||
| Explain | ||
|
|
||
| </details> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,10 +73,11 @@ | |
|
|
||
| ```md | ||
| <ai-thinking> | ||
| **bold** and more | ||
|
|
||
| **bold** and more | ||
|
|
||
| - list | ||
| - **item** | ||
|
|
||
| </ai-thinking> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,7 +77,12 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode | |
| (blockTags.has(String(child?.[0])) || (!inlineTags.has(String(child?.[0])) && !hasTextSibling)) | ||
|
|
||
| // A component written as a tag needs blank lines around its markdown, or CommonMark keeps it in the HTML block. | ||
| const markdownBody = !state.context.html && !oneLiner && $.html !== 1 | ||
| // Raw HTML gets them only around markdown blocks; a blank line in a literal body would end the HTML block. | ||
| const markdownBody = | ||
| !state.context.html && | ||
| !oneLiner && | ||
| ($.html !== 1 || | ||
| children.some((child) => typeof child !== 'string' && child[1]?.$?.html !== 1 && blockTags.has(String(child[0])))) | ||
|
Comment on lines
+84
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fix: Include 🤖 Prompt for AI agents |
||
|
|
||
| const childrenContent: string[] = [] | ||
| for (let i = 0; i < children.length; i++) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,4 +40,21 @@ describe('render: markdown/html', () => { | |
| '</alert>', | ||
| ]) | ||
| }) | ||
|
|
||
| it('keeps paragraphs parseable when its own output is rendered again', async () => { | ||
| // The second pass reads `<card>` back as raw HTML (`$.html: 1`) holding markdown paragraphs. | ||
| const once = await render(await parseMarkdown('::card\nFirst Paragraph\n\nSecond **bold**\n::'), { | ||
| format: 'markdown/html', | ||
| }) | ||
| const tokens = await reparse(once) | ||
|
|
||
| expect(tokens.filter((token) => token.type === 'inline').map((token) => token.content)).toEqual([ | ||
| 'First Paragraph', | ||
| 'Second **bold**', | ||
| ]) | ||
| expect(tokens.filter((token) => token.type === 'html_block').map((token) => token.content.trim())).toEqual([ | ||
|
Comment on lines
+51
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the token order. If
|
||
| '<card>', | ||
| '</card>', | ||
| ]) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add blank lines around fenced code blocks inside raw HTML.
When a raw HTML element's block children are fenced code blocks,
markdownBodystays false. The renderer writes the fence directly after the opening tag. CommonMark reads the HTML block until a blank line, so the fence is not parsed as code on reparse.Treat a markdown-produced
prechild, one withouthtml: 1, as a Markdown block in this check. Keep raw<pre>bodies tight, because a blank line inside them would end the HTML block.