Skip to content

Configure table-of-contents heading levels (h2, h3, h4...) #2700

Description

@hhk7734

💥 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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty: intermediateIssues that are medium difficulty level, e.g. moderate refactoring with a clear test plan.featureThis is not a bug or issue with Docusausus, per se. It is a feature request for the future.good first issueIf you are just getting started with Docusaurus, this issue should be a good place to begin.hacktoberfesthelp wantedAsking for outside help and/or contributions to this particular issue or PR.mlhMajor League Hacking Fellowship

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions