question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
felipecrscommented, Sep 9, 2021

This feature would be very much appreciated for documentation regarding nested objects, such as a complex JSON schema.

1reaction
schontzcommented, Sep 10, 2021

This feature would help new electron docs.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found