2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Created by Tivie on 06-01-2015.
|
|
|
|
*/
|
|
|
|
// Private properties
|
|
|
|
var showdown = {},
|
|
|
|
parsers = {},
|
2015-03-02 02:15:32 +08:00
|
|
|
extensions = {},
|
2015-07-13 12:09:03 +08:00
|
|
|
globalOptions = getDefaultOpts(true),
|
2017-01-09 03:09:12 +08:00
|
|
|
setFlavor = 'vanilla',
|
2015-07-12 09:15:35 +08:00
|
|
|
flavor = {
|
|
|
|
github: {
|
2016-11-11 16:15:24 +08:00
|
|
|
omitExtraWLInCodeBlocks: true,
|
|
|
|
simplifiedAutoLink: true,
|
|
|
|
literalMidWordUnderscores: true,
|
|
|
|
strikethrough: true,
|
|
|
|
tables: true,
|
|
|
|
tablesHeaderId: true,
|
|
|
|
ghCodeBlocks: true,
|
|
|
|
tasklists: true,
|
2016-12-01 02:04:17 +08:00
|
|
|
disableForced4SpacesIndentedSublists: true,
|
2016-12-17 14:01:15 +08:00
|
|
|
simpleLineBreaks: true,
|
2016-12-31 03:01:44 +08:00
|
|
|
requireSpaceBeforeHeadingText: true,
|
2017-01-06 12:33:12 +08:00
|
|
|
ghCompatibleHeaderId: true,
|
2017-08-07 00:53:06 +08:00
|
|
|
ghMentions: true,
|
2017-10-24 20:44:49 +08:00
|
|
|
backslashEscapesHTMLTags: true,
|
2017-12-22 17:54:23 +08:00
|
|
|
emoji: true,
|
|
|
|
splitAdjacentBlockquotes: true
|
2015-07-12 09:15:35 +08:00
|
|
|
},
|
2017-02-06 14:50:52 +08:00
|
|
|
original: {
|
|
|
|
noHeaderId: true,
|
|
|
|
ghCodeBlocks: false
|
|
|
|
},
|
2017-02-06 15:22:01 +08:00
|
|
|
ghost: {
|
|
|
|
omitExtraWLInCodeBlocks: true,
|
|
|
|
parseImgDimensions: true,
|
|
|
|
simplifiedAutoLink: true,
|
|
|
|
literalMidWordUnderscores: true,
|
|
|
|
strikethrough: true,
|
|
|
|
tables: true,
|
|
|
|
tablesHeaderId: true,
|
|
|
|
ghCodeBlocks: true,
|
|
|
|
tasklists: true,
|
|
|
|
smoothLivePreview: true,
|
|
|
|
simpleLineBreaks: true,
|
|
|
|
requireSpaceBeforeHeadingText: true,
|
|
|
|
ghMentions: false,
|
|
|
|
encodeEmails: true
|
|
|
|
},
|
2016-12-23 17:29:58 +08:00
|
|
|
vanilla: getDefaultOpts(true),
|
|
|
|
allOn: allOptionsOn()
|
2015-07-12 09:15:35 +08:00
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* helper namespace
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
showdown.helper = {};
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* TODO LEGACY SUPPORT CODE
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.extensions = {};
|
|
|
|
|
2015-01-20 00:28:14 +08:00
|
|
|
/**
|
|
|
|
* Set a global option
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-20 00:28:14 +08:00
|
|
|
* @param {string} key
|
2015-05-27 08:43:08 +08:00
|
|
|
* @param {*} value
|
2015-01-20 00:28:14 +08:00
|
|
|
* @returns {showdown}
|
|
|
|
*/
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.setOption = function (key, value) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
globalOptions[key] = value;
|
|
|
|
return this;
|
2015-01-16 05:21:33 +08:00
|
|
|
};
|
|
|
|
|
2015-01-20 00:28:14 +08:00
|
|
|
/**
|
|
|
|
* Get a global option
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-20 00:28:14 +08:00
|
|
|
* @param {string} key
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2015-01-18 10:12:32 +08:00
|
|
|
showdown.getOption = function (key) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
return globalOptions[key];
|
2015-01-18 10:12:32 +08:00
|
|
|
};
|
|
|
|
|
2015-01-20 00:28:14 +08:00
|
|
|
/**
|
|
|
|
* Get the global options
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-06-15 21:49:26 +08:00
|
|
|
* @returns {{}}
|
2015-01-20 00:28:14 +08:00
|
|
|
*/
|
2015-01-18 10:12:32 +08:00
|
|
|
showdown.getOptions = function () {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
return globalOptions;
|
2015-01-18 10:12:32 +08:00
|
|
|
};
|
|
|
|
|
2015-06-15 21:49:26 +08:00
|
|
|
/**
|
|
|
|
* Reset global options to the default values
|
|
|
|
* @static
|
|
|
|
*/
|
2015-06-01 03:56:28 +08:00
|
|
|
showdown.resetOptions = function () {
|
|
|
|
'use strict';
|
2015-07-13 12:09:03 +08:00
|
|
|
globalOptions = getDefaultOpts(true);
|
2015-06-01 03:56:28 +08:00
|
|
|
};
|
|
|
|
|
2015-07-12 09:15:35 +08:00
|
|
|
/**
|
|
|
|
* Set the flavor showdown should use as default
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
showdown.setFlavor = function (name) {
|
|
|
|
'use strict';
|
2017-01-09 03:09:12 +08:00
|
|
|
if (!flavor.hasOwnProperty(name)) {
|
|
|
|
throw Error(name + ' flavor was not found');
|
|
|
|
}
|
2017-03-31 06:17:10 +08:00
|
|
|
showdown.resetOptions();
|
2017-01-09 03:09:12 +08:00
|
|
|
var preset = flavor[name];
|
|
|
|
setFlavor = name;
|
|
|
|
for (var option in preset) {
|
|
|
|
if (preset.hasOwnProperty(option)) {
|
|
|
|
globalOptions[option] = preset[option];
|
2015-07-12 09:15:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-09 03:09:12 +08:00
|
|
|
/**
|
|
|
|
* Get the currently set flavor
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
showdown.getFlavor = function () {
|
|
|
|
'use strict';
|
|
|
|
return setFlavor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the options of a specified flavor. Returns undefined if the flavor was not found
|
|
|
|
* @param {string} name Name of the flavor
|
|
|
|
* @returns {{}|undefined}
|
|
|
|
*/
|
|
|
|
showdown.getFlavorOptions = function (name) {
|
|
|
|
'use strict';
|
|
|
|
if (flavor.hasOwnProperty(name)) {
|
|
|
|
return flavor[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-15 21:49:26 +08:00
|
|
|
/**
|
|
|
|
* Get the default options
|
|
|
|
* @static
|
2015-07-13 12:09:03 +08:00
|
|
|
* @param {boolean} [simple=true]
|
2015-06-15 21:49:26 +08:00
|
|
|
* @returns {{}}
|
|
|
|
*/
|
2015-07-13 12:09:03 +08:00
|
|
|
showdown.getDefaultOptions = function (simple) {
|
2015-06-15 21:49:26 +08:00
|
|
|
'use strict';
|
2015-07-13 12:09:03 +08:00
|
|
|
return getDefaultOpts(simple);
|
2015-06-15 21:49:26 +08:00
|
|
|
};
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-01-20 00:28:14 +08:00
|
|
|
* Get or set a subParser
|
2015-01-16 05:21:33 +08:00
|
|
|
*
|
|
|
|
* subParser(name) - Get a registered subParser
|
|
|
|
* subParser(name, func) - Register a subParser
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-16 05:21:33 +08:00
|
|
|
* @param {string} name
|
|
|
|
* @param {function} [func]
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
showdown.subParser = function (name, func) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
if (showdown.helper.isString(name)) {
|
|
|
|
if (typeof func !== 'undefined') {
|
|
|
|
parsers[name] = func;
|
|
|
|
} else {
|
|
|
|
if (parsers.hasOwnProperty(name)) {
|
|
|
|
return parsers[name];
|
|
|
|
} else {
|
|
|
|
throw Error('SubParser named ' + name + ' not registered!');
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2018-07-08 02:05:16 +08:00
|
|
|
} else {
|
|
|
|
throw Error('showdown.subParser function first argument must be a string (the name of the subparser)');
|
2015-01-19 19:37:21 +08:00
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
};
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Gets or registers an extension
|
|
|
|
* @static
|
|
|
|
* @param {string} name
|
2022-03-03 20:15:50 +08:00
|
|
|
* @param {object|object[]|function=} ext
|
2015-06-01 03:56:28 +08:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2015-03-02 02:15:32 +08:00
|
|
|
showdown.extension = function (name, ext) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if (!showdown.helper.isString(name)) {
|
|
|
|
throw Error('Extension \'name\' must be a string');
|
|
|
|
}
|
|
|
|
|
|
|
|
name = showdown.helper.stdExtName(name);
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
// Getter
|
2015-03-02 02:15:32 +08:00
|
|
|
if (showdown.helper.isUndefined(ext)) {
|
2015-06-01 03:56:28 +08:00
|
|
|
if (!extensions.hasOwnProperty(name)) {
|
|
|
|
throw Error('Extension named ' + name + ' is not registered!');
|
|
|
|
}
|
|
|
|
return extensions[name];
|
|
|
|
|
|
|
|
// Setter
|
2015-03-02 02:15:32 +08:00
|
|
|
} else {
|
2015-06-08 02:02:45 +08:00
|
|
|
// Expand extension if it's wrapped in a function
|
2015-06-01 03:56:28 +08:00
|
|
|
if (typeof ext === 'function') {
|
|
|
|
ext = ext();
|
|
|
|
}
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
// Ensure extension is an array
|
|
|
|
if (!showdown.helper.isArray(ext)) {
|
|
|
|
ext = [ext];
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
var validExtension = validate(ext, name);
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
if (validExtension.valid) {
|
|
|
|
extensions[name] = ext;
|
|
|
|
} else {
|
|
|
|
throw Error(validExtension.error);
|
|
|
|
}
|
2015-03-02 02:15:32 +08:00
|
|
|
}
|
2015-06-01 03:56:28 +08:00
|
|
|
};
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Gets all extensions registered
|
|
|
|
* @returns {{}}
|
|
|
|
*/
|
|
|
|
showdown.getAllExtensions = function () {
|
2015-03-02 02:15:32 +08:00
|
|
|
'use strict';
|
2015-06-01 03:56:28 +08:00
|
|
|
return extensions;
|
|
|
|
};
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Remove an extension
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
showdown.removeExtension = function (name) {
|
|
|
|
'use strict';
|
|
|
|
delete extensions[name];
|
|
|
|
};
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Removes all extensions
|
|
|
|
*/
|
|
|
|
showdown.resetExtensions = function () {
|
|
|
|
'use strict';
|
|
|
|
extensions = {};
|
|
|
|
};
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-06-01 03:56:28 +08:00
|
|
|
* Validate extension
|
2015-06-08 02:02:45 +08:00
|
|
|
* @param {array} extension
|
2015-06-01 03:56:28 +08:00
|
|
|
* @param {string} name
|
|
|
|
* @returns {{valid: boolean, error: string}}
|
2015-01-16 05:21:33 +08:00
|
|
|
*/
|
2017-01-31 13:46:25 +08:00
|
|
|
function validate (extension, name) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension',
|
2017-01-31 13:46:25 +08:00
|
|
|
ret = {
|
|
|
|
valid: true,
|
|
|
|
error: ''
|
|
|
|
};
|
2015-05-27 02:46:07 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (!showdown.helper.isArray(extension)) {
|
|
|
|
extension = [extension];
|
2015-01-19 19:37:21 +08:00
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
for (var i = 0; i < extension.length; ++i) {
|
2015-07-13 12:09:03 +08:00
|
|
|
var baseMsg = errMsg + ' sub-extension ' + i + ': ',
|
2015-06-08 02:02:45 +08:00
|
|
|
ext = extension[i];
|
|
|
|
if (typeof ext !== 'object') {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + 'must be an object, but ' + typeof ext + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-22 23:58:07 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (!showdown.helper.isString(ext.type)) {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + 'property "type" must be a string, but ' + typeof ext.type + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-22 23:58:07 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
var type = ext.type = ext.type.toLowerCase();
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
// normalize extension type
|
|
|
|
if (type === 'language') {
|
|
|
|
type = ext.type = 'lang';
|
|
|
|
}
|
2015-03-02 02:15:32 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (type === 'html') {
|
|
|
|
type = ext.type = 'output';
|
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
if (type !== 'lang' && type !== 'output' && type !== 'listener') {
|
2015-06-01 03:56:28 +08:00
|
|
|
ret.valid = false;
|
2015-08-03 10:47:49 +08:00
|
|
|
ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang/language", "output/html" or "listener"';
|
2015-06-01 03:56:28 +08:00
|
|
|
return ret;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
if (type === 'listener') {
|
|
|
|
if (showdown.helper.isUndefined(ext.listeners)) {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '. Extensions of type "listener" must have a property called "listeners"';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + type + ' extensions must define either a "regex" property or a "filter" method';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext.listeners) {
|
|
|
|
if (typeof ext.listeners !== 'object') {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '"listeners" property must be an object but ' + typeof ext.listeners + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
for (var ln in ext.listeners) {
|
|
|
|
if (ext.listeners.hasOwnProperty(ln)) {
|
|
|
|
if (typeof ext.listeners[ln] !== 'function') {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '"listeners" property must be an hash of [event name]: [callback]. listeners.' + ln +
|
|
|
|
' must be a function but ' + typeof ext.listeners[ln] + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (ext.filter) {
|
|
|
|
if (typeof ext.filter !== 'function') {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '"filter" must be a function, but ' + typeof ext.filter + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else if (ext.regex) {
|
|
|
|
if (showdown.helper.isString(ext.regex)) {
|
|
|
|
ext.regex = new RegExp(ext.regex, 'g');
|
|
|
|
}
|
2017-01-31 13:46:25 +08:00
|
|
|
if (!(ext.regex instanceof RegExp)) {
|
2015-06-08 02:02:45 +08:00
|
|
|
ret.valid = false;
|
2015-08-03 10:47:49 +08:00
|
|
|
ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' + typeof ext.regex + ' given';
|
2015-06-08 02:02:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (showdown.helper.isUndefined(ext.replace)) {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '"regex" extensions must implement a replace string or function';
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
}
|
2015-01-20 00:28:14 +08:00
|
|
|
}
|
2015-06-01 03:56:28 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-27 08:37:01 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Validate extension
|
|
|
|
* @param {object} ext
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
showdown.validateExtension = function (ext) {
|
|
|
|
'use strict';
|
2015-05-27 08:37:01 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
var validateExtension = validate(ext, null);
|
|
|
|
if (!validateExtension.valid) {
|
|
|
|
console.warn(validateExtension.error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-01-16 05:21:33 +08:00
|
|
|
};
|