Skip to content

Commit 39bad0e

Browse files
authored
Script to update v3 and v4 links (#16434)
* add script * add README to content-migrations subdir
1 parent 7b31c08 commit 39bad0e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

script/content-migrations/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Content migration scripts
2+
3+
This directory stores scripts that modify content and/or data files. Because
4+
writers are updating content all the time, scripts in here require more
5+
cross-team coordination and planning before they are run. Make sure to consider
6+
whether a script added here also needs to be run on translation files or if we
7+
can wait for the changes to come in organically via Crowdin.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const walk = require('walk-sync')
6+
const frontmatter = require('@github-docs/frontmatter')
7+
const loadPages = require('../../lib/pages')
8+
const patterns = require('../../lib/patterns')
9+
const loadRedirects = require('../../lib/redirects/precompile')
10+
const allVersions = Object.keys(require('../../lib/all-versions'))
11+
12+
// get all content and data files
13+
const files = ['content', 'data'].map(dir => {
14+
return walk(path.join(process.cwd(), dir), { includeBasePath: true, directories: false })
15+
.filter(file => file.endsWith('.md') && !file.endsWith('README.md'))
16+
}).flat()
17+
18+
// match [foo](/v3) and [bar](/v4) Markdown links
19+
const linkRegex = new RegExp('\\(/v[34].*?\\)', 'g')
20+
21+
main()
22+
23+
async function main () {
24+
// we need to load the pages so we can get the redirects
25+
const englishPages = (await loadPages()).filter(p => p.languageCode === 'en')
26+
const redirects = await loadRedirects(englishPages)
27+
28+
for (const file of files) {
29+
const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
30+
31+
const links = content.match(linkRegex)
32+
if (!links) continue
33+
34+
// remove parentheses: (/v3) -> /v3
35+
// also remove trailing slash before closing parens if there is one
36+
const devLinks = links
37+
.map(link => link.replace('(', '').replace(/\/?\)/, ''))
38+
39+
let newContent = content
40+
41+
for (const devLink of devLinks) {
42+
const [link, fragment] = devLink.split(/\/?#/)
43+
44+
let redirect = redirects[link]
45+
46+
if (!redirect) {
47+
console.log(`no redirect found for ${devLink} in ${file}`)
48+
continue
49+
}
50+
51+
// do some cleanup
52+
redirect = redirect
53+
// remove language code segment
54+
.replace(patterns.getLanguageCode, '')
55+
// remove version segment
56+
.replace(new RegExp(`/(${allVersions.join('|')})`), '')
57+
58+
// re-add the fragment
59+
const newLink = fragment
60+
? redirect + '#' + fragment
61+
: redirect
62+
63+
// first remove any trailing slashes from the old link,
64+
// then replace with the new link
65+
newContent = newContent
66+
.replace(`${devLink}/`, devLink)
67+
.replace(devLink, newLink)
68+
}
69+
70+
fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
71+
}
72+
console.log('Done!')
73+
}

0 commit comments

Comments
 (0)