-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathget-english-headings.ts
More file actions
78 lines (66 loc) · 2.34 KB
/
get-english-headings.ts
File metadata and controls
78 lines (66 loc) · 2.34 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { fromMarkdown } from 'mdast-util-from-markdown'
import { toString } from 'mdast-util-to-string'
import { visit } from 'unist-util-visit'
import type { Node } from 'unist'
import findPage from '@/frame/lib/find-page'
import { getDataByLanguage } from '@/data-directory/lib/get-data'
import type { Context } from '@/types'
interface HeadingNode extends Node {
type: 'heading'
depth: number
}
interface PageWithMarkdown {
relativePath: string
markdown: string
}
interface GlossaryTerm {
term: string
slug: string
}
// 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
export default function getEnglishHeadings(
page: PageWithMarkdown,
context: Context,
): Record<string, string> | undefined {
// Special handling for glossaries, because their headings are
// generated programmatically.
if (page.relativePath.endsWith('/github-glossary.md')) {
// Return an object of `{ localized-term: english-slug }`
const languageGlossary = getDataByLanguage('glossaries.external', 'en') as GlossaryTerm[]
return languageGlossary.reduce((prev: Record<string, string>, curr: GlossaryTerm) => {
prev[curr.term] = curr.slug
return prev
}, {})
}
const translatedHeadings = getHeadings(page.markdown)
if (!translatedHeadings.length) return
const englishPage = findPage(
`/en/${page.relativePath.replace(/.md$/, '')}`,
context.pages,
context.redirects,
)
if (!englishPage) return
// FIX there may be bugs if English headings are updated before translations sync up :/
const englishHeadings = getHeadings(englishPage.markdown)
if (!englishHeadings.length) return
// return a map from translation:English
const headingMap: Record<string, string> = {}
for (let i = 0; i < translatedHeadings.length; i++) {
const k = translatedHeadings[i]
headingMap[k] = englishHeadings[i]
}
return headingMap
}
function getHeadings(markdown: string): string[] {
const ast = fromMarkdown(markdown)
const headings: string[] = []
visit(ast, (node: Node) => {
if (node.type !== 'heading') return
const headingNode = node as HeadingNode
if (![2, 3, 4].includes(headingNode.depth)) return
headings.push(toString(node))
})
return headings
}