2015-05-04 07:53:29 +00:00
|
|
|
/*!
|
2016-02-22 00:58:06 +00:00
|
|
|
* markdown-it-regexp
|
2015-05-04 07:53:29 +00:00
|
|
|
* Copyright (c) 2014 Alex Kocharin
|
|
|
|
* MIT Licensed
|
|
|
|
*/
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
var inherits = function(ctor, superCtor) {
|
|
|
|
ctor.super_ = superCtor;
|
|
|
|
ctor.prototype = Object.create(superCtor.prototype, {
|
|
|
|
constructor: {
|
|
|
|
value: ctor,
|
|
|
|
enumerable: false,
|
|
|
|
writable: true,
|
|
|
|
configurable: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
/**
|
|
|
|
* Escape special characters in the given string of html.
|
|
|
|
*
|
|
|
|
* Borrowed from escape-html component, MIT-licensed
|
|
|
|
*/
|
|
|
|
var stuff = {};
|
|
|
|
stuff.escape = function(html) {
|
|
|
|
return String(html)
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
}
|
|
|
|
|
2016-01-12 13:34:44 +00:00
|
|
|
Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
|
|
|
|
if (!isIE9()) {
|
|
|
|
obj.__proto__ = proto;
|
|
|
|
} else {
|
|
|
|
/** IE9 fix - copy object methods from the protype to the new object **/
|
|
|
|
for (var prop in proto) {
|
|
|
|
obj[prop] = proto[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
var isIE9 = function() {
|
|
|
|
return navigator.appVersion.indexOf("MSIE") > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counter for multi usage.
|
|
|
|
*/
|
|
|
|
var counter = 0
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
/**
|
|
|
|
* Constructor function
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Plugin(regexp, replacer) {
|
|
|
|
// return value should be a callable function
|
2016-02-22 00:58:06 +00:00
|
|
|
// with strictly defined options passed by markdown-it
|
|
|
|
var self = function (md, options) {
|
2015-05-04 07:53:29 +00:00
|
|
|
self.options = options
|
2016-02-22 00:58:06 +00:00
|
|
|
self.init(md)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2016-02-22 00:58:06 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
// initialize plugin object
|
2016-01-12 13:34:44 +00:00
|
|
|
Object.setPrototypeOf(self, Plugin.prototype)
|
2016-02-22 00:58:06 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
// clone regexp with all the flags
|
|
|
|
var flags = (regexp.global ? 'g' : '')
|
|
|
|
+ (regexp.multiline ? 'm' : '')
|
|
|
|
+ (regexp.ignoreCase ? 'i' : '')
|
|
|
|
|
|
|
|
self.regexp = RegExp('^' + regexp.source, flags)
|
|
|
|
|
|
|
|
// copy init options
|
|
|
|
self.replacer = replacer
|
2016-02-22 00:58:06 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
// this plugin can be inserted multiple times,
|
|
|
|
// so we're generating unique name for it
|
2016-01-12 13:34:44 +00:00
|
|
|
self.id = 'regexp-' + counter
|
|
|
|
counter++
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
inherits(Plugin, Function)
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
// function that registers plugin with markdown-it
|
|
|
|
Plugin.prototype.init = function (md) {
|
|
|
|
md.inline.ruler.push(this.id, this.parse.bind(this))
|
|
|
|
|
|
|
|
md.renderer.rules[this.id] = this.render.bind(this)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
Plugin.prototype.parse = function (state, silent) {
|
2015-05-04 07:53:29 +00:00
|
|
|
// slowwww... maybe use an advanced regexp engine for this
|
|
|
|
var match = this.regexp.exec(state.src.slice(state.pos))
|
|
|
|
if (!match) return false
|
|
|
|
|
|
|
|
// valid match found, now we need to advance cursor
|
|
|
|
state.pos += match[0].length
|
|
|
|
|
|
|
|
// don't insert any tokens in silent mode
|
|
|
|
if (silent) return true
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
var token = state.push(this.id, '', 0)
|
|
|
|
token.meta = { match: match }
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
Plugin.prototype.render = function (tokens, id, options, env) {
|
|
|
|
return this.replacer(tokens[id].meta.match, stuff)
|
|
|
|
}
|