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

Function main

script/content-migrations/update-developer-site-links.js:26–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24main()
25
26async function main() {
27 // we need to load the pages so we can get the redirects
28 const englishPages = (await loadPages()).filter((p) => p.languageCode === 'en')
29 const englishPageMap = await loadPageMap(englishPages)
30 const redirects = await loadRedirects(englishPages, englishPageMap)
31
32 for (const file of files) {
33 const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
34
35 const links = content.match(linkRegex)
36 if (!links) continue
37
38 // remove parentheses: (/v3) -> /v3
39 // also remove trailing slash before closing parens if there is one
40 const devLinks = links.map((link) => link.replace('(', '').replace(/\/?\)/, ''))
41
42 let newContent = content
43
44 for (const devLink of devLinks) {
45 const [link, fragment] = devLink.split(/\/?#/)
46
47 let redirect = redirects[link]
48
49 if (!redirect) {
50 console.log(`no redirect found for ${devLink} in ${file}`)
51 continue
52 }
53
54 // do some cleanup
55 redirect = redirect
56 // remove language code segment
57 .replace(patterns.getLanguageCode, '')
58 // remove version segment
59 .replace(new RegExp(`/(${allVersionKeys.join('|')})`), '')
60
61 // re-add the fragment after removing any fragment added via the redirect
62 // otherwise /v3/git/refs/#create-a-reference will become /rest/reference/git#refs#create-a-reference
63 // we want to preserve the #create-a-reference fragment, not #refs
64 const newLink = fragment ? redirect.replace(/#.+?$/, '') + '#' + fragment : redirect
65
66 // first replace the old link with the new link
67 // then remove any trailing slashes
68 newContent = newContent.replace(new RegExp(`${devLink}/?(?=\\))`), newLink)
69 }
70
71 fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
72 }
73 console.log('Done!')
74}

Callers 1

Calls 2

loadPageMapFunction · 0.90
frontmatterFunction · 0.85

Tested by

no test coverage detected