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

feat(v2): allow specifying TOC max depth (themeConfig + frontMatter) #5578

Merged
merged 36 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b9266ad
feat: add all TOC levels to MDX loader
erickzhao Sep 16, 2021
9fbef88
feat: add theme-level config for heading depth
erickzhao Sep 16, 2021
fda5103
test: add remark MDX loader test
erickzhao Sep 16, 2021
fd1b5f7
fix: limit maxDepth validation to H2 - H6
erickzhao Sep 16, 2021
0785423
refactor: set default `maxDepth` using `joi`
erickzhao Sep 17, 2021
7269c0f
refactor: `maxDepth` -> `maxHeadingLevel
erickzhao Sep 17, 2021
213289e
refactor: invert underlying TOC depth API
erickzhao Sep 19, 2021
9c363cc
refactor: make TOC algorithm level-aware
erickzhao Sep 20, 2021
f4973c2
feat: add support for per-doc TOC heading levels
erickzhao Sep 20, 2021
7a1eeeb
feat: support document-level heading levels for blog
erickzhao Sep 20, 2021
f3d61b2
fix: correct validation for toc level frontmatter
erickzhao Sep 20, 2021
d2b234a
fix: ensure TOC doesn't generate redundant DOM
erickzhao Sep 20, 2021
0a1e91e
perf: simpler TOC heading search alg
erickzhao Sep 21, 2021
6ff892d
docs: document heading level props for `TOCInline`
erickzhao Sep 21, 2021
2abd6a8
Update website/docs/guides/markdown-features/markdown-features-inline…
erickzhao Sep 21, 2021
a00e19e
docs: fix docs (again)
erickzhao Sep 21, 2021
7d39b26
create dedicated test file for heading searching logic: exhaustive t…
slorber Sep 24, 2021
d3fe29f
toc search: add real-world test
slorber Sep 24, 2021
787057a
fix test
slorber Sep 24, 2021
9ef5679
add dogfooding tests for toc min/max
slorber Sep 24, 2021
d9fbbd6
add test for min/max toc frontmatter
slorber Sep 24, 2021
c581d56
reverse min/max order
slorber Sep 24, 2021
8e8e311
add theme minHeadingLevel + tests
slorber Sep 24, 2021
73ad639
simpler TOC rendering logic
slorber Sep 24, 2021
90c0943
simplify TOC implementation (temp, WIP)
slorber Sep 24, 2021
40cdce6
reverse unnatural order for minHeadingLevel/maxHeadingLevel
slorber Sep 28, 2021
f8026f9
add TOC dogfooding tests to all content plugins
slorber Sep 28, 2021
ee8f31c
expose toc min/max heading level frontmatter to all 3 content plugins
slorber Sep 28, 2021
f222017
refactor blogLayout: accept toc ReactElement directly
slorber Sep 28, 2021
7e2c562
move toc utils to theme-common
slorber Sep 28, 2021
9e99c69
add tests for filterTOC
slorber Sep 28, 2021
11b78da
create new generic TOCItems component
slorber Sep 28, 2021
91248eb
useless css file copied
slorber Sep 28, 2021
e01ad12
fix toc highlighting className conflicts
slorber Sep 28, 2021
7988a43
update doc
slorber Sep 28, 2021
3db8779
fix types
slorber Sep 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@ exports[`inline code should be escaped 1`] = `
{
value: '<code>&lt;Head&gt;Test&lt;/Head&gt;</code>',
id: 'headtesthead',
children: []
children: [],
level: 3
}
]
],
level: 2
},
{
value: '<code>&lt;div /&gt;</code>',
id: 'div-',
children: []
children: [],
level: 2
},
{
value: '<code>&lt;div&gt; Test &lt;/div&gt;</code>',
id: 'div-test-div',
children: []
children: [],
level: 2
},
{
value: '<code>&lt;div&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/div&gt;</code>',
id: 'divitestidiv',
children: []
children: [],
level: 2
}
];

