UNPKG

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