Skip to content

Commit

Permalink
Updates frontmatter detection, updates test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonWeill committed Mar 16, 2023
1 parent daeac85 commit fc80fa5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/toc/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ export function getHeadings(text: string): IMarkdownHeading[] {
continue;
}

// Don't check for Markdown headings if in a YAML frontmatter block
if (line === '---') {
// 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 (line === '---' && (isFrontmatterBlock || lineIdx === 0)) {
isFrontmatterBlock = !isFrontmatterBlock;
}

if (isFrontmatterBlock) {
continue;
}
Expand Down
61 changes: 60 additions & 1 deletion packages/toc/test/markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ describe('TableOfContentsUtils', () => {
['---\n<h1>Title</h1>\n---', []],
['---\n# Title\n---', []],
[
'---\n<h1>Ignored</h1>\n---\n# Title',
`---
<h1>Ignored</h1>
---
# Title`,
[
{
text: 'Title',
Expand All @@ -208,6 +211,62 @@ describe('TableOfContentsUtils', () => {
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
}
]
])('should extract headings from %s', (src, headers) => {
const headings = TableOfContentsUtils.filterHeadings(
Expand Down

0 comments on commit fc80fa5

Please sign in to comment.