forked from piotrwitek/react-redux-typescript-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-readme.js
50 lines (41 loc) · 1.15 KB
/
generate-readme.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
const fs = require('fs');
const ROOT_PATH = `${__dirname}/`;
const inputFiles = [ROOT_PATH + 'README_SOURCE-zh.md'];
const outputFile = ROOT_PATH + 'README.md';
// const outputFile = ROOT_PATH + 'README.zh.md';
const result = inputFiles
.map(filePath => fs.readFileSync(filePath, 'utf8'))
.map(injectCodeBlocks)
.map(injectExpanders);
fs.writeFileSync(outputFile, result, 'utf8');
function injectCodeBlocks(text) {
const regex = /::codeblock='(.+?)'::/g;
return text.replace(regex, createMatchReplacer(withSourceWrapper));
}
function injectExpanders(text) {
const regex = /::expander='(.+?)'::/g;
return text.replace(regex, createMatchReplacer(withDetailsWrapper));
}
function createMatchReplacer(wrapper) {
return (match, filePath) => {
console.log(ROOT_PATH + filePath);
const text = fs.readFileSync(ROOT_PATH + filePath, 'utf8');
return wrapper(text);
};
}
function withSourceWrapper(text) {
return `
${'```tsx'}
${text}
${'```'}
`.trim();
}
function withDetailsWrapper(text) {
return `
<details><summary><i>Click to expand</i></summary><p>
${'```tsx'}
${text}
${'```'}
</p></details>
`.trim();
}