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

Function main

script/reconcile-category-dirs-with-ids.js:37–138  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

35main()
36
37async function main() {
38 const englishCategoryIndices = getEnglishCategoryIndices()
39
40 for (const categoryIndex of englishCategoryIndices) {
41 const contents = fs.readFileSync(categoryIndex, 'utf8')
42 const { data, content } = frontmatter(contents)
43
44 // Get the parent directory name
45 const categoryDirPath = path.dirname(categoryIndex)
46 const categoryDirName = path.basename(categoryDirPath)
47
48 const title = await renderContent(data.title, {}, { textOnly: true })
49 slugger.reset()
50 const expectedSlug = slugger.slug(decode(title))
51
52 // If the directory name already matches the expected slug, bail out now
53 if (categoryDirName === expectedSlug) continue
54
55 // Figure out the new path for the category
56 const categoryDirParentDir = path.dirname(categoryDirPath)
57 const newPath = path.join(categoryDirParentDir, expectedSlug)
58
59 // Figure out redirect path
60 const relativeOldPath = path.relative(contentDir, categoryDirPath)
61 const redirectPath = '/' + slash(relativeOldPath)
62
63 // Log it
64 const relativeNewPath = path.relative(contentDir, newPath)
65 console.log(`Renaming category directory:
66Old: "${relativeOldPath}"
67New: "${relativeNewPath}"
68Redirect: "${redirectPath}"
69`)
70
71 // Add a new redirect to the frontmatter
72 if (!data.redirect_from) {
73 data.redirect_from = []
74 }
75 data.redirect_from.push(redirectPath)
76
77 // Update the category index file on disk
78 fs.writeFileSync(categoryIndex, frontmatter.stringify(content, data, { lineWidth: 10000 }))
79
80 // Update all of the category's articles on disk as well to add a new redirect to their frontmatter
81 for (const articleFileName of fs.readdirSync(categoryDirPath)) {
82 const articlePath = path.join(categoryDirPath, articleFileName)
83
84 // Figure out redirect path
85 const articlePathMinusExtension = path.join(
86 categoryDirPath,
87 path.basename(articleFileName, '.md')
88 )
89 const redirectArticlePath = '/' + slash(path.relative(contentDir, articlePathMinusExtension))
90
91 // Log it
92 const relativeOldArticlePath = path.relative(contentDir, articlePath)
93 const newArticlePath = path.join(categoryDirParentDir, expectedSlug, articleFileName)
94 const relativeNewArticlePath = path.relative(contentDir, newArticlePath)

Calls 3

frontmatterFunction · 0.85
renderContentFunction · 0.85

Tested by

no test coverage detected