-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir-parse.js
60 lines (55 loc) · 1.53 KB
/
dir-parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// https://code-maven.com/list-content-of-directory-with-nodejs
// https://github.com/jxson/front-matter <- consider for yaml front matter parsing
const fs = require('fs');
const fm = require('front-matter');
const path = require('path');
const jsonfile = require('jsonfile');
const postsjson = 'src/assets/_posts/posts.json';
const dir = 'src/assets/_posts';
const extFilter = 'md';
const pathSupplied = dir;
function extension(element) {
const extName = path.extname(element);
return extName === '.' + extFilter;
}
/**
* cycle through directory for files
*/
fs.readdir(pathSupplied, function (err, items) {
let opener = '{ "entries": ',
closer = ' }',
posts = [],
file = '',
fileContent = '',
content = '',
body = '',
attributes = {},
title = '',
description = '';
/**
* cycle over items, filtering for files matching extension
* @type {Array}
*/
posts = items.filter(extension).map((item) => {
file = pathSupplied + '/' + item;
fileContent = fs.readFileSync(file, 'utf8');
content = fm(fileContent);
console.log('*** content ***\n', content);
body = content.body;
attributes = content.attributes;
title = attributes.title;
description = attributes.description;
return {
post: content
};
// return {
// file: file,
// title: title,
// description: description,
// body: body
// };
});
opener += JSON.stringify(posts);
opener += closer;
fs.writeFile(postsjson, opener);
});