-
Notifications
You must be signed in to change notification settings - Fork 5
/
extractCode.js
53 lines (43 loc) · 1.44 KB
/
extractCode.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
/* eslint-env node */
/* eslint no-console:0 */
const walkSync = require('walk-sync');
const _ = require('lodash');
const codeBlocks = require('gfm-code-blocks');
const { extname, join, dirname } = require('path');
const {
readFileSync,
writeFileSync,
mkdirSync,
existsSync,
} = require('fs');
// these regexs are used against the lang definition of a code block to identify
// the language. If you only want to check certain types then you should change
// this line
const templatesMatch = /^(handlebars|html|text|javascript|bash|sh|css|hbs|json|apacheconf)/;
const mdFiles = _.chain(walkSync('guides/release'))
.filter(path => extname(path) === '.md')
.value();
mdFiles.forEach((filename) => {
const source = readFileSync(join(__dirname, 'guides', 'release', filename), 'utf-8');
// console.log(filename);
const blocks = codeBlocks(source);
const codeOnly = blocks.reduce((prev, current) => {
if (!current.lang) {
console.error(`Missing lang in code block on ${filename}`);
}
if (current.lang.match(templatesMatch)) {
return `${prev}\n\n${current.block}`;
}
console.log(`Ignoring language ${current.lang}`);
return prev;
}, '');
if (codeOnly) {
const newFilename = join(__dirname, 'code', filename);
if (!existsSync(dirname(newFilename))) {
mkdirSync(dirname(newFilename), {
recursive: true,
});
}
writeFileSync(join(__dirname, 'code', filename), codeOnly);
}
});