Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

use-english-headings.js 989 B

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  1. const GithubSlugger = require('github-slugger')
  2. const Entities = require('html-entities').XmlEntities
  3. const toString = require('hast-util-to-string')
  4. const visit = require('unist-util-visit')
  5. const slugger = new GithubSlugger()
  6. const entities = new Entities()
  7. const matcher = node => (
  8. node.type === 'element' &&
  9. ['h2', 'h3', 'h4'].includes(node.tagName)
  10. )
  11. // replace translated IDs and links in headings with English
  12. module.exports = function useEnglishHeadings ({ englishHeadings }) {
  13. if (!englishHeadings) return
  14. return tree => {
  15. visit(tree, matcher, node => {
  16. slugger.reset()
  17. // Get the plain text content of the heading node
  18. const text = toString(node)
  19. // find English heading in the collection
  20. const englishHeading = englishHeadings[entities.encode(text)]
  21. // get English slug
  22. const englishSlug = slugger.slug(englishHeading)
  23. // use English slug for heading ID and link
  24. node.properties.id = englishSlug
  25. })
  26. }
  27. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...