Skip to content

Instantly share code, notes, and snippets.

@potato4d
Created July 13, 2018 16:18
Show Gist options
  • Save potato4d/c6046d13e3631786ae7f6771fd2fb810 to your computer and use it in GitHub Desktop.
Save potato4d/c6046d13e3631786ae7f6771fd2fb810 to your computer and use it in GitHub Desktop.
c94 markdown builder
const ejs = require('ejs')
const util = require('util')
const promisify = util.promisify
const inspect = util.inspect
const glob = promisify(require('glob'))
const fs = require('fs')
const chalk = require('chalk')
const moment = require('moment')
const markdownIt = require('markdown-it')
const compileMarkdown = new markdownIt({ html: true })
const DIST = `./_print_data/index.html`
const build = (arg) => compileMarkdown.render(arg)
;(async () => {
try {
if (!(await glob('package.json')).length) {
console.log(chalk.red.bold('Error: The build script needs to be executed in the project root.'))
process.exit(1)
}
const buildStart = moment()
console.log(chalk.inverse("C94 INTERNET PROGRAMMING BUILDER", "\n"))
const targets = (await glob('src/authors/**/index.md'))
console.log(chalk.bold('target files:'))
console.log(targets.join("\n"), "\n")
console.log(chalk.bold('Build processing...', "\n"))
const articles = targets.map((f) => build(fs.readFileSync(f, 'utf8')))
const html = ejs.render(fs.readFileSync('./src/index.ejs', 'utf8'), {
articles
})
fs.writeFileSync(DIST, html)
const buildTime = (+moment().diff(buildStart, 'miliseconds')) * 0.0001
console.log(chalk.green.bold('success!', `${('' + buildTime).slice(0, 5)}s`, "\n"))
console.log(`Export to`, chalk.green(DIST), "\n")
} catch (e) {
console.log(chalk.red("Error:"))
console.log(chalk.red(inspect(e)))
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment