forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatenate-docs.mjs
105 lines (87 loc) · 2.98 KB
/
concatenate-docs.mjs
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
import fs from 'fs';
import path from 'path';
import os from 'os';
import front from 'front-matter';
// root path
const rootPath = path.join('src', 'content');
const outFileName = 'printable.mdx';
console.info(
'Concatenating *.mdx files of each content directory to create chapter-wide help files to be used for printing'
);
// getDirectoryRecursive() recursively walks through all sub directories of the provided path
// concatenates the .mdx files content in each directory, sorted by their FrontMatter sort
// attribute, and creates a compound mdx file named by using the directory name,
// prefixed by an underscore and suffixed by '_all.mdx' from the concatenated content
// in the corresponding directory.
function getDirectoryRecursive(basePath) {
console.log(`Processing: ${basePath}`);
// create destination file name of compound file
const targetFilePath = path.join(basePath, outFileName);
// list current working directory
fs.readdir(basePath, function (err, fileNames) {
if (err) throw err;
let fileContents = [];
for (let file of fileNames) {
const fullPath = path.join(basePath, file);
// if the directory entry is a directory, recurse into that directory
if (fs.statSync(fullPath).isDirectory()) {
getDirectoryRecursive(fullPath);
} else if (fullPath.endsWith('.mdx')) {
// import the respective mdx file
// prefix a `W` to prevent filename like 12345.mdx
const basename = path
.basename(file, '.mdx')
.split('-')
.map((x) => x.toUpperCase())
.join('');
fileContents[fileContents.length] = {
...front(fs.readFileSync(fullPath).toString()),
body: `
import W${basename} from './${file}'
<W${basename} />
`,
};
}
}
// sort mdx files by FrontMatter 'sort' attribute (QuickSort)
for (let i = 0; i < fileContents.length - 1; ++i)
for (let j = i + 1; j < fileContents.length; ++j) {
const left = fileContents[i].attributes;
const right = fileContents[j].attributes;
if (
left.sort > right.sort ||
(left.sort === right.sort && left.title > right.title)
) {
[fileContents[i], fileContents[j]] = [
fileContents[j],
fileContents[i],
];
}
}
// write compound target file
const targetFile = fs.createWriteStream(targetFilePath);
targetFile.write(`---
title: Printable
sort: 999
contributors:
- webpack
---
`);
targetFile.on('error', (error) => {
throw error;
});
for (let file of fileContents) {
// use FrontMatter 'title' attribute as main heading of target file
targetFile.write(os.EOL + os.EOL + '# ' + file.attributes.title + os.EOL);
targetFile.write(file.body);
}
targetFile.end();
});
}
getDirectoryRecursive(rootPath);
// end message
process.on('exit', () =>
console.info(
`Successfully created "${outFileName}" files in each directory within ${rootPath}`
)
);