fix(markdown): handle frontmatter in heading includes#5207
Open
Naloam wants to merge 2 commits into
Open
Conversation
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
Contributor
There was a problem hiding this comment.
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) totoken.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 on lines
+66
to
+67
| // Adjust line numbers to account for stripped frontmatter | ||
| start = token.map![0] + frontmatterLines |
Comment on lines
+49
to
+52
| const { content: contentWithoutFrontmatter } = matter(content) | ||
| const frontmatterLines = | ||
| content.split('\n').length - | ||
| contentWithoutFrontmatter.split('\n').length |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
frontmatterPluginstrips 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
<!--@include: ...#heading-->with source frontmatterpnpm run test:unitandpnpm run format:failto verify