UNPKG

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