UNPKG

1.28 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const walk = require('walk');
4const yamlFront = require('yaml-front-matter')
5
6
7export default 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 // console.log(file);
19 if (ext === config.ext) {
20 const input = fs.readFileSync(file, 'utf-8');
21 const yaml = yamlFront.loadFront(input, 'content');
22 // console.log(yaml);
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 // const res = md(results.content);
30 // console.log(res);
31 }
32
33 next();
34 });
35
36
37 walker.on('end', () => {
38
39 // console.log('end');
40 console.log(mdData);
41 if (callback) callback(mdData);
42 });
43
44 walker.on('error', (e) => {
45 console.log(e);
46 });
47}
\No newline at end of file