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

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

Closed
hhk7734 opened this issue Apr 30, 2020 · 6 comments · Fixed by #5578
Closed

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

hhk7734 opened this issue Apr 30, 2020 · 6 comments · Fixed by #5578
Labels
difficulty: intermediate Issues that are medium difficulty level, e.g. moderate refactoring with a clear test plan. feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin. hacktoberfest help wanted Asking for outside help and/or contributions to this particular issue or PR. mlh Major League Hacking Fellowship

Comments

@hhk7734
Copy link

hhk7734 commented Apr 30, 2020

💥 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;
}
@hhk7734 hhk7734 added status: needs triage This issue has not been triaged by maintainers proposal This issue is a proposal, usually non-trivial change labels Apr 30, 2020
@fanny
Copy link
Contributor

fanny commented May 9, 2020

Hey @hhk7734 I appreciate your suggestion, but I don't know if make sense allows the TOC in every level of Heading. The goal of the TOC is set highlights for sections, if you put a Head like h5, h6 sounds like less important, so for me is ok not include in TOC.

@hhk7734
Copy link
Author

hhk7734 commented May 9, 2020

Hi @fanny
The range that user want to display may be different. I want to be able to set the range. So I used minDepth and maxDepth in the code.

@lex111
Copy link
Contributor

lex111 commented May 19, 2020

I think this will be a useful feature, definitely we need it. However, the algorithm presented in the issue does not seem reliable, we need to work on a better solution. PR as always welcome.

Similar issue on Canny - https://docusaurus.canny.io/admin/board/feature-requests/p/configuration-option-for-table-of-content

@lex111 lex111 added help wanted Asking for outside help and/or contributions to this particular issue or PR. feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin. and removed status: needs triage This issue has not been triaged by maintainers proposal This issue is a proposal, usually non-trivial change labels May 19, 2020
@yangshun yangshun added mlh Major League Hacking Fellowship difficulty: intermediate Issues that are medium difficulty level, e.g. moderate refactoring with a clear test plan. labels Jun 5, 2020
@slorber slorber changed the title Set ToC in the desired range. Configure TOC heading levels (h2, h3, h4...) Dec 29, 2020
@slorber slorber changed the title Configure TOC heading levels (h2, h3, h4...) Configure table-of-contents heading levels (h2, h3, h4...) Dec 29, 2020
@vjpr
Copy link

vjpr commented Mar 4, 2021

A workaround to make it configurable per doc is to use the suggest plugin above and configure via first comment in document.

<!-- {tocMinDepth: 1, tocMaxDepth: 4} -->

# h1
const plugin = (options = {}) => {
  const name = options.name || 'toc';

  const transformer = (node) => {

    // Because frontmatter is not passed along we must use comments instead for config.
    const firstComment = node.children.find(node => node.type === 'comment')
    let config = {}
    if (firstComment) {
      config = require('json5').parse(firstComment.value)
    }
    // --

Have to use hacky comments because frontmatter is inaccessible. See: #3935

@felipecrs
Copy link
Contributor

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

@schontz
Copy link

schontz commented Sep 10, 2021

This feature would help new electron docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: intermediate Issues that are medium difficulty level, e.g. moderate refactoring with a clear test plan. feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin. hacktoberfest help wanted Asking for outside help and/or contributions to this particular issue or PR. mlh Major League Hacking Fellowship
Projects
None yet
7 participants