-
Notifications
You must be signed in to change notification settings - Fork 15
/
sync-partials.js
37 lines (32 loc) · 1.19 KB
/
sync-partials.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
import glob from "glob";
import fs from "fs";
// use this comment syntax to include markdown partials between the include and end tags
// <!-- INCLUDE path-to-partial.md -->
// <!-- END -->
const includeRegex = /(\<\!\-\- INCLUDE (.*) -->)(.|\n)*?(<!-- END -->)/g;
const warning =
"<!-- WARNING: copied from the included file path. Do not edit directly. -->";
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_the_replacement
function replacer(path) {
return (_, includeTag, file, __, endTag) => {
console.log(`${path} << ${file}`)
const body = fs.readFileSync(file, "utf8");
let content;
if (file.endsWith(".md")) {
content = body;
} else if (file.endsWith(".sol")) {
content = `\`\`\`solidity\n${body}\n\`\`\``;
}
return [includeTag, warning, content, warning, endTag].join("\n");
};
}
function modifyFile(path) {
let file = fs.readFileSync(path, "utf8");
file = file.replace(includeRegex, replacer(path));
fs.writeFileSync(path, file);
}
glob("**/*.md", {}, (_, paths) =>
paths
.filter((path) => !path.startsWith("node_modules"))
.forEach((path) => modifyFile(path))
);