generated from Adiltonzs/docs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reconcile-filenames-with-ids.js
executable file
·79 lines (62 loc) · 2.68 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
79
#!/usr/bin/env node
// [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]
import fs from 'fs'
import path from 'path'
import walk from 'walk-sync'
import GithubSlugger from 'github-slugger'
import { decode } from 'html-entities'
import frontmatter from '../lib/read-frontmatter.js'
import { execSync } from 'child_process'
import addRedirectToFrontmatter from './helpers/add-redirect-to-frontmatter.js'
const slugger = new GithubSlugger()
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')
}
)
// 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(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))
})