Expand All @@ -51,24 +56,29 @@ exports[`non text phrasing content 1`] = `
{
value: '<strong>Importance</strong>',
id: 'importance',
children: []
children: [],
level: 3
}
]
],
level: 2
},
{
value: '<del>Strikethrough</del>',
id: 'strikethrough',
children: []
children: [],
level: 2
},
{
value: '<i>HTML</i>',
id: 'html',
children: []
children: [],
level: 2
},
{
value: '<code>inline.code()</code>',
id: 'inlinecode',
children: []
children: [],
level: 2
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import vfile from 'to-vfile';
import plugin from '../index';
import headings from '../../headings/index';

const processFixture = async (name, options) => {
const processFixture = async (name, options?) => {
const path = join(__dirname, 'fixtures', `${name}.md`);
const file = await vfile.read(path);
const result = await remark()
Expand Down Expand Up @@ -41,7 +41,8 @@ test('text content', async () => {
{
value: 'Endi',
id: 'endi',
children: []
children: [],
level: 3
},
{
value: 'Endi',
Expand All @@ -50,14 +51,17 @@ test('text content', async () => {
{
value: 'Yangshun',
id: 'yangshun',
children: []
children: [],
level: 3
}
]
],
level: 2
},
{
value: 'I ♥ unicode.',
id: 'i--unicode',
children: []
children: [],
level: 2
}
];

Expand Down Expand Up @@ -87,7 +91,8 @@ test('should export even with existing name', async () => {
{
value: 'Thanos',
id: 'thanos',
children: []
children: [],
level: 2
},
{
value: 'Tony Stark',
Expand All @@ -96,9 +101,11 @@ test('should export even with existing name', async () => {
{
value: 'Avengers',
id: 'avengers',
children: []
children: [],
level: 3
}
]
],
level: 2
}
];

Expand All @@ -121,7 +128,8 @@ test('should export with custom name', async () => {
{
value: 'Endi',
id: 'endi',
children: []
children: [],
level: 3
},
{
value: 'Endi',
Expand All @@ -130,14 +138,17 @@ test('should export with custom name', async () => {
{
value: 'Yangshun',
id: 'yangshun',
children: []
children: [],
level: 3
}
]
],
level: 2
},
{
value: 'I ♥ unicode.',
id: 'i--unicode',
children: []
children: [],
level: 2
}
];

Expand Down Expand Up @@ -171,7 +182,8 @@ test('should insert below imports', async () => {
{
value: 'Title',
id: 'title',
children: []
children: [],
level: 2
},
{
value: 'Test',
Expand All @@ -180,9 +192,11 @@ test('should insert below imports', async () => {
{
value: 'Again',
id: 'again',
children: []
children: [],
level: 3
}
]
],
level: 2
}
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import remark from 'remark';
import mdx from 'remark-mdx';
import search from '../search';
import headings from '../../headings/index';

const getHeadings = async (mdText: string) => {
const node = remark().parse(mdText);
const result = await remark().use(headings).use(mdx).run(node);
return search(result);
};

test('should process all heading levels', async () => {
const md = `
# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot

`;

expect(await getHeadings(md)).toEqual([
{
children: [
{
children: [
{
children: [
{
children: [
{
children: [],
id: 'foxtrot',
level: 6,
value: 'Foxtrot',
},
],
id: 'echo',
level: 5,
value: 'Echo',
},
],
id: 'delta',
level: 4,
value: 'Delta',
},
],
id: 'charlie',
level: 3,
value: 'Charlie',
},
],
id: 'bravo',
level: 2,
value: 'Bravo',
},
]);
});

test('should process real-world well-formatted md', async () => {
const md = `
# title

some text

## section 1

some text

### subsection 1-1

some text

#### subsection 1-1-1

some text

#### subsection 1-1-2

some text

### subsection 1-2

some text

### subsection 1-3

some text

## section 2

some text

### subsection 2-1

some text

### subsection 2-1

some text

## section 3

some text

### subsection 3-1

some text

### subsection 3-2

some text

`;

expect(await getHeadings(md)).toEqual([
{
children: [
{
children: [
{
children: [],
id: 'subsection-1-1-1',
level: 4,
value: 'subsection 1-1-1',
},
{
children: [],
id: 'subsection-1-1-2',
level: 4,
value: 'subsection 1-1-2',
},
],
id: 'subsection-1-1',
level: 3,
value: 'subsection 1-1',
},
{children: [], id: 'subsection-1-2', level: 3, value: 'subsection 1-2'},
{children: [], id: 'subsection-1-3', level: 3, value: 'subsection 1-3'},
],
id: 'section-1',
level: 2,
value: 'section 1',
},
{
children: [
{children: [], id: 'subsection-2-1', level: 3, value: 'subsection 2-1'},
{
children: [],
id: 'subsection-2-1-1',
level: 3,
value: 'subsection 2-1',
},
],
id: 'section-2',
level: 2,
value: 'section 2',
},
{
children: [
{children: [], id: 'subsection-3-1', level: 3, value: 'subsection 3-1'},
{children: [], id: 'subsection-3-2', level: 3, value: 'subsection 3-2'},
],
id: 'section-3',
level: 2,
value: 'section 3',
},
]);
});
Loading