2015-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-01-19 22:57:43 +08:00
|
|
|
* showdownjs helper functions
|
2015-01-16 05:21:33 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-19 22:57:43 +08:00
|
|
|
if (!showdown.hasOwnProperty('helper')) {
|
|
|
|
showdown.helper = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if var is string
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-19 22:57:43 +08:00
|
|
|
* @param {string} a
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.isString = function (a) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
return (typeof a === 'string' || a instanceof String);
|
2015-01-19 22:57:43 +08:00
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
/**
|
|
|
|
* Check if var is a function
|
|
|
|
* @static
|
2017-01-28 09:19:07 +08:00
|
|
|
* @param {*} a
|
2016-01-02 07:33:33 +08:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.isFunction = function (a) {
|
2016-01-02 07:33:33 +08:00
|
|
|
'use strict';
|
|
|
|
var getType = {};
|
|
|
|
return a && getType.toString.call(a) === '[object Function]';
|
|
|
|
};
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* isArray helper function
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-16 05:21:33 +08:00
|
|
|
* @param {*} a
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.isArray = function (a) {
|
2015-01-19 22:57:43 +08:00
|
|
|
'use strict';
|
|
|
|
return a.constructor === Array;
|
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if value is undefined
|
|
|
|
* @static
|
|
|
|
* @param {*} value The value to check.
|
|
|
|
* @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.isUndefined = function (value) {
|
2015-01-19 22:57:43 +08:00
|
|
|
'use strict';
|
|
|
|
return typeof value === 'undefined';
|
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-01-28 09:19:07 +08:00
|
|
|
/**
|
|
|
|
* ForEach helper function
|
|
|
|
* Iterates over Arrays and Objects (own properties only)
|
|
|
|
* @static
|
|
|
|
* @param {*} obj
|
|
|
|
* @param {function} callback Accepts 3 params: 1. value, 2. key, 3. the original array/object
|
|
|
|
*/
|
|
|
|
showdown.helper.forEach = function (obj, callback) {
|
|
|
|
'use strict';
|
|
|
|
// check if obj is defined
|
|
|
|
if (showdown.helper.isUndefined(obj)) {
|
|
|
|
throw new Error('obj param is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showdown.helper.isUndefined(callback)) {
|
|
|
|
throw new Error('callback param is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!showdown.helper.isFunction(callback)) {
|
|
|
|
throw new Error('callback param must be a function/closure');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof obj.forEach === 'function') {
|
|
|
|
obj.forEach(callback);
|
|
|
|
} else if (showdown.helper.isArray(obj)) {
|
|
|
|
for (var i = 0; i < obj.length; i++) {
|
|
|
|
callback(obj[i], i, obj);
|
|
|
|
}
|
|
|
|
} else if (typeof (obj) === 'object') {
|
|
|
|
for (var prop in obj) {
|
|
|
|
if (obj.hasOwnProperty(prop)) {
|
|
|
|
callback(obj[prop], prop, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('obj does not seem to be an array or an iterable object');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-02 02:15:32 +08:00
|
|
|
/**
|
|
|
|
* Standardidize extension name
|
|
|
|
* @static
|
|
|
|
* @param {string} s extension name
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
showdown.helper.stdExtName = function (s) {
|
|
|
|
'use strict';
|
2017-01-28 09:19:07 +08:00
|
|
|
return s.replace(/[_?*+\/\\.^-]/g, '').replace(/\s/g, '').toLowerCase();
|
2015-03-02 02:15:32 +08:00
|
|
|
};
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function escapeCharactersCallback (wholeMatch, m1) {
|
2015-01-19 23:42:20 +08:00
|
|
|
'use strict';
|
|
|
|
var charCodeToEscape = m1.charCodeAt(0);
|
2017-01-29 08:07:19 +08:00
|
|
|
return '¨E' + charCodeToEscape + 'E';
|
2015-01-19 23:42:20 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Callback used to escape characters when passing through String.replace
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-16 05:21:33 +08:00
|
|
|
* @param {string} wholeMatch
|
|
|
|
* @param {string} m1
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2015-01-19 23:42:20 +08:00
|
|
|
showdown.helper.escapeCharactersCallback = escapeCharactersCallback;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape characters in a string
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-16 05:21:33 +08:00
|
|
|
* @param {string} text
|
|
|
|
* @param {string} charsToEscape
|
|
|
|
* @param {boolean} afterBackslash
|
|
|
|
* @returns {XML|string|void|*}
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.escapeCharacters = function (text, charsToEscape, afterBackslash) {
|
2015-01-19 22:57:43 +08:00
|
|
|
'use strict';
|
|
|
|
// First we have to escape the escape characters so that
|
|
|
|
// we can build a character class out of them
|
|
|
|
var regexString = '([' + charsToEscape.replace(/([\[\]\\])/g, '\\$1') + '])';
|
|
|
|
|
|
|
|
if (afterBackslash) {
|
|
|
|
regexString = '\\\\' + regexString;
|
|
|
|
}
|
|
|
|
|
|
|
|
var regex = new RegExp(regexString, 'g');
|
|
|
|
text = text.replace(regex, escapeCharactersCallback);
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
2015-05-31 20:08:15 +08:00
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
var rgxFindMatchPos = function (str, left, right, flags) {
|
|
|
|
'use strict';
|
|
|
|
var f = flags || '',
|
2017-01-31 13:46:25 +08:00
|
|
|
g = f.indexOf('g') > -1,
|
|
|
|
x = new RegExp(left + '|' + right, 'g' + f.replace(/g/g, '')),
|
|
|
|
l = new RegExp(left, f.replace(/g/g, '')),
|
|
|
|
pos = [],
|
|
|
|
t, s, m, start, end;
|
2016-01-02 07:33:33 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
t = 0;
|
|
|
|
while ((m = x.exec(str))) {
|
|
|
|
if (l.test(m[0])) {
|
|
|
|
if (!(t++)) {
|
|
|
|
s = x.lastIndex;
|
|
|
|
start = s - m[0].length;
|
|
|
|
}
|
|
|
|
} else if (t) {
|
|
|
|
if (!--t) {
|
|
|
|
end = m.index + m[0].length;
|
2016-01-02 09:08:17 +08:00
|
|
|
var obj = {
|
|
|
|
left: {start: start, end: s},
|
|
|
|
match: {start: s, end: m.index},
|
|
|
|
right: {start: m.index, end: end},
|
|
|
|
wholeMatch: {start: start, end: end}
|
|
|
|
};
|
|
|
|
pos.push(obj);
|
2016-01-02 07:33:33 +08:00
|
|
|
if (!g) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t && (x.lastIndex = s));
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
};
|
|
|
|
|
2015-10-19 08:55:35 +08:00
|
|
|
/**
|
|
|
|
* matchRecursiveRegExp
|
|
|
|
*
|
|
|
|
* (c) 2007 Steven Levithan <stevenlevithan.com>
|
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* Accepts a string to search, a left and right format delimiter
|
|
|
|
* as regex patterns, and optional regex flags. Returns an array
|
|
|
|
* of matches, allowing nested instances of left/right delimiters.
|
|
|
|
* Use the "g" flag to return all matches, otherwise only the
|
|
|
|
* first is returned. Be careful to ensure that the left and
|
|
|
|
* right format delimiters produce mutually exclusive matches.
|
|
|
|
* Backreferences are not supported within the right delimiter
|
|
|
|
* due to how it is internally combined with the left delimiter.
|
|
|
|
* When matching strings whose format delimiters are unbalanced
|
|
|
|
* to the left or right, the output is intentionally as a
|
|
|
|
* conventional regex library with recursion support would
|
|
|
|
* produce, e.g. "<<x>" and "<x>>" both produce ["x"] when using
|
|
|
|
* "<" and ">" as the delimiters (both strings contain a single,
|
|
|
|
* balanced instance of "<x>").
|
|
|
|
*
|
|
|
|
* examples:
|
|
|
|
* matchRecursiveRegExp("test", "\\(", "\\)")
|
|
|
|
* returns: []
|
|
|
|
* matchRecursiveRegExp("<t<<e>><s>>t<>", "<", ">", "g")
|
|
|
|
* returns: ["t<<e>><s>", ""]
|
|
|
|
* matchRecursiveRegExp("<div id=\"x\">test</div>", "<div\\b[^>]*>", "</div>", "gi")
|
|
|
|
* returns: ["test"]
|
|
|
|
*/
|
|
|
|
showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) {
|
|
|
|
'use strict';
|
|
|
|
|
2016-01-02 09:16:40 +08:00
|
|
|
var matchPos = rgxFindMatchPos (str, left, right, flags),
|
2017-01-31 13:46:25 +08:00
|
|
|
results = [];
|
2015-10-19 08:55:35 +08:00
|
|
|
|
2016-01-02 09:16:40 +08:00
|
|
|
for (var i = 0; i < matchPos.length; ++i) {
|
|
|
|
results.push([
|
|
|
|
str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end),
|
|
|
|
str.slice(matchPos[i].match.start, matchPos[i].match.end),
|
|
|
|
str.slice(matchPos[i].left.start, matchPos[i].left.end),
|
|
|
|
str.slice(matchPos[i].right.start, matchPos[i].right.end)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return results;
|
2015-10-19 08:55:35 +08:00
|
|
|
};
|
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @param {string|function} replacement
|
|
|
|
* @param {string} left
|
|
|
|
* @param {string} right
|
|
|
|
* @param {string} flags
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
showdown.helper.replaceRecursiveRegExp = function (str, replacement, left, right, flags) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if (!showdown.helper.isFunction(replacement)) {
|
|
|
|
var repStr = replacement;
|
|
|
|
replacement = function () {
|
|
|
|
return repStr;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchPos = rgxFindMatchPos(str, left, right, flags),
|
|
|
|
finalStr = str,
|
|
|
|
lng = matchPos.length;
|
|
|
|
|
|
|
|
if (lng > 0) {
|
|
|
|
var bits = [];
|
2016-01-02 09:08:17 +08:00
|
|
|
if (matchPos[0].wholeMatch.start !== 0) {
|
|
|
|
bits.push(str.slice(0, matchPos[0].wholeMatch.start));
|
2016-01-02 07:33:33 +08:00
|
|
|
}
|
|
|
|
for (var i = 0; i < lng; ++i) {
|
2016-01-02 09:08:17 +08:00
|
|
|
bits.push(
|
|
|
|
replacement(
|
|
|
|
str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end),
|
|
|
|
str.slice(matchPos[i].match.start, matchPos[i].match.end),
|
|
|
|
str.slice(matchPos[i].left.start, matchPos[i].left.end),
|
|
|
|
str.slice(matchPos[i].right.start, matchPos[i].right.end)
|
|
|
|
)
|
|
|
|
);
|
2016-01-02 07:33:33 +08:00
|
|
|
if (i < lng - 1) {
|
2016-01-02 09:08:17 +08:00
|
|
|
bits.push(str.slice(matchPos[i].wholeMatch.end, matchPos[i + 1].wholeMatch.start));
|
2016-01-02 07:33:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-02 09:08:17 +08:00
|
|
|
if (matchPos[lng - 1].wholeMatch.end < str.length) {
|
|
|
|
bits.push(str.slice(matchPos[lng - 1].wholeMatch.end));
|
2016-01-02 07:33:33 +08:00
|
|
|
}
|
|
|
|
finalStr = bits.join('');
|
|
|
|
}
|
|
|
|
return finalStr;
|
|
|
|
};
|
|
|
|
|
2017-05-29 00:20:07 +08:00
|
|
|
/**
|
|
|
|
* Returns the index within the passed String object of the first occurrence of the specified regex,
|
|
|
|
* starting the search at fromIndex. Returns -1 if the value is not found.
|
|
|
|
*
|
|
|
|
* @param {string} str string to search
|
|
|
|
* @param {RegExp} regex Regular expression to search
|
|
|
|
* @param {int} [fromIndex = 0] Index to start the search
|
|
|
|
* @returns {Number}
|
|
|
|
* @throws InvalidArgumentError
|
|
|
|
*/
|
|
|
|
showdown.helper.regexIndexOf = function (str, regex, fromIndex) {
|
|
|
|
'use strict';
|
|
|
|
if (!showdown.helper.isString(str)) {
|
|
|
|
throw 'InvalidArgumentError: first parameter of showdown.helper.regexIndexOf function must be a string';
|
|
|
|
}
|
|
|
|
if (regex instanceof RegExp === false) {
|
|
|
|
throw 'InvalidArgumentError: second parameter of showdown.helper.regexIndexOf function must be an instance of RegExp';
|
|
|
|
}
|
|
|
|
var indexOf = str.substring(fromIndex || 0).search(regex);
|
|
|
|
return (indexOf >= 0) ? (indexOf + (fromIndex || 0)) : indexOf;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits the passed string object at the defined index, and returns an array composed of the two substrings
|
|
|
|
* @param {string} str string to split
|
|
|
|
* @param {int} index index to split string at
|
|
|
|
* @returns {[string,string]}
|
|
|
|
* @throws InvalidArgumentError
|
|
|
|
*/
|
|
|
|
showdown.helper.splitAtIndex = function (str, index) {
|
|
|
|
'use strict';
|
|
|
|
if (!showdown.helper.isString(str)) {
|
|
|
|
throw 'InvalidArgumentError: first parameter of showdown.helper.regexIndexOf function must be a string';
|
|
|
|
}
|
|
|
|
return [str.substring(0, index), str.substring(index)];
|
|
|
|
};
|
|
|
|
|
2017-01-28 03:03:37 +08:00
|
|
|
/**
|
|
|
|
* Obfuscate an e-mail address through the use of Character Entities,
|
|
|
|
* transforming ASCII characters into their equivalent decimal or hex entities.
|
|
|
|
*
|
|
|
|
* Since it has a random component, subsequent calls to this function produce different results
|
|
|
|
*
|
|
|
|
* @param {string} mail
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
showdown.helper.encodeEmailAddress = function (mail) {
|
|
|
|
'use strict';
|
|
|
|
var encode = [
|
|
|
|
function (ch) {
|
|
|
|
return '&#' + ch.charCodeAt(0) + ';';
|
|
|
|
},
|
|
|
|
function (ch) {
|
|
|
|
return '&#x' + ch.charCodeAt(0).toString(16) + ';';
|
|
|
|
},
|
|
|
|
function (ch) {
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
mail = mail.replace(/./g, function (ch) {
|
|
|
|
if (ch === '@') {
|
|
|
|
// this *must* be encoded. I insist.
|
|
|
|
ch = encode[Math.floor(Math.random() * 2)](ch);
|
|
|
|
} else {
|
|
|
|
var r = Math.random();
|
|
|
|
// roughly 10% raw, 45% hex, 45% dec
|
|
|
|
ch = (
|
|
|
|
r > 0.9 ? encode[2](ch) : r > 0.45 ? encode[1](ch) : encode[0](ch)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
});
|
|
|
|
|
|
|
|
return mail;
|
|
|
|
};
|
|
|
|
|
2015-05-31 20:08:15 +08:00
|
|
|
/**
|
|
|
|
* POLYFILLS
|
|
|
|
*/
|
2017-01-06 12:00:30 +08:00
|
|
|
// use this instead of builtin is undefined for IE8 compatibility
|
|
|
|
if (typeof(console) === 'undefined') {
|
2015-05-31 20:08:15 +08:00
|
|
|
console = {
|
|
|
|
warn: function (msg) {
|
|
|
|
'use strict';
|
|
|
|
alert(msg);
|
|
|
|
},
|
|
|
|
log: function (msg) {
|
|
|
|
'use strict';
|
|
|
|
alert(msg);
|
2015-10-19 08:55:35 +08:00
|
|
|
},
|
|
|
|
error: function (msg) {
|
|
|
|
'use strict';
|
|
|
|
throw msg;
|
2015-05-31 20:08:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-01-28 10:46:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Common regexes.
|
|
|
|
* We declare some common regexes to improve performance
|
|
|
|
*/
|
|
|
|
showdown.helper.regexes = {
|
|
|
|
asteriskAndDash: /([*_])/g
|
|
|
|
};
|