/** * Created by Tivie on 06-01-2015. */ // Private properties var showdown = {}, parsers = {}, extensions = {}, defaultOptions = { omitExtraWLInCodeBlocks: false, prefixHeaderId: false }, globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P /** * helper namespace * @type {{}} */ showdown.helper = {}; /** * TODO LEGACY SUPPORT CODE * @type {{}} */ showdown.extensions = {}; /** * Set a global option * @static * @param {string} key * @param {*} value * @returns {showdown} */ showdown.setOption = function (key, value) { 'use strict'; globalOptions[key] = value; return this; }; /** * Get a global option * @static * @param {string} key * @returns {*} */ showdown.getOption = function (key) { 'use strict'; return globalOptions[key]; }; /** * Get the global options * @static * @returns {{omitExtraWLInCodeBlocks: boolean, prefixHeaderId: boolean}} */ showdown.getOptions = function () { 'use strict'; return globalOptions; }; showdown.resetOptions = function () { 'use strict'; globalOptions = JSON.parse(JSON.stringify(defaultOptions)); }; /** * Get or set a subParser * * subParser(name) - Get a registered subParser * subParser(name, func) - Register a subParser * @static * @param {string} name * @param {function} [func] * @returns {*} */ showdown.subParser = function (name, func) { '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!'); } } } }; /** * Gets or registers an extension * @static * @param {string} name * @param {object|function=} ext * @returns {*} */ 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 if (showdown.helper.isUndefined(ext)) { if (!extensions.hasOwnProperty(name)) { throw Error('Extension named ' + name + ' is not registered!'); } return extensions[name]; // Setter } else { if (typeof ext === 'function') { ext = ext(); } var validExtension = validate(ext, name); if (validExtension.valid) { extensions[name] = ext; } else { throw Error(validExtension.error); } } }; /** * Gets all extensions registered * @returns {{}} */ showdown.getAllExtensions = function () { 'use strict'; return extensions; }; /** * Remove an extension * @param {string} name */ showdown.removeExtension = function (name) { 'use strict'; delete extensions[name]; }; /** * Removes all extensions */ showdown.resetExtensions = function () { 'use strict'; extensions = {}; }; /** * Validate extension * @param {object} ext * @param {string} name * @returns {{valid: boolean, error: string}} */ function validate(ext, name) { 'use strict'; var baseMsg = (name) ? 'Error in ' + name + ' extension: ' : 'Error in unnamed extension', ret = { valid: true, error: baseMsg }; if (typeof ext !== 'object') { ret.valid = false; ret.error = baseMsg + 'it 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(); // normalize extension type if (type === 'language') { type = ext.type = 'lang'; } if (type === 'html') { type = ext.type = 'output'; } if (type !== 'lang' && type !== 'output') { ret.valid = false; ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang" or "output"'; 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; } } else { ret.valid = false; ret.error = baseMsg + 'extensions must define either a "regex" property or a "filter" method'; return ret; } if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) { ret.valid = false; ret.error = baseMsg + 'output extensions must define a filter property'; return ret; } 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; };