mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-11 02:12:53 +00:00
added markdown parsing - working on converting html posts
This commit is contained in:
54
metadata.js
Normal file
54
metadata.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import fs from 'fs';
|
||||
import marked from 'marked';
|
||||
import highlight from 'highlight.js';
|
||||
|
||||
marked.setOptions({
|
||||
header: true,
|
||||
highlight: (code) => {
|
||||
return highlight.highlightAuto(code).value;
|
||||
}
|
||||
});
|
||||
|
||||
const dir = './posts/';
|
||||
const json = {
|
||||
posts: []
|
||||
};
|
||||
|
||||
//do everything synchronously to keep posts ordered
|
||||
function parse_dir(dir, folder_name){
|
||||
const posts = fs.readdirSync(dir);
|
||||
|
||||
for(let post of posts){
|
||||
const stats = fs.statSync(dir + post);
|
||||
|
||||
if(stats.isDirectory()){
|
||||
parse_dir(dir + post + '/', post);
|
||||
} else {
|
||||
const file = fs.readFileSync(dir+post, 'utf8');
|
||||
const tokens = marked.lexer(file, null);
|
||||
const path = dir + post;
|
||||
const temp = {
|
||||
path: path.slice(1, path.length),
|
||||
category: folder_name,
|
||||
date: post.slice(0, 10),
|
||||
title: marked('<h2>' + tokens[0].text + '</h2>'),
|
||||
intro: marked(tokens[1].text)
|
||||
}
|
||||
console.log(marked(file));
|
||||
json.posts.push(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parse_dir(dir, 'posts');
|
||||
|
||||
//sort posts by date
|
||||
json.posts.sort((a, b) => {
|
||||
return new Date(b.date) - new Date(a.date);
|
||||
});
|
||||
|
||||
//output to public path
|
||||
fs.writeFile('./dist/metadata.json', JSON.stringify(json,null,4), (err) => {
|
||||
if (err) throw err;
|
||||
console.log("Saved metadata.json");
|
||||
})
|
||||
Reference in New Issue
Block a user