Skip to content

fix(custom-tags): restructure inline content with newlines to prevent ATX heading block splitting#481

Open
sleitor wants to merge 1 commit intovercel:mainfrom
sleitor:fix-477
Open

fix(custom-tags): restructure inline content with newlines to prevent ATX heading block splitting#481
sleitor wants to merge 1 commit intovercel:mainfrom
sleitor:fix-477

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Mar 26, 2026

Problem

Custom tags with multiline content are prematurely closed by the CommonMark parser when the content starts inline (on the same line as the opening tag) and contains a newline followed by a block-level element like an ATX heading.

Minimal reproduction:

const markdown = '<ai-thinking> hi\n # yes</ai-thinking>';
// Expected: Both 'hi' and '# yes' inside the AiThinking component
// Actual: 'hi' inside, '# yes' rendered outside

The marked Lexer splits this as:

  1. paragraph: <ai-thinking> hi\n
  2. heading: # yes</ai-thinking>

Root Cause

preprocess-custom-tags.ts only restructures tags to block-level HTML when there are blank lines (\n\n) in the content. However, a single newline inside inline content is sufficient to introduce block-starting sequences (ATX headings, thematic breaks, list items, etc.) that break the parser.

Content that already starts on its own line (\nContent\n) is correctly parsed as a block-level HTML element by the Lexer — even with headings inside — because the opening tag is on its own line.

Fix

Extend the restructuring condition to also apply when:

  • Content is inline (doesn't start with \n), AND
  • Content contains any newline

Tags where content already starts on its own line (block-level) are unaffected. Single-line tags without newlines are also unaffected.

Fixes #477
Related: #478 (same root cause)

… ATX heading block splitting

Custom tags with content inline on the opening tag line and a newline inside
would be prematurely closed by the CommonMark parser when the newline was
followed by a block-level element like an ATX heading (# title).

Example:
  <ai-thinking> hi
  # yes</ai-thinking>

The marked Lexer parsed this as:
  - paragraph: '<ai-thinking> hi\n'
  - heading: '# yes</ai-thinking>'

The preprocessor already handled blank lines (\n\n) by restructuring the
tag to block-level HTML. Extend the condition to also restructure when
content is inline (doesn't start with \n) and contains any newline,
since any newline in inline content can introduce block-starting sequences.

Tags where content already starts on its own line (\nContent\n) are
unaffected — they're correctly parsed as block-level HTML even with
headings inside.

Fixes vercel#477
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 26, 2026

@sleitor is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom tags prematurely close when followed by a newline and a Markdown header (e.g., ##)

1 participant