-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
298 lines (273 loc) · 7.93 KB
/
cli.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env node
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const packageJson = require('./package.json')
import fs from 'fs'
import { program } from 'commander'
import TurndownService from 'turndown'
import { mkdirp } from 'mkdirp'
import { deleteAsync } from 'del'
import WPAPI from 'wpapi'
import { tables } from 'turndown-plugin-gfm'
// Languages that can be specified in the code markdown
const codeLanguages = {
css: 'css',
bash: 'bash',
php: 'php',
yaml: 'yaml',
xml: 'xml',
jscript: 'javascript',
}
// Rules that remove escapes in code blocks
const unEscapes = [
[/\\\\/g, '\\'],
[/\\\*/g, '*'],
[/\\-/g, '-'],
[/^\\+ /g, '+ '],
[/\\=/g, '='],
[/\\`/g, '`'],
[/\\~~~/g, '~~~'],
[/\\\[/g, '['],
[/\\\]/g, ']'],
[/\\>/g, '>'],
[/\\_/g, '_'],
[/\"/g, '"'],
[/\</g, '<'],
[/\>/g, '>'],
]
const turndownService = new TurndownService({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
emDelimiter: '*',
})
turndownService.use(tables)
// Remove Glossary
turndownService.addRule('glossary', {
filter: (node) => {
const classList = node.getAttribute('class')
if (classList) {
return classList === 'glossary-item-hidden-content'
}
return false
},
replacement: () => {
return ''
},
})
// Remove code trigger anchor
turndownService.addRule('code-trigger-anchor', {
filter: (node) => {
const classList = node.getAttribute('class')
if (classList) {
return (
classList.includes('show-complete-source') ||
classList.includes(`less-complete-source`)
)
}
return false
},
replacement: () => {
return ''
},
})
// Transform dt tag to strong tag
turndownService.addRule('dt-to-strong', {
filter: ['dt'],
replacement: (content, node, options) => {
return options.strongDelimiter + content + options.strongDelimiter
},
})
// Transform pre code block to code markdown
turndownService.addRule('precode to code', {
filter: (node) => {
const classList = node.getAttribute('class')
return node.nodeName === 'PRE' && classList && classList.includes('brush:')
},
replacement: (content, node) => {
const classList = node.getAttribute('class')
// Search for a language that matches the list of code languages
const codeLanguage = Object.keys(codeLanguages).reduce(
(currentLanguage, language) => {
if (classList.includes(language)) {
return codeLanguages[language]
}
return currentLanguage
},
undefined,
)
// Unescape contents
let newContent = unEscapes.reduce((accumulator, unEscape) => {
return accumulator.replace(unEscape[0], unEscape[1])
}, content)
// Remove br tag
newContent = newContent.replace(/^<br \/>\n\n|<br \/>\n/g, '\n')
// Remove first and last paragraph tag
newContent = newContent.replace(/^<\/p>|<p>$/g, '')
// Remove first new line
newContent = newContent.replace(/^\n/, '')
// Convert to language-aware markdown
newContent = '```' + (codeLanguage ?? '') + '\n' + newContent + '```'
return newContent
},
})
const getAll = (request) => {
return request.then((response) => {
if (!response._paging || !response._paging.next) {
return response
}
// Request the next page and return both responses as one collection
return Promise.all([response, getAll(response._paging.next)]).then(
(responses) => responses.flat(),
)
})
}
const generateJson = async (
team,
handbook,
subdomain,
outputDir,
regenerate,
) => {
team = team ? `${team}/` : ''
handbook = handbook ? handbook : 'handbook'
subdomain = `${
subdomain ? (subdomain === 'w.org' ? '' : subdomain) : 'make'
}.`
outputDir = outputDir ? outputDir.replace(/\/$/, '') + '/' : 'en/'
if (regenerate) {
// Remove the output directory first if -r option is set.
await deleteAsync([`${outputDir}`]).catch(() => {})
}
await mkdirp(`${outputDir}/`)
.then((made) => {
if (made) {
console.log(`Created directory ${made}`)
}
})
.catch((e) => {
console.error(
'Could not create output directory. Make sure you have right permission on the directory and try again.',
)
throw e
})
const wp = new WPAPI({
endpoint: `https://${subdomain}wordpress.org/${team}wp-json`,
})
wp.handbooks = wp.registerRoute('wp/v2', `/${handbook}/(?P<id>)`)
console.log(
`Connecting to https://${subdomain}wordpress.org/${team}wp-json/wp/v2/${handbook}/`,
)
getAll(wp.handbooks()).then(async (allPosts) => {
if (allPosts.length === 0) {
console.warn('No posts found.')
process.exit(1)
}
let rootPath = ''
for (const item of allPosts) {
if (parseInt(item.parent) === 0) {
rootPath = item.link.split(item.slug)[0]
break
} else {
rootPath = `https://${subdomain}wordpress.org/${team}/${handbook}/`
}
}
for (const item of allPosts) {
const path = item.link.split(rootPath)[1].replace(/\/$/, '') || 'index'
const filePath =
path.split('/').length > 1
? path.substring(0, path.lastIndexOf('/')) + '/'
: ''
const content = item.content.rendered
const markdownContent = turndownService.turndown(content)
const markdown = `# ${item.title.rendered}\n\n${markdownContent}`
await mkdirp(`${outputDir}/${filePath}`)
.then((_) => {
try {
fs.readFile(`${outputDir}/${path}.md`, 'utf8', (err, data) => {
if (!data) {
fs.writeFile(
`${outputDir}/${path}.md`,
markdown,
{ encoding: 'utf8' },
(err) => {
if (err) {
throw err
} else {
console.log(`Created ${path}.md`)
}
},
)
} else if (data === markdown) {
console.log(
'\x1b[37m%s\x1b[0m',
`${path}.md already exists with the exact same content. Skipping...`,
)
} else {
fs.writeFile(
`${outputDir}/${path}.md`,
markdown,
{ encoding: 'utf8' },
(err) => {
if (err) {
throw err
} else {
console.log(`Updated ${path}.md`)
}
},
)
}
})
} catch (e) {
fs.writeFile(
`${outputDir}/${path}.md`,
markdown,
{ encoding: 'utf8' },
(err) => {
if (err) {
throw err
} else {
console.log(`Created ${path}.md`)
}
},
)
}
})
.catch((e) => {
console.error(
'An error occurred during saving files. Please try again.',
)
throw e
})
}
})
}
program
.version(packageJson.version)
.description('Generate a menu JSON file for WordPress.org handbook')
.option('-t, --team <team>', 'Specify team name')
.option(
'-b, --handbook <handbook>',
'Specify handbook name (default "handbook")',
)
.option(
'-s, --sub-domain <subdomain>',
'Specify subdomain, for example, "developer" for developer.w.org, "w.org" for w.org (default "make")',
)
.option(
'-o --output-dir <outputDir>',
'Specify directory to save files (default en/)',
)
.option(
'-r --regenerate',
'If this option is supplied, the directory you specified as output directory will once deleted, and it will regenerate all the files in the directory',
)
.action((options) => {
generateJson(
options.team,
options.handbook,
options.subDomain,
options.outputDir,
options.regenerate,
)
})
program.parse(process.argv)