Skip to content

Instantly share code, notes, and snippets.

@crhallberg
Last active February 12, 2018 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crhallberg/cbe8bb74950332628419ed9fb3dbdebe to your computer and use it in GitHub Desktop.
Save crhallberg/cbe8bb74950332628419ed9fb3dbdebe to your computer and use it in GitHub Desktop.
Publish markdown to html, ignores files that start with _ as drafts.
const marky = require('marky-markdown'),
touch = require('touch'),
fs = require('fs');
const template = fs.readFileSync('./template.html', 'utf8');
function reportCard(name, mtime) {
const fileCol = name + Array(33).join(' ').slice(name.length);
const date = mtime.toDateString(); // mtime.getFullYear() + '-' + mtime.getMonth() + '-' + mtime.getDate();
console.log(' $ ' + fileCol + date);
}
// Read all files from current directory
fs.readdir('.', function(err, files) {
for (let i = 0; i < files.length; i++) {
// underscore for drafts || only markdown files please
if (files[i].charAt(0) === '_' || !files[i].match('\.md$')) {
console.log(' x ' + files[i]);
continue;
}
console.log(' + ' + files[i]);
let md = fs.readFile(files[i], 'utf8', function(err, data) {
fs.writeFile(
'./output/' + files[i].replace('.md', '.html'),
template.replace('{{ content }}', marky(data, { prefixHeadingIds: false })),
'utf8',
function (err, data) {
// Set mod date of html to the mod date of the source
const stats = fs.statSync(files[i]);
touch.sync(
'./output/' + files[i].replace('.md', '.html'),
{ time: stats.mtime }
);
// Hang report card on fridge
reportCard(files[i], stats.mtime);
}
);
});
}
console.log('---');
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<nav><a href="/">&larr; Home</a> - <a href="/blog">Index</a></nav>
{{ content }}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment