-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(www): Heading ids are hierarchical (#814)
Allows for better, nicer visual presentation of the headings while still maintaining very meaningful and stable anchors.
- Loading branch information
Showing
18 changed files
with
148 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
import ghSlugger from "github-slugger"; | ||
import { hasProperty } from "hast-util-has-property"; | ||
import { heading } from "hast-util-heading"; | ||
import { headingRank } from "hast-util-heading-rank"; | ||
import { toString } from "hast-util-to-string"; | ||
import { visit } from "unist-util-visit"; | ||
|
||
const { slug : slugger } = ghSlugger; | ||
|
||
export default function rehypeHierarchicalSlug() { | ||
return (tree) => { | ||
const slugs = []; | ||
|
||
visit(tree, "element", (node) => { | ||
if(!heading(node) || !node.properties || hasProperty(node, "id")) { | ||
return; | ||
} | ||
|
||
const rank = headingRank(node); | ||
const slug = slugger(toString(node)); | ||
|
||
let last = slugs[slugs.length - 1]; | ||
|
||
while(last && last.rank >= rank) { | ||
slugs.pop(); | ||
|
||
last = slugs[slugs.length - 1]; | ||
} | ||
|
||
if(rank > 1) { | ||
slugs.push({ rank, slug }); | ||
} | ||
|
||
node.properties.id = slugs.map(({ slug : s }) => s).join("-"); | ||
}); | ||
}; | ||
} |
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
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
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
Oops, something went wrong.