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 | module.exports = function walkMD(config, callback) {
|
8 | const mdData = {};
|
9 |
|
10 |
|
11 | const walker = walk.walk(config.root);
|
12 | walker.on('file', function (root, fileStats, next) {
|
13 | const name = fileStats.name;
|
14 | const ext = path.extname(name);
|
15 | const basename = path.basename(name, config.ext);
|
16 |
|
17 | const file = path.resolve('', root + '/' + name);
|
18 |
|
19 | if (ext === config.ext) {
|
20 | const input = fs.readFileSync(file, 'utf-8');
|
21 | const yaml = yamlFront.loadFront(input, 'content');
|
22 |
|
23 | const title = yaml.title;
|
24 | const rank = yaml.rank;
|
25 |
|
26 | if (!mdData[title]) mdData[title] = {};
|
27 | if (!mdData[title].files) mdData[title].files = [];
|
28 | mdData[title].files[rank] = file;
|
29 | }
|
30 |
|
31 | next();
|
32 | });
|
33 |
|
34 |
|
35 | walker.on('end', () => {
|
36 | console.log(mdData);
|
37 | fs.writeFileSync('__md__.json', mdData);
|
38 | if (callback) callback(mdData);
|
39 | });
|
40 |
|
41 | walker.on('error', (e) => {
|
42 | console.log(e);
|
43 | });
|
44 | } |
\ | No newline at end of file |