MCPcopy Index your code
hub / github.com/github/docs / rewriteLocalLinks

Function rewriteLocalLinks

lib/render-content/plugins/rewrite-local-links.js:30–69  ·  view source on GitHub ↗
(context)

Source from the content-addressed store, hash-verified

28// Content authors write links like `/some/article/path`, but they need to be
29// rewritten on the fly to match the current language and page version
30export default function rewriteLocalLinks(context) {
31 const { currentLanguage, currentVersion } = context
32 // There's no languageCode or version passed, so nothing to do
33 if (!currentLanguage || !currentVersion) return
34
35 return async (tree) => {
36 const promises = []
37
38 visit(tree, matcher, (node) => {
39 const newHref = getNewHref(node, currentLanguage, currentVersion)
40 if (newHref) {
41 node.properties.href = newHref
42 }
43 for (const child of node.children) {
44 if (child.value) {
45 if (AUTOTITLE.test(child.value)) {
46 promises.push(getNewTitleSetter(child, node.properties.href, context))
47 } else if (process.env.NODE_ENV !== 'production') {
48 // Throw if the link text *almost* is AUTOTITLE
49 if (
50 child.value.toUpperCase() === 'AUTOTITLE' ||
51 distance(child.value.toUpperCase(), 'AUTOTITLE') <= 2
52 ) {
53 throw new Error(
54 `Found link text '${child.value}', expected 'AUTOTITLE'. ` +
55 `Find the mention of the link text '${child.value}' and change it to 'AUTOTITLE'. Case matters.`
56 )
57 }
58 }
59 }
60 }
61 })
62
63 if (promises.length) {
64 await Promise.all(promises)
65 }
66
67 return tree
68 }
69}
70
71async function getNewTitleSetter(child, href, context) {
72 child.value = await getNewTitle(href, context)

Callers

nothing calls this directly

Calls 2

getNewHrefFunction · 0.85
getNewTitleSetterFunction · 0.85

Tested by

no test coverage detected