Created
October 21, 2015 09:29
-
-
Save madx/4ed1fd86758a837f52e2 to your computer and use it in GitHub Desktop.
Quick-n-dirty static site builder using deku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File processing | |
import path from "path" | |
import fs from "fs" | |
import fmt from "chalk" | |
import glob from "glob" | |
import mkdirp from "mkdirp" | |
// Config | |
import pkg from "./package" | |
const {config} = pkg | |
// Rendering | |
import matter from "gray-matter" | |
import MarkdownIt from "markdown-it" | |
import {renderString, deku} from "deku" | |
import * as templates from "./templates" | |
const md = new MarkdownIt(config.markdown || {}) | |
const defaultData = { | |
title: "Untitled", | |
template: "Default", | |
} | |
// Logging | |
const log = (msg) => console.log(msg) | |
const error = (msg) => console.error(fmt.red(msg)) | |
const warning = (msg) => console.warn(fmt.yellow(msg)) | |
// Process a single file | |
function processFile(fileName) { | |
const outputFileName = fileName | |
.replace(new RegExp(`^${config.content}`), config.output) | |
.replace(/\.md$/, ".html") | |
const outputDirName = path.dirname(outputFileName) | |
log(`> Processing ${fileName}`) | |
mkdirp(outputDirName) | |
fs.writeFileSync(outputFileName, render(fileName)) | |
} | |
function render(fileName) { | |
const props = matter.read(fileName) | |
props.data = Object.assign({}, defaultData, props.data) | |
props.formattedContent = md.render(props.content) | |
return renderString(deku({ | |
type: templates[props.data.template], | |
attributes: props, | |
})) | |
} | |
// Main code | |
glob(`${config.content}/**/*.md`, (err, files) => { | |
if (err) { | |
error(fmt.red("Cannot read content source")) | |
throw err | |
} | |
if (!files.length) { | |
return warning("No files to process!") | |
} | |
files.forEach(processFile) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment