Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created September 19, 2012 10:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklabs/3748798 to your computer and use it in GitHub Desktop.
Save mklabs/3748798 to your computer and use it in GitHub Desktop.
Grunt markdown task, compiling down to html, triggering reload
var path = require('path');
var hogan = require('hogan');
var marked = require('marked');
module.exports = function(grunt) {
// Basic grunt task to use along watch task, possibly the reload task
// of yeoman server. Designed to work with any given webserver, as
// long as the LiveReload extension for your favorite browser is used
// (http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions-).
//
// Example:
//
// # for the LiveReload server
// yeoman server:reload
//
// # another shell, spawn up your HTTP server (npm i serve -g)
// serve
//
// Config:
//
// markdown: {
// options: {
// // (optional) Mustache layout template to use. If undefined, default
// // to built-in layout.html template
// template: grunt.file.read('scripts/docs/layout.html'),
// // (optional) Array of stylesheets to include during the
// // generation process
// styles: [
// 'https://a248.e.akamai.net/assets.github.com/assets/github-6f96eda30ff1556bf501fd7c2915aa0157322687.css'
// ],
// // (optional) path to the directory where you'd like to compile
// // files, defaults to cwd.
// out: ''
// }
// }
//
// Watch configuration:
//
// watch: {
// files: ['*.md'],
// // reload is provided by yeoman
// tasks: 'markdown reload'
// }
//
grunt.registerTask('markdown', 'Markdown compile task', function() {
var o = this.options();
var layout = o.template;
if(!layout) {
layout = grunt.file.read(path.join(__dirname, 'layout.html'));
}
layout = hogan.compile(layout);
var changed = grunt.file.watchFiles.changed[0];
if(!changed) return;
var file = grunt.file.read(changed);
var body = marked.parser(marked.lexer(file));
var output = layout.render({
title: o.title || 'Default title (change it via options.title in markdown task config)',
description: o.description || 'Default description (change it via options.title in markdown task config)',
styles: o.styles || [],
body: body
});
var fileout = path.join(o.out || './', changed.replace(path.extname(changed), '.html'));
grunt.log.write('Writing to ' + fileout);
grunt.file.write(fileout, output);
grunt.log.ok();
});
};
<!doctype html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>{{ title }}</title>
<meta name="description" content="{{ description }}">
<meta name="viewport" content="width=device-width">
{{#styles}}
<link rel="stylesheet" href="{{ . }}">
{{/styles}}
<style>
body {
margin: auto;
width: 85%;
}
</style>
</head>
<body>
{{{ body }}}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</body>
</html>
{
"name": "mamamarkdown",
"version": "0.0.0",
"description": "Grunt task - Quick and dirty little markdown utility, compiling down to html, triggering reload",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"marked": "~0.2.5",
"hogan": "~1.0.5-dev"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment