Skip to content

Commit c451f32

Browse files
authored
Script to update ol/ul indents from 2 to 4 spaces (github#20384)
* Script to update ol/ul indents from 2 to 4 spaces * Update commonmark-indent.js * Update commonmark-indent.js
1 parent 6c5b005 commit c451f32

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
5+
As we update our markdown rendering pipeline,
6+
the package update includes a new markdown rendering engine,
7+
micromark, that is CommonMark compliant. The most obvious change is that
8+
indents within `ul` and `ol` now require 4 spaces or a tab instead of
9+
2 spaces. This script updates our content to that specification.
10+
*/
11+
12+
import walkSync from 'walk-sync'
13+
import fs from 'fs/promises'
14+
15+
/******************************************************************************/
16+
17+
async function readFiles(opts) {
18+
const paths = walkSync('./', {
19+
directories: false,
20+
includeBasePath: true,
21+
...opts,
22+
})
23+
return await Promise.all(paths.map(async (path) => [path, await fs.readFile(path, 'utf8')]))
24+
}
25+
26+
function withAllFiles(files, fn) {
27+
return files.map(([path, file]) => [path, fn(path, file)])
28+
}
29+
30+
async function writeFiles(files) {
31+
return await Promise.all(files.map(async ([path, file]) => await fs.writeFile(path, file)))
32+
}
33+
34+
/******************************************************************************/
35+
36+
function updateIndent(path, file) {
37+
// We don't want to change the frontmatter
38+
let [, frontmatter, content] = file.split(/^-{3,}$/gm)
39+
40+
// If the file doesn't have frontmatter or content, meh
41+
if (!frontmatter || !content) return file
42+
43+
// If there no `ul` or `ol`, no change
44+
if (!/^([-*]\s)|(\d+[.)]\s)/gi.test(content)) return file
45+
46+
// If this file has a yaml example, skip it; its too hard to update automatically
47+
if (/^\s*`{3}ya?ml/gim.test(content)) {
48+
if (/^ {2,3}(\S)/gm.test(content)) {
49+
console.log('YAML example, will skip', path)
50+
}
51+
return file
52+
}
53+
54+
// Only within a `ul` or `ol`, update indent level
55+
content = content
56+
.split('\n')
57+
.map((line, index, arr) => {
58+
// If this isn't an indented line, continue
59+
if (!/^ {2,3}(\S)/.test(line)) return line
60+
61+
// Find the previous line that is not whitespace or indented
62+
let prevLineIndex = index - 1
63+
while (/^\s*$/.test(arr[prevLineIndex]) || /^\s{2,}/.test(arr[prevLineIndex])) {
64+
prevLineIndex--
65+
}
66+
67+
// Only do it if previous line is ol/ul
68+
const prevLine = arr[prevLineIndex]
69+
if (!/^([-*]\s)|(\d+[.)]\s)/i.test(prevLine)) return line
70+
71+
return line.replace(/^ {2,3}(\S)/, ' $1')
72+
})
73+
.join('\n')
74+
75+
return ['---', frontmatter.trim(), '---', '', content.trim(), ''].join('\n')
76+
}
77+
78+
async function main() {
79+
let files = await readFiles({
80+
globs: ['content/**/*.md', 'data/**/*.md'],
81+
ignore: ['**/README.md'],
82+
})
83+
files = withAllFiles(files, updateIndent)
84+
await writeFiles(files)
85+
}
86+
87+
main()

0 commit comments

Comments
 (0)