1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const walk = require('walk');
|
4 | const yamlFront = require('yaml-front-matter')
|
5 |
|
6 |
|
7 | function verifyExist(arr, key) {
|
8 | arr.every(item => {
|
9 |
|
10 | });
|
11 | }
|
12 |
|
13 |
|
14 | module.exports = function walkMD(config, callback) {
|
15 | const mdData = {};
|
16 |
|
17 | const walker = walk.walk(config.root);
|
18 | walker.on('file', function (root, fileStats, next) {
|
19 | const name = fileStats.name;
|
20 | const ext = path.extname(name);
|
21 | const basename = path.basename(name, config.ext);
|
22 |
|
23 | const file = path.resolve('', root + '/' + name);
|
24 |
|
25 | if (ext === config.ext) {
|
26 | const input = fs.readFileSync(file, 'utf-8');
|
27 | const yaml = yamlFront.loadFront(input, 'content');
|
28 |
|
29 | const title = yaml.title;
|
30 | const rank = yaml.rank;
|
31 | const category = yaml.category;
|
32 |
|
33 | if (!mdData[category]) mdData[category] = [];
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | if (!mdData[title]) mdData[title] = {};
|
39 | if (!mdData[title].files) mdData[title].files = [];
|
40 | mdData[title].files[rank] = `require(${file})`;
|
41 | }
|
42 |
|
43 | next();
|
44 | });
|
45 |
|
46 |
|
47 | walker.on('end', () => {
|
48 | console.log(mdData);
|
49 | fs.writeFileSync(__dirname + '/web/__md__.json', JSON.stringify(mdData));
|
50 | if (callback) callback(mdData);
|
51 | });
|
52 |
|
53 | walker.on('error', (e) => {
|
54 | console.log(e);
|
55 | });
|
56 | } |
\ | No newline at end of file |