-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-info.js
More file actions
92 lines (71 loc) · 2.65 KB
/
Copy pathpost-info.js
File metadata and controls
92 lines (71 loc) · 2.65 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import fs from 'fs';
import fm from 'front-matter';
import markdown from 'markdown-it';
import hljs from 'highlight.js';
import chalk from 'chalk';
import { writeFile } from './utils';
//parse markdown to add highlight classes and reformat for line numbers
const md = new markdown({
highlight: function (str) {
//create line numbers for code block
let lineNumber = 0;
//pass in string to be highlighted
const hl = hljs.highlightAuto(str, ['html', 'javascript', 'css', 'sass', 'shell']).value;
const commentPattern = /<span class="hljs-comment">(.|\n)*?<\/span>/g;
const adaptedHighlightedContent = hl.replace(commentPattern, data => {
return data.replace(/\r?\n/g, () => {
return '\n<span class="hljs-comment">';
});
});
const contentBlock = adaptedHighlightedContent.split(/\r?\n/).map(lineContent => {
return `<div class="code-line">
<span class="code-line-number" data-pseudo-content=${++lineNumber}></span>
<span class="code-line-content">${lineContent}</span>
</div>`
}).join('');
return `<pre class="code"><code class="code-block">${contentBlock}</code></pre>`;
}
});
//get directory post
fs.readdir(`./src/post/article`, (err, posts) => {
//if error in getting
if (err) { throw err; }
//create array to store list of post
let postList = {};
//create an array to store the tags which will make up our nav
let nav = [];
//map through each post and pull out the data
posts.map(post => {
const fileName = post;
//get yaml front matter from each post file
fs.readFile(`./src/post/article/${fileName}`, 'utf8', (err, data) => {
//if error in getting
if (err) { throw err; }
//pull yaml front matter
const content = fm(data);
const postData = content.attributes;
//parse markdown and store the html
postData.body = md.render(content.body);
//get file name and remove extension
postData.filename = fileName.replace(/\.[^/.]+$/, "");
//push new post object onto list array
postList[fileName] = postData;
//add post tags array to nav array to get filtered (no dupes)
nav = nav.concat(postData.tags).reduce((a, b) => {
if (a.indexOf(b) < 0 ) { a.push(b); }
return a;
},[]);
//create json file with post data.
writeFile(
'tmp/data/post.json',
JSON.stringify(postList, null, 2)
);
//create json file with post data.
writeFile(
'docs/static/data/post.json',
JSON.stringify(postList, null, 2),
chalk.red.bold(`SAVED TO POST.JSON: ${postData.filename}`)
);
});
});
});