1
0
Fork 0

added highlight.js support

Dieser Commit ist enthalten in:
Matthew Scragg 2013-10-08 14:47:49 -05:00
Ursprung d30c9d71a6
Commit 9d3751f241
11 geänderte Dateien mit 204 neuen und 38 gelöschten Zeilen

Dateidiff unterdrückt, weil mindestens eine Zeile zu lang ist

Dateidiff unterdrückt, weil mindestens eine Zeile zu lang ist

Datei anzeigen

@ -0,0 +1,105 @@
/*global module:true*/
/*
* Basic table support with re-entrant parsing, where cell content
* can also specify markdown.
*
* Tables
* ======
*
* | Col 1 | Col 2 |
* |======== |====================================================|
* |**bold** | ![Valid XHTML] (http://w3.org/Icons/valid-xhtml10) |
* | Plain | Value |
*
*/
(function(){
var table = function(converter) {
var tables = {}, style = 'text-align:left;', filter;
tables.th = function(header){
if (header.trim() === "") { return "";}
var id = header.trim().replace(/ /g, '_').toLowerCase();
return '<th id="' + id + '" style="'+style+'">' + header + '</th>';
};
tables.td = function(cell) {
return '<td style="'+style+'">' + converter.makeHtml(cell) + '</td>';
};
tables.ths = function(){
var out = "", i = 0, hs = [].slice.apply(arguments);
for (i;i<hs.length;i+=1) {
out += tables.th(hs[i]) + '\n';
}
return out;
};
tables.tds = function(){
var out = "", i = 0, ds = [].slice.apply(arguments);
for (i;i<ds.length;i+=1) {
out += tables.td(ds[i]) + '\n';
}
return out;
};
tables.thead = function() {
var out, i = 0, hs = [].slice.apply(arguments);
out = "<thead>\n";
out += "<tr>\n";
out += tables.ths.apply(this, hs);
out += "</tr>\n";
out += "</thead>\n";
return out;
};
tables.tr = function() {
var out, i = 0, cs = [].slice.apply(arguments);
out = "<tr>\n";
out += tables.tds.apply(this, cs);
out += "</tr>\n";
return out;
};
filter = function(text) {
var i=0, lines = text.split('\n'), tbl = [], line, hs, rows, out = [];
for (i; i<lines.length;i+=1) {
line = lines[i];
// looks like a table heading
if (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
line = line.trim();
tbl.push('<table>');
hs = line.substring(1, line.length -1).split('|');
tbl.push(tables.thead.apply(this, hs));
line = lines[++i];
if (!line.trim().match(/^[|]{1}[-=| ]+[|]{1}$/)) {
// not a table rolling back
line = lines[--i];
}
else {
line = lines[++i];
tbl.push('<tbody>');
while (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
line = line.trim();
tbl.push(tables.tr.apply(this, line.substring(1, line.length -1).split('|')));
line = lines[++i];
}
tbl.push('</tbody>');
tbl.push('</table>');
// we are done with this table and we move along
out.push(tbl.join('\n'));
continue;
}
}
out.push(line);
}
return out.join('\n');
};
return [
{
type: 'lang',
filter: filter
}
];
};
// Client-side export
if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.table = table; }
// Server-side export
if (typeof module !== 'undefined') {
module.exports = table;
}
}());

Datei anzeigen

@ -3,7 +3,15 @@
* Copyright (c) 2010 Caolan McMahon
*/
function escapeHtml(s) {
s = ('' + s); /* Coerce to string */
s = s.replace(/&/g, '&amp;');
s = s.replace(/</g, '&lt;');
s = s.replace(/>/g, '&gt;');
s = s.replace(/"/g, '&quot;');
s = s.replace(/'/g, '&#39;');
return s;
}
/**
* Main function for converting markdown to HTML.
@ -87,6 +95,34 @@ WMD.preprocessors = {
}
doc.markdown = lines.join('\n');
return doc;
},
fencedCodeBlocksHighlightJS: function (doc) {
var re1 = /```([A-Za-z]+)\s*([\s\S]+?)```/; // with syntax highlighting
var re2 = /```\s*([\s\S]+?)```/; // without syntax highlighting
var block;
while (block = re1.exec(doc.markdown) || re2.exec(doc.markdown)) {
var pre;
if (block.length === 3) {
// we have a code format
pre = '<pre style="padding:0;"><code class="' + escapeHtml(block[1]) + '">';
if (block[1] in hljs.LANGUAGES) {
pre += hljs.highlight(block[1], block[2]).value;
}
else {
pre += escapeHtml(block[2]);
}
pre += '</code></pre>';
}
else {
// no syntax highlighting
pre = '<pre style="padding:0;"><code class="no-highlight">' +
escapeHtml(block[1]) + '</code></pre>';
}
doc.markdown = doc.markdown.substr(0, block.index) +
pre + doc.markdown.substr(block.index + block[0].length);
}
return doc;
}
};
@ -104,9 +140,11 @@ WMD.readOptions = function (options) {
var obj = {
preprocessors: [
WMD.preprocessors.metadata,
WMD.preprocessors.underscores
WMD.preprocessors.underscores,
WMD.preprocessors.fencedCodeBlocksHighlightJS
],
postprocessors: []
postprocessors: [
]
};
for (var k in options) {
obj[k] = options[k];