Skip to content

Commit 39f7a0c

Browse files
authored
Merge pull request ember-learn#589 from ember-learn/extract-code-script
add script for extracting code blocks
2 parents b49ef02 + b9faa11 commit 39f7a0c

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525

2626
# Deployment credentials
2727
config/credentials.json
28+
29+
/code

extractCode.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-env node */
2+
/* eslint no-console:0 */
3+
4+
const walkSync = require('walk-sync');
5+
const _ = require('lodash');
6+
const codeBlocks = require('gfm-code-blocks');
7+
const { extname, join, dirname } = require('path');
8+
const {
9+
readFileSync,
10+
writeFileSync,
11+
mkdirSync,
12+
existsSync,
13+
} = require('fs');
14+
15+
// these regexs are used against the lang definition of a code block to identify
16+
// the language. If you only want to check certain types then you should change
17+
// this line
18+
const templatesMatch = /^(handlebars|html|text|javascript|bash|sh|css|hbs|json|apacheconf)/;
19+
20+
const mdFiles = _.chain(walkSync('guides/release'))
21+
.filter(path => extname(path) === '.md')
22+
.value();
23+
24+
mdFiles.forEach((filename) => {
25+
const source = readFileSync(join(__dirname, 'guides', 'release', filename), 'utf-8');
26+
// console.log(filename);
27+
const blocks = codeBlocks(source);
28+
29+
const codeOnly = blocks.reduce((prev, current) => {
30+
if (!current.lang) {
31+
console.error(`Missing lang in code block on ${filename}`);
32+
}
33+
34+
if (current.lang.match(templatesMatch)) {
35+
return `${prev}\n\n${current.block}`;
36+
}
37+
38+
console.log(`Ignoring language ${current.lang}`);
39+
return prev;
40+
}, '');
41+
42+
if (codeOnly) {
43+
const newFilename = join(__dirname, 'code', filename);
44+
45+
if (!existsSync(dirname(newFilename))) {
46+
mkdirSync(dirname(newFilename), {
47+
recursive: true,
48+
});
49+
}
50+
51+
writeFileSync(join(__dirname, 'code', filename), codeOnly);
52+
}
53+
});

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"eslint-config-airbnb-base": "^13.0.0",
6969
"eslint-plugin-ember": "^6.3.0",
7070
"eslint-plugin-import": "^2.16.0",
71+
"gfm-code-blocks": "^1.0.0",
7172
"guidemaker": "^1.3.0",
7273
"guidemaker-ember-template": "^1.1.0",
7374
"loader.js": "^4.7.0",

0 commit comments

Comments
 (0)