1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/lib/render-content/plugins/use-english-headings.js
Kevin Heis 43158e8392 Upgrade NPM packages (#21480)
* Upgrade a bunch of packages

* ...and upgrade highlightjs

* ... and ajv-formats (but not ajv)

* Revert graphql changes

* Revert "Revert graphql changes"

This reverts commit a5b8a34d52a48a7c2858415f6b1081c1d4902482.

* Update build-changelog.js

* Upgrade html-entities

* Upgrade commander, following migration guide
2021-09-13 23:31:43 +00:00

26 lines
921 B
JavaScript

import GithubSlugger from 'github-slugger'
import { encode } from 'html-entities'
import { toString } from 'hast-util-to-string'
import { visit } from 'unist-util-visit'
const slugger = new GithubSlugger()
const matcher = (node) => node.type === 'element' && ['h2', 'h3', 'h4'].includes(node.tagName)
// replace translated IDs and links in headings with English
export default function useEnglishHeadings({ englishHeadings }) {
if (!englishHeadings) return
return (tree) => {
visit(tree, matcher, (node) => {
slugger.reset()
// Get the plain text content of the heading node
const text = toString(node)
// find English heading in the collection
const englishHeading = englishHeadings[encode(text)]
// get English slug
const englishSlug = slugger.slug(englishHeading)
// use English slug for heading ID and link
node.properties.id = englishSlug
})
}
}