Skip to content

fix(markdown): handle frontmatter in heading includes#5207

Open
Naloam wants to merge 2 commits into
vuejs:mainfrom
Naloam:fix/include-frontmatter-heading-range
Open

fix(markdown): handle frontmatter in heading includes#5207
Naloam wants to merge 2 commits into
vuejs:mainfrom
Naloam:fix/include-frontmatter-heading-range

Conversation

@Naloam

@Naloam Naloam commented Jun 2, 2026

Copy link
Copy Markdown

Description

When using <!--@include: ...#heading--> to include a section from a Markdown file with frontmatter, the included content was incorrect because line numbers were misaligned.

The frontmatterPlugin strips frontmatter before parsing, so token line numbers are based on content without frontmatter. But the slice operation used those numbers on the original content (with frontmatter), causing an offset.

Fix by explicitly stripping frontmatter before parsing and adjusting line numbers accordingly.

Linked Issues

fixes #5202

Additional Context

  • Add a regression test for <!--@include: ...#heading--> with source frontmatter
  • Adjust include processing so heading ranges are resolved correctly when frontmatter is present
  • Run pnpm run test:unit and pnpm run format:fail to verify

When using `<!--@include: ...#heading-->` to include a section from a
Markdown file with frontmatter, the included content was incorrect
because line numbers were misaligned.

The `frontmatterPlugin` strips frontmatter before parsing, so token
line numbers are based on content without frontmatter. But the slice
operation used those numbers on the original content (with frontmatter),
causing an offset.

Fix by explicitly stripping frontmatter before parsing and adjusting
line numbers accordingly.

Fixes vuejs#5202
Copilot AI review requested due to automatic review settings June 2, 2026 07:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes incorrect line offsets when a Markdown <!--@include: file#heading--> directive points at a file that begins with YAML frontmatter. The markdown-it parser reports line numbers relative to the parsed content, but the include logic was slicing from the original (unstripped) file, so the wrong section was being included.

Changes:

  • Strip frontmatter before parsing with markdown-it and offset the returned token line numbers by the number of frontmatter lines.
  • Switch the section start from token.map[1] (line after heading) to token.map[0] (heading line itself), and add a fallback so the section extends to end-of-file when no sibling/parent heading follows.
  • Add a unit test suite plus fixtures covering heading includes with/without frontmatter and full-file includes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/node/utils/processIncludes.ts Strip frontmatter prior to markdown parsing and adjust token line offsets; include the heading line and handle missing end token.
tests/unit/node/utils/processIncludes.test.ts New unit tests for region/heading includes with and without frontmatter.
tests/unit/node/utils/fixtures/include-frontmatter/source.md Test fixture with frontmatter and multiple sections.
tests/unit/node/utils/fixtures/include-frontmatter/source-no-frontmatter.md Test fixture without frontmatter.
tests/unit/node/utils/fixtures/include-frontmatter/importing.md Importing file using the include directive.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/node/utils/processIncludes.ts Outdated
Comment on lines +66 to +67
// Adjust line numbers to account for stripped frontmatter
start = token.map![0] + frontmatterLines
Comment thread src/node/utils/processIncludes.ts Outdated
Comment on lines +49 to +52
const { content: contentWithoutFrontmatter } = matter(content)
const frontmatterLines =
content.split('\n').length -
contentWithoutFrontmatter.split('\n').length
Comment thread src/node/utils/processIncludes.ts Outdated
Comment on lines +75 to +78
// If no end found, include rest of content
if (end === undefined) {
end = lines.length
}
Comment on lines +22 to +34
// The result should NOT contain frontmatter
expect(result).not.toContain(
'description: This page has frontmatter description'
)
expect(result).not.toContain('This page has frontmatter description')

// The result should NOT contain content from other sections
expect(result).not.toContain('Intro text before the target heading.')
expect(result).not.toContain('This line should NOT be included.')
expect(result).not.toContain('## Other Section')

// The result should NOT contain the frontmatter description as content
expect(result).not.toMatch(/This page has frontmatter description/)
- Revert start to token.map![1] to preserve original heading exclusion behavior
- Detect frontmatter span directly by scanning for closing --- line
- Remove redundant end === undefined check (slice handles undefined natively)
- Remove duplicate test assertions
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.

Using <!--@include: from page with frontmatter description breaks imported text

2 participants