Skip to content
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

Suppresses header check inside frontmatter #14203

Merged
merged 5 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/toc/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,35 @@ export function getHeadings(text: string): IMarkdownHeading[] {
// Iterate over the lines to get the header level and text for each line:
const headings = new Array<IMarkdownHeading>();
let isCodeBlock;
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
let lineIdx = 0;

// Don't check for Markdown headings if in a YAML frontmatter block.
// We can only start a frontmatter block on the first line of the file.
// At other positions in a markdown file, '---' represents a horizontal rule.
if (lines[lineIdx] === '---') {
// Search for another '---' and treat that as the end of the frontmatter.
// If we don't find one, treat the file as containing no frontmatter.
for (
let frontmatterEndLineIdx = lineIdx + 1;
frontmatterEndLineIdx < lines.length;
frontmatterEndLineIdx++
) {
if (lines[frontmatterEndLineIdx] === '---') {
lineIdx = frontmatterEndLineIdx + 1;
break;
}
}
}

for (; lineIdx < lines.length; lineIdx++) {
const line = lines[lineIdx];

if (line === '') {
// Bail early
continue;
}

// Don't check for Markdown headings if in a code block:
// Don't check for Markdown headings if in a code block
if (line.startsWith('```')) {
isCodeBlock = !isCodeBlock;
}
Expand Down
91 changes: 90 additions & 1 deletion packages/toc/test/markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,96 @@ describe('TableOfContentsUtils', () => {
['\nTitle\n\n--', []],
['```\n# Title\n```', []],
['```\nTitle\n--\n```', []],
['```\n<h1>Title</h1>\n```', []]
['```\n<h1>Title</h1>\n```', []],
['---\n<h1>Title</h1>\n---', []],
['---\n# Title\n---', []],
[
`---
<h1>Ignored</h1>
---
# Title`,
[
{
text: 'Title',
level: 1,
line: 3,
raw: '# Title',
prefix: '1. ',
skip: false
}
]
],
[
`---
front: matter
---

# Header

> this has whitespace _after_`,
[
{
text: 'Header',
level: 1,
line: 4,
raw: '# Header',
prefix: '1. ',
skip: false
}
]
],
[
`---
front: matter
---
# Header

---
# Header between horizontal rules
---

# Header after horizontal rules`,
[
{
text: 'Header',
level: 1,
line: 3,
raw: '# Header',
prefix: '1. ',
skip: false
},
{
text: 'Header between horizontal rules',
level: 1,
line: 6,
raw: '# Header between horizontal rules',
prefix: '2. ',
skip: false
},
{
text: 'Header after horizontal rules',
level: 1,
line: 9,
raw: '# Header after horizontal rules',
prefix: '3. ',
skip: false
}
]
],
[
`---
# Header`,
[
{
text: 'Header',
level: 1,
line: 1,
raw: '# Header',
prefix: '1. ',
skip: false
}
]
]
])('should extract headings from %s', (src, headers) => {
const headings = TableOfContentsUtils.filterHeadings(
TableOfContentsUtils.Markdown.getHeadings(src),
Expand Down