2014-03-20 17:52:16 +04:00
|
|
|
/* jshint node:true, browser:true */
|
|
|
|
|
|
|
|
// Ghost GFM
|
|
|
|
// Taken and extended from the Showdown Github Extension (WIP)
|
|
|
|
// Makes a number of pre and post-processing changes to the way markdown is handled
|
2013-08-20 01:52:50 +04:00
|
|
|
//
|
2014-03-20 17:52:16 +04:00
|
|
|
// ~~strike-through~~ -> <del>strike-through</del> (Pre)
|
|
|
|
// GFM newlines & underscores (Pre)
|
|
|
|
// 4 or more underscores (Pre)
|
|
|
|
// autolinking / custom image handling (Post)
|
2013-08-20 01:52:50 +04:00
|
|
|
|
|
|
|
(function () {
|
2014-03-20 17:52:16 +04:00
|
|
|
var ghostgfm = function () {
|
2013-08-20 01:52:50 +04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
// strike-through
|
|
|
|
// NOTE: showdown already replaced "~" with "~T", so we need to adjust accordingly.
|
|
|
|
type : 'lang',
|
|
|
|
regex : '(~T){2}([^~]+)(~T){2}',
|
|
|
|
replace : function (match, prefix, content, suffix) {
|
|
|
|
return '<del>' + content + '</del>';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2013-09-27 14:35:44 +04:00
|
|
|
// GFM newline and underscore modifications, happen BEFORE showdown
|
2013-08-20 01:52:50 +04:00
|
|
|
type : 'lang',
|
|
|
|
filter : function (text) {
|
2013-10-25 23:54:19 +04:00
|
|
|
var extractions = {},
|
2013-09-27 17:17:19 +04:00
|
|
|
imageMarkdownRegex = /^(?:\{(.*?)\})?!(?:\[([^\n\]]*)\])(?:\(([^\n\]]*)\))?$/gim,
|
2013-08-20 01:52:50 +04:00
|
|
|
hashID = 0;
|
|
|
|
|
|
|
|
function hashId() {
|
|
|
|
return hashID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract pre blocks
|
|
|
|
text = text.replace(/<pre>[\s\S]*?<\/pre>/gim, function (x) {
|
|
|
|
var hash = hashId();
|
2013-10-25 23:54:19 +04:00
|
|
|
extractions[hash] = x;
|
2013-08-20 01:52:50 +04:00
|
|
|
return "{gfm-js-extract-pre-" + hash + "}";
|
|
|
|
}, 'm');
|
|
|
|
|
2013-10-25 23:54:19 +04:00
|
|
|
// Extract code blocks
|
|
|
|
text = text.replace(/```[\s\S]*```/gim, function (x) {
|
|
|
|
var hash = hashId();
|
|
|
|
extractions[hash] = x;
|
|
|
|
return "{gfm-js-extract-code-" + hash + "}";
|
|
|
|
}, 'm');
|
|
|
|
|
|
|
|
|
2013-09-04 15:53:40 +04:00
|
|
|
//prevent foo_bar and foo_bar_baz from ending up with an italic word in the middle
|
2014-01-03 08:45:39 +04:00
|
|
|
text = text.replace(/(^(?! {4}|\t)(?!__)\w+_\w+_\w[\w_]*)/gm, function (x) {
|
2013-08-20 01:52:50 +04:00
|
|
|
return x.replace(/_/gm, '\\_');
|
|
|
|
});
|
|
|
|
|
2013-10-25 23:54:19 +04:00
|
|
|
text = text.replace(/\{gfm-js-extract-code-([0-9]+)\}/gm, function (x, y) {
|
|
|
|
return extractions[y];
|
|
|
|
});
|
|
|
|
|
2013-08-20 01:52:50 +04:00
|
|
|
// in very clear cases, let newlines become <br /> tags
|
2013-11-21 17:50:30 +04:00
|
|
|
text = text.replace(/^[\w\<\"\'][^\n]*\n+/gm, function (x) {
|
2013-08-20 01:52:50 +04:00
|
|
|
return x.match(/\n{2}/) ? x : x.trim() + " \n";
|
|
|
|
});
|
|
|
|
|
2013-09-27 17:17:19 +04:00
|
|
|
// better URL support, but no title support
|
|
|
|
text = text.replace(imageMarkdownRegex, function (match, key, alt, src) {
|
|
|
|
if (src) {
|
|
|
|
return '<img src="' + src + '" alt="' + alt + '" />';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
});
|
2013-09-27 14:35:44 +04:00
|
|
|
|
2013-08-20 01:52:50 +04:00
|
|
|
text = text.replace(/\{gfm-js-extract-pre-([0-9]+)\}/gm, function (x, y) {
|
2013-10-25 23:54:19 +04:00
|
|
|
return "\n\n" + extractions[y];
|
2013-08-20 01:52:50 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
},
|
2014-03-20 17:52:16 +04:00
|
|
|
|
|
|
|
// 4 or more inline underscores e.g. Ghost rocks my _____!
|
|
|
|
{
|
|
|
|
type: 'lang',
|
|
|
|
filter: function (text) {
|
|
|
|
return text.replace(/([^_\n\r])(_{4,})/g, function (match, prefix, underscores) {
|
|
|
|
return prefix + underscores.replace(/_/g, '_');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-20 01:52:50 +04:00
|
|
|
{
|
2013-09-27 14:35:44 +04:00
|
|
|
// GFM autolinking & custom image handling, happens AFTER showdown
|
|
|
|
type : 'html',
|
2013-08-20 01:52:50 +04:00
|
|
|
filter : function (text) {
|
2013-09-27 14:35:44 +04:00
|
|
|
var refExtractions = {},
|
|
|
|
preExtractions = {},
|
2013-08-20 01:52:50 +04:00
|
|
|
hashID = 0;
|
|
|
|
|
|
|
|
function hashId() {
|
|
|
|
return hashID++;
|
|
|
|
}
|
|
|
|
|
2013-09-27 14:35:44 +04:00
|
|
|
// Extract pre blocks
|
|
|
|
text = text.replace(/<(pre|code)>[\s\S]*?<\/(\1)>/gim, function (x) {
|
|
|
|
var hash = hashId();
|
|
|
|
preExtractions[hash] = x;
|
|
|
|
return "{gfm-js-extract-pre-" + hash + "}";
|
|
|
|
}, 'm');
|
|
|
|
|
2013-08-20 01:52:50 +04:00
|
|
|
// filter out def urls
|
2013-09-01 19:32:54 +04:00
|
|
|
// from Marked https://github.com/chjj/marked/blob/master/lib/marked.js#L24
|
|
|
|
text = text.replace(/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/gmi,
|
2013-08-20 01:52:50 +04:00
|
|
|
function (x) {
|
|
|
|
var hash = hashId();
|
2013-09-27 14:35:44 +04:00
|
|
|
refExtractions[hash] = x;
|
2013-08-20 01:52:50 +04:00
|
|
|
return "{gfm-js-extract-ref-url-" + hash + "}";
|
|
|
|
});
|
|
|
|
|
2013-09-01 19:32:54 +04:00
|
|
|
// match a URL
|
|
|
|
// adapted from https://gist.github.com/jorilallo/1283095#L158
|
|
|
|
// and http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
|
|
|
|
text = text.replace(/(\]\(|\]|\[|<a[^\>]*?\>)?https?\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!]/gmi,
|
|
|
|
function (wholeMatch, lookBehind, matchIndex) {
|
|
|
|
// Check we are not inside an HTML tag
|
|
|
|
var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
|
|
|
|
if ((left.match(/<[^>]+$/) && right.match(/^[^>]*>/)) || lookBehind) {
|
|
|
|
return wholeMatch;
|
|
|
|
}
|
|
|
|
// If we have a matching lookBehind, this is a failure, else wrap the match in <a> tag
|
|
|
|
return lookBehind ? wholeMatch : "<a href='" + wholeMatch + "'>" + wholeMatch + "</a>";
|
|
|
|
});
|
2013-08-20 01:52:50 +04:00
|
|
|
|
2013-09-27 14:35:44 +04:00
|
|
|
// replace extractions
|
|
|
|
text = text.replace(/\{gfm-js-extract-pre-([0-9]+)\}/gm, function (x, y) {
|
|
|
|
return preExtractions[y];
|
|
|
|
});
|
|
|
|
|
2013-09-01 19:32:54 +04:00
|
|
|
text = text.replace(/\{gfm-js-extract-ref-url-([0-9]+)\}/gi, function (x, y) {
|
2013-09-27 14:35:44 +04:00
|
|
|
return "\n\n" + refExtractions[y];
|
2013-08-20 01:52:50 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Client-side export
|
2014-03-20 17:52:16 +04:00
|
|
|
if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) {
|
|
|
|
window.Showdown.extensions.ghostgfm = ghostgfm;
|
|
|
|
}
|
2013-08-20 01:52:50 +04:00
|
|
|
// Server-side export
|
2014-03-20 17:52:16 +04:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = ghostgfm;
|
|
|
|
}
|
2013-10-25 23:54:19 +04:00
|
|
|
}());
|