Skip to content

Commit

Permalink
fix(shared): avoid resolve headers in nested blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 23, 2022
1 parent 3bc939c commit ce146bd
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/shared/src/resolve-headers-from-tokens.ts
Expand Up @@ -59,8 +59,8 @@ export const resolveHeadersFromTokens = (
for (let i = 0; i < tokens.length; i += 1) {
const token = tokens[i];

// if the token type does not match, skip
if (token?.type !== 'heading_open') {
// if the token type does not match, or the token level is not 0, skip
if (token?.type !== 'heading_open' || token?.level !== 0) {
continue;
}

Expand Down
110 changes: 110 additions & 0 deletions packages/shared/tests/resolve-headers-from-tokens.spec.ts
@@ -0,0 +1,110 @@
import type { MarkdownItHeader } from '@mdit-vue/types';
import MarkdownIt from 'markdown-it';
import { describe, expect, it } from 'vitest';
import { resolveHeadersFromTokens, slugify } from '../src/index.js';

describe('shared > resolve-headers-from-tokens', () => {
const md = MarkdownIt();

it('should resolve headers correctly', () => {
const source = `\
# h1
## h2
### h3
#### h4
##### h5
###### h6
`;
const tokens = md.parse(source, {});
const expected: MarkdownItHeader[] = [
{
level: 1,
title: 'h1',
slug: 'h1',
link: '#h1',
children: [
{
level: 2,
title: 'h2',
slug: 'h2',
link: '#h2',
children: [
{
level: 3,
title: 'h3',
slug: 'h3',
link: '#h3',
children: [
{
level: 4,
title: 'h4',
slug: 'h4',
link: '#h4',
children: [],
},
],
},
],
},
],
},
];

expect(
resolveHeadersFromTokens(tokens, {
level: [1, 2, 3, 4],
slugify,
shouldAllowHtml: false,
shouldEscapeText: false,
}),
).toEqual(expected);
});

it('should not resolve headers in nested blocks', () => {
const source = `\
> # h1 in blockquote
> ## h2 in blockquote
> ### h3
> #### h4 in blockquote
> ##### h5 in blockquote
> ###### h6 in blockquote
- # h1 in list
- ## h2 in list
- ### h3 in list
- #### h4 in list
- ##### h5 in list
- ###### h6 in list
# h1 outside nested blocks
## h2 outside nested blocks
`;
const tokens = md.parse(source, {});
const expected: MarkdownItHeader[] = [
{
level: 1,
title: 'h1 outside nested blocks',
slug: 'h1-outside-nested-blocks',
link: '#h1-outside-nested-blocks',
children: [
{
level: 2,
title: 'h2 outside nested blocks',
slug: 'h2-outside-nested-blocks',
link: '#h2-outside-nested-blocks',
children: [],
},
],
},
];

expect(
resolveHeadersFromTokens(tokens, {
level: [1, 2, 3, 4],
slugify,
shouldAllowHtml: false,
shouldEscapeText: false,
}),
).toEqual(expected);
});
});

0 comments on commit ce146bd

Please sign in to comment.