Skip to content

Instantly share code, notes, and snippets.

View kartiknair's full-sized avatar

Kartik Nair kartiknair

View GitHub Profile
mkdir node-ssg && cd node-ssg
npm init -y
{
"name": "planar",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "node ./src/index.js"
},
"keywords": [],
"author": "",
const config = {
dev: {
postsdir: "./content",
outdir: "./public"
}
};
module.exports = config;
const config = require("./config");
const posts = fs
.readdirSync(config.dev.postsdir)
.map(post => post.slice(0, -3));
console.log(posts);
title date description
Post One
2020–02–28T22:19:00Z
My reasons for starting a blog.

This is an amazing blog post.

Really it's just great

const config = require("./config");
const fm = require("front-matter");
const marked = require("marked");
const createPost = postPath => {
const data = fs.readFileSync(`${config.dev.postsdir}/${postPath}.md`, "utf8");
const content = fm(data);
content.body = marked(content.body);
content.path = postPath;
return content;
const config = require("./config");
const createPost = require("./posts.js");
const posts = fs
.readdirSync(config.dev.postsdir)
.map(post => post.slice(0, -3))
.map(post => postMethods.createPost(post));
console.log(posts);
const marked = require("marked");
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function(code, language) {
const hljs = require("highlight.js");
const validLanguage = hljs.getLanguage(language) ? language : "plaintext";
return hljs.highlight(validLanguage, code).value;
},
pedantic: false,
const posthtml = data => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="${data.attributes.description}" />
<title>${data.attributes.title}</title>
</head>
<body>
const createPosts = posts => {
posts.forEach(post => {
if (!fs.existsSync(`${config.dev.outdir}/${post.path}`))
fs.mkdirSync(`${config.dev.outdir}/${post.path}`);
fs.writeFile(
`${config.dev.outdir}/${post.path}/index.html`,
posthtml(post),
e => {
if (e) throw e;