Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created June 6, 2012 00:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukyo/2879128 to your computer and use it in GitHub Desktop.
Save ukyo/2879128 to your computer and use it in GitHub Desktop.
markdown.html5.js
// Released under MIT license
// Copyright (c) 2012 Syu Kato <ukyo.web@gmail.com>
markdown.toHTML5 = function(source, dialect, options) {
return markdown.renderJsonML((function to5(tree, level) {
var i, m,
indices = [],
hx = 'h' + level,
n = tree.length,
blocks = [];
if(!n) return [];
function set(start, end) {
blocks.push(['section', ['h1', tree[start][1]]].concat(to5(tree.slice(start + 1, end), level + 1)));
}
for(i = 0; i < n && hx !== tree[i][0]; ++i) blocks.push(tree[i]);
for(i = 0; i < n; ++i) if(hx === tree[i][0]) indices.push(i);
for(i = 0, m = indices.length - 1; i < m; ++i) set(indices[i], indices[i + 1]);
if(indices.length) set(indices[m], n);
return blocks;
})(markdown.toHTMLTree(source, dialect, options), 1));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment