Browse Source

added frontmatter parsing

main
zen 1 year ago
parent
commit
177d9dcc4f
  1. 32
      index.js

32
index.js

@ -1,4 +1,5 @@
const express = require('express'); const express = require('express');
const matter = require('gray-matter');
const router = express.Router(); const router = express.Router();
const { exec } = require('child_process'); const { exec } = require('child_process');
@ -59,7 +60,36 @@ router.get('/viewPost', (req, res) => {
console.log(`stderr: ${stderr}`); console.log(`stderr: ${stderr}`);
return; return;
} }
res.send(stdout.split('\n').filter(Boolean)); let post = matter(stdout)
let { title, author, timestamp } = post.data
let html=""
if ( title ) {
html += `<h1>${title}</h1>`
}
if ( author && timestamp ) {
let date = new Date(parseInt(timestamp)).toLocaleString()
html += `<header>Created by ${author} on ${date}</header>`
}
html += '<hr/>'
html += `<p>${post.content}</p>`
res.send(html);
});
});
router.get('/api/viewPost', (req, res) => {
let { filename } = req.query;
exec(`./routes/viewPost --filename=${filename}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
res.json(matter(stdout));
}); });
}); });

Loading…
Cancel
Save