123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /**
- * Created by Estevao on 31-05-2015.
- */
- /**
- * Showdown Converter class
- * @class
- * @param {object} [converterOptions]
- * @returns {
- * {makeHtml: Function},
- * {setOption: Function},
- * {getOption: Function},
- * {getOptions: Function}
- * }
- */
- showdown.Converter = function (converterOptions) {
- 'use strict';
- var
- /**
- * Options used by this converter
- * @private
- * @type {{}}
- */
- options = {
- omitExtraWLInCodeBlocks: false,
- prefixHeaderId: false,
- noHeaderId: false
- },
- /**
- * Language extensions used by this converter
- * @private
- * @type {Array}
- */
- langExtensions = [],
- /**
- * Output modifiers extensions used by this converter
- * @private
- * @type {Array}
- */
- outputModifiers = [],
- /**
- * The parser Order
- * @private
- * @type {string[]}
- */
- parserOrder = [
- 'githubCodeBlocks',
- 'hashHTMLBlocks',
- 'stripLinkDefinitions',
- 'blockGamut',
- 'unescapeSpecialChars'
- ];
- _constructor();
- /**
- * Converter constructor
- * @private
- */
- function _constructor() {
- converterOptions = converterOptions || {};
- for (var gOpt in globalOptions) {
- if (globalOptions.hasOwnProperty(gOpt)) {
- options[gOpt] = globalOptions[gOpt];
- }
- }
- // Merge options
- if (typeof converterOptions === 'object') {
- for (var opt in converterOptions) {
- if (converterOptions.hasOwnProperty(opt)) {
- options[opt] = converterOptions[opt];
- }
- }
- } else {
- throw Error('Converter expects the passed parameter to be an object, but ' + typeof converterOptions +
- ' was passed instead.');
- }
- if (options.extensions) {
- showdown.helper.forEach(options.extensions, _parseExtension);
- }
- }
- /**
- * Parse extension
- * @param {*} ext
- * @private
- */
- function _parseExtension(ext) {
- // If it's a string, the extension was previously loaded
- if (showdown.helper.isString(ext)) {
- ext = showdown.helper.stdExtName(ext);
- // LEGACY_SUPPORT CODE
- if (showdown.extensions[ext]) {
- console.warn('DEPRECATION WARNING: ' + ext + ' is an old extension that uses a deprecated loading method.' +
- 'Please inform the developer that the extension should be updated!');
- legacyExtensionLoading(showdown.extensions[ext], ext);
- return;
- // END LEGACY SUPPORT CODE
- } else if (!showdown.helper.isUndefined(extensions[ext])) {
- ext = extensions[ext];
- } else {
- throw Error('Extension "' + ext + '" could not be loaded. It was either not found or is not a valid extension.');
- }
- }
- if (typeof ext === 'function') {
- ext = ext();
- }
- if (!showdown.helper.isArray(ext)) {
- ext = [ext];
- }
- if (!showdown.validateExtension(ext)) {
- return;
- }
- for (var i = 0; i < ext.length; ++i) {
- switch (ext[i].type) {
- case 'lang':
- langExtensions.push(ext[i]);
- break;
- case 'output':
- outputModifiers.push(ext[i]);
- break;
- default:
- // should never reach here
- throw Error('Extension loader error: Type unrecognized!!!');
- }
- }
- }
- /**
- * LEGACY_SUPPORT
- * @param {*} ext
- * @param {string} name
- */
- function legacyExtensionLoading(ext, name) {
- if (typeof ext === 'function') {
- ext = ext(new showdown.Converter());
- }
- if (!showdown.helper.isArray(ext)) {
- ext = [ext];
- }
- var valid = validate(ext, name);
- if (!valid.valid) {
- throw Error(valid.error);
- }
- for (var i = 0; i < ext.length; ++i) {
- switch (ext[i].type) {
- case 'lang':
- langExtensions.push(ext[i]);
- break;
- case 'output':
- outputModifiers.push(ext[i]);
- break;
- default:// should never reach here
- throw Error('Extension loader error: Type unrecognized!!!');
- }
- }
- }
- /**
- * Converts a markdown string into HTML
- * @param {string} text
- * @returns {*}
- */
- this.makeHtml = function (text) {
- //check if text is not falsy
- if (!text) {
- return text;
- }
- var globals = {
- gHtmlBlocks: [],
- gUrls: {},
- gTitles: {},
- gDimensions: {},
- gListLevel: 0,
- hashLinkCounts: {},
- langExtensions: langExtensions,
- outputModifiers: outputModifiers,
- converter: this
- };
- // attacklab: Replace ~ with ~T
- // This lets us use tilde as an escape char to avoid md5 hashes
- // The choice of character is arbitrary; anything that isn't
- // magic in Markdown will work.
- text = text.replace(/~/g, '~T');
- // attacklab: Replace $ with ~D
- // RegExp interprets $ as a special character
- // when it's in a replacement string
- text = text.replace(/\$/g, '~D');
- // Standardize line endings
- text = text.replace(/\r\n/g, '\n'); // DOS to Unix
- text = text.replace(/\r/g, '\n'); // Mac to Unix
- // Make sure text begins and ends with a couple of newlines:
- text = '\n\n' + text + '\n\n';
- // detab
- text = showdown.subParser('detab')(text, options, globals);
- // stripBlankLines
- text = showdown.subParser('stripBlankLines')(text, options, globals);
- //run languageExtensions
- showdown.helper.forEach(langExtensions, function (ext) {
- text = showdown.subParser('runExtension')(ext, text, options, globals);
- });
- // Run all registered parsers
- for (var i = 0; i < parserOrder.length; ++i) {
- var name = parserOrder[i];
- text = parsers[name](text, options, globals);
- }
- // attacklab: Restore dollar signs
- text = text.replace(/~D/g, '$$');
- // attacklab: Restore tildes
- text = text.replace(/~T/g, '~');
- // Run output modifiers
- showdown.helper.forEach(outputModifiers, function (ext) {
- text = showdown.subParser('runExtension')(ext, text, options, globals);
- });
- return text;
- };
- /**
- * Set an option of this Converter instance
- * @param {string} key
- * @param {*} value
- */
- this.setOption = function (key, value) {
- options[key] = value;
- };
- /**
- * Get the option of this Converter instance
- * @param {string} key
- * @returns {*}
- */
- this.getOption = function (key) {
- return options[key];
- };
- /**
- * Get the options of this Converter instance
- * @returns {{}}
- */
- this.getOptions = function () {
- return options;
- };
- /**
- * Add extension to THIS converter
- * @param {{}} extension
- */
- this.addExtension = function (extension) {
- _parseExtension(extension);
- };
- /**
- * Use a global registered extension with THIS converter
- * @param {string} extensionName Name of the previously registered extension
- */
- this.useExtension = function (extensionName) {
- _parseExtension(extensionName);
- };
- /**
- * Remove an extension from THIS converter.
- * Note: This is a costly operation. It's better to initialize a new converter
- * and specify the extensions you wish to use
- * @param {Array} extension
- */
- this.removeExtension = function (extension) {
- if (!showdown.helper.isArray(extension)) {
- extension = [extension];
- }
- for (var a = 0; a < extension.length; ++a) {
- var ext = extension[a];
- for (var i = 0; i < langExtensions.length; ++i) {
- if (langExtensions[i] === ext) {
- langExtensions[i].splice(i, 1);
- }
- }
- for (var ii = 0; ii < outputModifiers.length; ++i) {
- if (outputModifiers[ii] === ext) {
- outputModifiers[ii].splice(i, 1);
- }
- }
- }
- };
- /**
- * Get all extension of THIS converter
- * @returns {{language: Array, output: Array}}
- */
- this.getAllExtensions = function () {
- return {
- language: langExtensions,
- output: outputModifiers
- };
- };
- };
|