showdown/src/showdown.js

380 lines
9.4 KiB
JavaScript
Raw Normal View History

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),
setFlavor = 'vanilla',
flavor = {
github: {
omitExtraWLInCodeBlocks: true,
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true,
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true,
requireSpaceBeforeHeadingText: true,
ghCompatibleHeaderId: true,
ghMentions: true,
backslashEscapesHTMLTags: true,
emoji: true,
splitAdjacentBlockquotes: true
},
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-01-16 05:21:33 +08:00
/**
* helper namespace
* @type {{}}
*/
showdown.helper = {};
/**
* TODO LEGACY SUPPORT CODE
* @type {{}}
*/
2015-01-16 05:21:33 +08:00
showdown.extensions = {};
/**
* Set a global option
2015-03-02 02:15:32 +08:00
* @static
* @param {string} key
* @param {*} value
* @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
};
/**
* Get a global option
2015-03-02 02:15:32 +08:00
* @static
* @param {string} key
* @returns {*}
*/
showdown.getOption = function (key) {
2015-01-19 19:37:21 +08:00
'use strict';
return globalOptions[key];
};
/**
* Get the global options
2015-03-02 02:15:32 +08:00
* @static
* @returns {{}}
*/
showdown.getOptions = function () {
2015-01-19 19:37:21 +08:00
'use strict';
return globalOptions;
};
/**
* Reset global options to the default values
* @static
*/
showdown.resetOptions = function () {
'use strict';
2015-07-13 12:09:03 +08:00
globalOptions = getDefaultOpts(true);
};
/**
* Set the flavor showdown should use as default
* @param {string} name
*/
showdown.setFlavor = function (name) {
'use strict';
if (!flavor.hasOwnProperty(name)) {
throw Error(name + ' flavor was not found');
}
showdown.resetOptions();
var preset = flavor[name];
setFlavor = name;
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
globalOptions[option] = preset[option];
}
}
};
/**
* 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];
}
};
/**
* Get the default options
* @static
2015-07-13 12:09:03 +08:00
* @param {boolean} [simple=true]
* @returns {{}}
*/
2015-07-13 12:09:03 +08:00
showdown.getDefaultOptions = function (simple) {
'use strict';
2015-07-13 12:09:03 +08:00
return getDefaultOpts(simple);
};
2015-01-16 05:21:33 +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
}
} 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
};
/**
* Gets or registers an extension
* @static
* @param {string} name
* @param {object|function=} ext
* @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);
// Getter
2015-03-02 02:15:32 +08:00
if (showdown.helper.isUndefined(ext)) {
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 {
// Expand extension if it's wrapped in a function
if (typeof ext === 'function') {
ext = ext();
}
2015-03-02 02:15:32 +08:00
// Ensure extension is an array
if (!showdown.helper.isArray(ext)) {
ext = [ext];
}
var validExtension = validate(ext, name);
2015-03-02 02:15:32 +08:00
if (validExtension.valid) {
extensions[name] = ext;
} else {
throw Error(validExtension.error);
}
2015-03-02 02:15:32 +08:00
}
};
2015-03-02 02:15:32 +08:00
/**
* Gets all extensions registered
* @returns {{}}
*/
showdown.getAllExtensions = function () {
2015-03-02 02:15:32 +08:00
'use strict';
return extensions;
};
2015-03-02 02:15:32 +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
/**
* 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
/**
* Validate extension
* @param {array} extension
* @param {string} name
* @returns {{valid: boolean, error: string}}
2015-01-16 05:21:33 +08:00
*/
function validate (extension, name) {
2015-01-19 19:37:21 +08:00
'use strict';
var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension',
ret = {
valid: true,
error: ''
};
if (!showdown.helper.isArray(extension)) {
extension = [extension];
2015-01-19 19:37:21 +08:00
}
2015-01-16 05:21:33 +08:00
for (var i = 0; i < extension.length; ++i) {
2015-07-13 12:09:03 +08:00
var baseMsg = errMsg + ' sub-extension ' + i + ': ',
ext = extension[i];
if (typeof ext !== 'object') {
ret.valid = false;
ret.error = baseMsg + 'must be an object, but ' + typeof ext + ' given';
return ret;
}
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;
}
var type = ext.type = ext.type.toLowerCase();
2015-03-02 02:15:32 +08:00
// normalize extension type
if (type === 'language') {
type = ext.type = 'lang';
}
2015-03-02 02:15:32 +08:00
if (type === 'html') {
type = ext.type = 'output';
}
2015-01-19 19:37:21 +08:00
if (type !== 'lang' && type !== 'output' && type !== 'listener') {
ret.valid = false;
ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang/language", "output/html" or "listener"';
return ret;
2015-01-16 05:21:33 +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;
}
}
}
}
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');
}
if (!(ext.regex instanceof RegExp)) {
ret.valid = false;
ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' + typeof ext.regex + ' given';
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
}
}
return ret;
}
/**
* Validate extension
* @param {object} ext
* @returns {boolean}
*/
showdown.validateExtension = function (ext) {
'use strict';
var validateExtension = validate(ext, null);
if (!validateExtension.valid) {
console.warn(validateExtension.error);
return false;
}
return true;
2015-01-16 05:21:33 +08:00
};