Configure table-of-contents heading levels (h2, h3, h4...)
See original GitHub issue💥 Proposal
(A clear and concise description of what the proposal is.)
Have you read the Contributing Guidelines on issues?
I read the issue side.
Docs: ToC is created using only <h2>
and <h3>
.
After changing node_modules/@docusaurus/mdx-loader/src/remark/rightToc/search.js
with the code below and setting minDepth and maxDepth, Users can display the ToC in the desired range.
This code has only been tested on my page. I am not familiar with javascript and I always write docs from <h1>
(#). So I’m not sure if the code below is ok.
function search(node) {
const headings = [];
const minDepth = 1;
const maxDepth = 4;
const current = Array(maxDepth - minDepth + 1).fill(-1);
let previousDepth = 0;
const onHeading = (child, index, parent) => {
const value = toString(child);
if (parent !== node || !value || child.depth > maxDepth || child.depth < minDepth) {
return;
}
const entry = {
value: toValue(child),
id: child.data.id,
children: [],
};
if (!headings.length || previousDepth >= child.depth) {
previousDepth = child.depth;
current.fill(-1, child.depth - minDepth + 1)
}
index = child.depth - minDepth;
let table = headings;
for(let i = 0; i < index; i++) {
table = table[current[i]].children;
}
table.push(entry);
current[index] += 1;
};
visit(node, 'heading', onHeading);
return headings;
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Add a Table of Contents to Your Website
Best practices stipulate that an h3 tag must be preceded by an h2 tag so that each subheading is always preceded by a...
Read more >Editing Heading Levels for a Print TOC
The mc-heading-level property is used to indicate a TOC level for a style. This property is already set to 1 for h1 styles,...
Read more >Headings and Table of contents
Each Markdown document displays a table of contents on the top-right corner. By default, this table only shows h2 and h3 headings, which...
Read more ><h1>–<h6>: The HTML Section Heading elements
The to HTML elements represent six levels of section headings. is the highest section level and is the lowest.
Read more >How to add a Table of Contents (TOC) to WordPress
Gutenberg Block; ToC Plugins; Manually create a ToC with anchor links; How to add ... Choose the heading levels to include (H2, H3,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This feature would be very much appreciated for documentation regarding nested objects, such as a complex JSON schema.
This feature would help new electron docs.