Created
June 6, 2012 00:33
-
-
Save ukyo/2879128 to your computer and use it in GitHub Desktop.
markdown.html5.js
This file contains 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
// Released under MIT license | |
// Copyright (c) 2012 Syu Kato <[email protected]> | |
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