forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile-category-dirs-with-ids.js
executable file
·100 lines (80 loc) · 2.93 KB
/
reconcile-category-dirs-with-ids.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
// [start-readme]
//
// This script will say which category pages needs to be renamed
// so they match their respective titles (from the front matter)
//
// [end-readme]
import fs from 'fs'
import path from 'path'
import assert from 'node:assert/strict'
import walk from 'walk-sync'
import chalk from 'chalk'
import GithubSlugger from 'github-slugger'
import { decode } from 'html-entities'
import frontmatter from '../lib/read-frontmatter.js'
import { renderContent } from '#src/content-render/index.js'
import { allVersions } from '#src/versions/lib/all-versions.js'
import { ROOT } from '../lib/constants.js'
const slugger = new GithubSlugger()
const contentDir = path.join(ROOT, 'content')
main()
async function main() {
const englishCategoryIndices = getEnglishCategoryIndices()
const shouldRename = []
for (const categoryIndex of englishCategoryIndices) {
const contents = fs.readFileSync(categoryIndex, 'utf8')
const { data } = frontmatter(contents)
const categoryDirPath = path.dirname(categoryIndex)
const categoryDirName = path.basename(categoryDirPath)
const currentVersionObj = allVersions['free-pro-team@latest']
assert(currentVersionObj, "No current version found for 'free-pro-team@latest'")
const context = {
currentLanguage: 'en',
currentVersionObj,
}
const title = await renderContent(data.title, context, { textOnly: true })
slugger.reset()
const expectedSlug = slugger.slug(decode(title))
// If the directory name already matches the expected slug, bail out now
if (categoryDirName === expectedSlug) continue
if (data.allowTitleToDifferFromFilename) {
continue
}
// Figure out the new path for the category
const categoryDirParentDir = path.dirname(categoryDirPath)
const newPath = path.join(categoryDirParentDir, expectedSlug)
const oldRelativePath = path.relative(ROOT, categoryDirPath)
const newRelativePath = path.relative(ROOT, newPath)
shouldRename.push({ oldRelativePath, newRelativePath })
}
if (shouldRename.length > 0) {
console.log(
chalk.yellow(
`${shouldRename.length} ${
shouldRename.length === 1 ? 'category' : 'categories'
} need to be renamed because their title doesn't match their directory name.`,
),
)
console.log(chalk.dim('Run the following commands to rename them:'))
for (const { oldRelativePath, newRelativePath } of shouldRename) {
console.log(`./script/move-content.js ${oldRelativePath} ${newRelativePath}`)
}
} else {
console.log(chalk.green('No categories need to be renamed! 🎉'))
}
}
function getEnglishCategoryIndices() {
const walkOptions = {
globs: ['*/*/**/index.md'],
ignore: [
'{rest,graphql,developers}/**',
'enterprise/admin/index.md',
'**/articles/**',
'**/early-access/**',
],
directories: false,
includeBasePath: true,
}
return walk(contentDir, walkOptions)
}