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

get-english-headings.js 1.5 KB

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
30
31
32
33
34
35
36
37
  1. // capture h2, h3, and h4 headings
  2. const headingRegex = /^###?#? (.*?)$/gm
  3. // for any translated page, first get corresponding English markdown
  4. // then get the headings on both the translated and English pageMap
  5. // finally, create a map of translation:English for all headings on the page
  6. module.exports = function getEnglishHeadings (page, context) {
  7. // Special handling for glossaries, because their headings are
  8. // generated programatically.
  9. if (page.relativePath.endsWith('/github-glossary.md')) {
  10. // Return an object of `{ localized-term: english-slug }`
  11. const languageGlossary = context.site.data.glossaries.external
  12. return languageGlossary.reduce((prev, curr) => {
  13. prev[curr.term] = curr.slug
  14. return prev
  15. }, {})
  16. }
  17. const translatedHeadings = page.markdown.match(headingRegex)
  18. if (!translatedHeadings) return
  19. const englishPage = context.pages[`/en/${page.relativePath.replace(/.md$/, '')}`]
  20. if (!englishPage) return
  21. // FIX there may be bugs if English headings are updated before Crowdin syncs up :/
  22. const englishHeadings = englishPage.markdown.match(headingRegex)
  23. if (!englishHeadings) return
  24. // select heading text only
  25. const cleanTranslatedHeadings = translatedHeadings.map(h => h.replace(headingRegex, '$1'))
  26. const cleanEnglishHeadings = englishHeadings.map(h => h.replace(headingRegex, '$1'))
  27. // return a map from translation:English
  28. return Object.assign(...cleanTranslatedHeadings.map((k, i) => ({
  29. [k]: cleanEnglishHeadings[i]
  30. })))
  31. }
Tip!

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

Comments

Loading...