1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/lib/get-english-headings.js
Kevin Heis d68dde17d1 Upgrade pipeline with env COMMONMARK=1 npm start to see new, otherwise parse current (#20508)
* Update the trim nightmare

* Update create-processor.js

* Update other packages in the rendering pipeline

* A few more updates

* Fix tables

* Update lint-files.js

* Fix copy code blocks

* Update render-content.js

* remove whitespace from liquid conditionals

* We no longer need require eslint rules

* Neat, it worked

* Revert test change

* Update create-processor.js

* Without aliases

Co-authored-by: Chiedo John <2156688+chiedo@users.noreply.github.com>
Co-authored-by: Rachael Sewell <rachmari@github.com>
2021-07-29 14:24:26 +00:00

55 lines
1.7 KiB
JavaScript

import { fromMarkdown } from 'mdast-util-from-markdown'
import { toString } from 'mdast-util-to-string'
import { visit } from 'unist-util-visit'
import findPage from './find-page.js'
// 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, 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 = 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 Crowdin syncs up :/
const englishHeadings = getHeadings(englishPage.markdown)
if (!englishHeadings.length) return
// return a map from translation:English
return Object.assign(
...translatedHeadings.map((k, i) => ({
[k]: englishHeadings[i],
}))
)
}
function getHeadings(markdown) {
const ast = fromMarkdown(markdown)
const headings = []
visit(ast, (node) => {
if (node.type !== 'heading') return
if (![2, 3, 4].includes(node.depth)) return
headings.push(toString(node))
})
return headings
}