forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile-filenames-with-ids.js
executable file
·78 lines (62 loc) · 2.72 KB
/
reconcile-filenames-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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const walk = require('walk-sync')
const slugger = new (require('github-slugger'))()
const entities = new (require('html-entities').XmlEntities)()
const frontmatter = require('@github-docs/frontmatter')
const { execSync } = require('child_process')
const addRedirectToFrontmatter = require('../lib/redirects/add-redirect-to-frontmatter')
const contentDir = path.join(process.cwd(), 'content')
const contentFiles = walk(contentDir, { includeBasePath: true, directories: false })
.filter(file => {
return file.endsWith('.md') &&
!file.endsWith('index.md') &&
!file.includes('README')
})
// [start-readme]
//
// An automated test checks for discrepancies between filenames and [autogenerated heading IDs](https://www.npmjs.com/package/remark-autolink-headings).
// If the test fails, a human needs to run this script to update the filenames.
//
// **This script is not currently supported on Windows.**
//
// [end-readme]
// TODO fix path separators in the redirect
if (process.platform.startsWith('win')) {
console.log('This script cannot be run on Windows at this time! Exiting...')
process.exit()
}
contentFiles.forEach(oldFullPath => {
const { data, content } = frontmatter(fs.readFileSync(oldFullPath, 'utf8'))
// skip pages with frontmatter flag
if (data.allowTitleToDifferFromFilename) return
// slugify the title of each article
// where title = Foo bar
// and slug = foo-bar
slugger.reset()
const slug = slugger.slug(entities.decode(data.title))
// get the basename of each file
// where file = content/foo-bar.md
// and basename = foo-bar
const basename = path.basename(oldFullPath, '.md')
// if slug and basename match, return early
if (basename === slug) return
// otherwise rename the file using the slug
const newFullPath = oldFullPath.replace(basename, slug)
const oldContentPath = path.relative(process.cwd(), oldFullPath)
const newContentPath = path.relative(process.cwd(), newFullPath)
const gitStatusOfFile = execSync(`git status --porcelain ${oldContentPath}`).toString()
// if file is untracked, do a regular mv; otherwise do a git mv
if (gitStatusOfFile.includes('??')) {
execSync(`mv ${oldContentPath} ${newContentPath}`)
} else {
execSync(`git mv ${oldContentPath} ${newContentPath}`)
}
// then add the old path to the redirect_from frontmatter
// TODO fix path separators on Windows (e.g. \github\extending-github\about-webhooks)
const redirect = path.join('/', path.relative(contentDir, oldFullPath).replace(/.md$/, ''))
data.redirect_from = addRedirectToFrontmatter(data.redirect_from, redirect)
// update the file
fs.writeFileSync(newFullPath, frontmatter.stringify(content, data))
})