MCPcopy
hub / github.com/github/docs / main

Function main

script/update-internal-links.js:57–193  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55main()
56
57async function main() {
58 console.log('Working...')
59 const pageList = await loadPages()
60 const pageMap = await loadPageMap(pageList)
61 const redirects = await loadRedirects(pageList)
62
63 const context = {
64 pages: pageMap,
65 redirects,
66 currentLanguage: 'en',
67 }
68
69 for (const file of allFiles) {
70 const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
71 let newContent = content
72
73 // Do a blanket find-replace for /enterprise/{{ currentVersion }}/ to /enterprise/{{currentVersion}}/
74 // so that the AST parser recognizes the link as a link node. The spaces prevent it from doing so.
75 newContent = newContent.replace(currentVersionWithSpacesRegex, currentVersionWithoutSpaces)
76
77 const ast = fromMarkdown(newContent)
78
79 // We can't do async functions within visit, so gather the nodes upfront
80 const nodesPerFile = []
81
82 visit(ast, (node) => {
83 if (node.type !== 'link') return
84 if (!node.url.startsWith('/')) return
85 if (node.url.startsWith('/assets')) return
86 if (node.url.startsWith('/public')) return
87 if (node.url.includes('/11.10.340/')) return
88 if (node.url.includes('/2.1/')) return
89 if (node.url === '/') return
90
91 nodesPerFile.push(node)
92 })
93
94 // For every Markdown link...
95 for (const node of nodesPerFile) {
96 const oldLink = node.url
97
98 // Find and preserve any inline markup in link titles, like [*Foo*](/foo)
99 let inlineMarkup = ''
100 if (node.children[0].children) {
101 inlineMarkup = linkInlineMarkup[node.children[0].type]
102
103 if (!inlineMarkup) {
104 console.error(`Cannot find an inline markup entry for ${node.children[0].type}!`)
105 process.exit(1)
106 }
107 }
108
109 const oldTitle = node.children[0].value || node.children[0].children[0].value
110 const oldMarkdownLink = `[${inlineMarkup}${oldTitle}${inlineMarkup}](${oldLink})`
111
112 // As a blanket rule, only update titles in links that begin with quotes. (Many links
113 // have punctuation before the closing quotes, so we'll only check for opening quotes.)
114 // Update: "[Foo](/foo)

Callers 1

Calls 6

loadPageMapFunction · 0.90
getPathWithoutLanguageFunction · 0.90
getPathWithoutVersionFunction · 0.90
frontmatterFunction · 0.85
renderContentFunction · 0.85
findPageFunction · 0.70

Tested by

no test coverage detected