forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-english-headings.js
37 lines (31 loc) · 1.48 KB
/
get-english-headings.js
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
// capture h2, h3, and h4 headings
const headingRegex = /^###?#? (.*?)$/gm
// for any translated page, first get corresponding English markdown
// then get the headings on both the translated and English pageMap
// finally, create a map of translation:English for all headings on the page
module.exports = function getEnglishHeadings (page, context) {
// Special handling for glossaries, because their headings are
// generated programatically.
if (page.relativePath.endsWith('/github-glossary.md')) {
// Return an object of `{ localized-term: english-slug }`
const languageGlossary = context.site.data.glossaries.external
return languageGlossary.reduce((prev, curr) => {
prev[curr.term] = curr.slug
return prev
}, {})
}
const translatedHeadings = page.markdown.match(headingRegex)
if (!translatedHeadings) return
const englishPage = context.pages[`/en/${page.relativePath.replace(/.md$/, '')}`]
if (!englishPage) return
// FIX there may be bugs if English headings are updated before Crowdin syncs up :/
const englishHeadings = englishPage.markdown.match(headingRegex)
if (!englishHeadings) return
// select heading text only
const cleanTranslatedHeadings = translatedHeadings.map(h => h.replace(headingRegex, '$1'))
const cleanEnglishHeadings = englishHeadings.map(h => h.replace(headingRegex, '$1'))
// return a map from translation:English
return Object.assign(...cleanTranslatedHeadings.map((k, i) => ({
[k]: cleanEnglishHeadings[i]
})))
}