This repository was archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(toc): 🐛 fix issue with table of contents not following properly
Sections generated by gatsby-remark-sectionize were nested, while spying nested sections is unsupported by react-scrollspy. Thus needed to fork remark-sectionize plugin and change it to take into account ToC depth.
- Loading branch information
Showing
5 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const sectionizeToc = require('./sectionize-toc'); | ||
|
||
const transform = sectionizeToc(); | ||
|
||
module.exports = function ({ markdownAST }, pluginOptions ) { | ||
transform(markdownAST, pluginOptions.maxDepth); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "gatsby-remark-sectionize-toc", | ||
"description": "Forked sectionize plugin used to generate sections to make react-scrollspy work with Table of Contents", | ||
"author": "Mateusz Filipowicz <[email protected]>", | ||
"version": "0.1.0", | ||
"dependencies": { | ||
"remark": "^12.0.0" | ||
}, | ||
"license": "MIT", | ||
"main": "index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const findAfter = require('unist-util-find-after'); | ||
const visit = require('unist-util-visit-parents'); | ||
|
||
const MAX_HEADING_DEPTH = 6; | ||
|
||
module.exports = () => transform; | ||
|
||
const transform = (tree, maxDepth) => { | ||
const maxTocDepth = maxDepth ? maxDepth : MAX_HEADING_DEPTH; | ||
const visitFunction = sectionize(maxTocDepth); | ||
for (let depth = MAX_HEADING_DEPTH; depth > 0; depth--) { | ||
visit(tree, (node) => node.type === 'heading' && node.depth === depth, visitFunction); | ||
} | ||
}; | ||
const sectionize = (maxTocDepth) => { | ||
return (node, ancestors) => { | ||
const start = node; | ||
const depth = start.depth; | ||
const parent = ancestors[ancestors.length - 1]; | ||
|
||
const isEnd = (node) => | ||
(node.type === 'heading' && node.depth <= depth) || | ||
(node.type === 'section' && node.depth > depth && node.depth <= maxTocDepth) || | ||
node.type === 'export'; | ||
const end = findAfter(parent, start, isEnd); | ||
|
||
const startIndex = parent.children.indexOf(start); | ||
const endIndex = parent.children.indexOf(end); | ||
|
||
const between = parent.children.slice(startIndex, endIndex > 0 ? endIndex : undefined); | ||
|
||
const section = { | ||
type: 'section', | ||
depth: depth, | ||
children: between, | ||
data: { | ||
hName: 'section', | ||
}, | ||
}; | ||
parent.children.splice(startIndex, section.children.length, section); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters