UNPKG

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