2017-01-28 09:19:07 +08:00
|
|
|
;/*! showdown 28-01-2017 */
|
2016-06-21 09:03:51 +08:00
|
|
|
(function(){
|
2015-07-13 12:09:03 +08:00
|
|
|
/**
|
|
|
|
* Created by Tivie on 13-07-2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getDefaultOpts(simple) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var defaultOptions = {
|
|
|
|
omitExtraWLInCodeBlocks: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Omit the default extra whiteline added to code blocks',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
noHeaderId: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off generated header id',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
prefixHeaderId: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Specify a prefix to generated header ids',
|
|
|
|
type: 'string'
|
|
|
|
},
|
2016-12-31 03:01:44 +08:00
|
|
|
ghCompatibleHeaderId: {
|
|
|
|
defaultValue: false,
|
2016-12-31 03:46:46 +08:00
|
|
|
describe: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
|
2017-01-06 10:42:47 +08:00
|
|
|
type: 'boolean'
|
2016-12-31 03:01:44 +08:00
|
|
|
},
|
2015-07-13 12:09:03 +08:00
|
|
|
headerLevelStart: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'The header blocks level start',
|
|
|
|
type: 'integer'
|
|
|
|
},
|
|
|
|
parseImgDimensions: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off image dimension parsing',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
simplifiedAutoLink: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off GFM autolink style',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
2016-12-01 23:25:46 +08:00
|
|
|
excludeTrailingPunctuationFromURLs: {
|
|
|
|
defaultValue: false,
|
|
|
|
describe: 'Excludes trailing punctuation from links generated with autoLinking',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
2015-07-13 12:09:03 +08:00
|
|
|
literalMidWordUnderscores: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Parse midword underscores as literal underscores',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
strikethrough: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off strikethrough support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tables: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off tables support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tablesHeaderId: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Add an id to table headers',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
ghCodeBlocks: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: true,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off GFM fenced code blocks support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tasklists: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-13 12:09:03 +08:00
|
|
|
describe: 'Turn on/off GFM tasklist support',
|
|
|
|
type: 'boolean'
|
2015-07-15 00:10:52 +08:00
|
|
|
},
|
|
|
|
smoothLivePreview: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2015-07-15 00:10:52 +08:00
|
|
|
describe: 'Prevents weird effects in live previews due to incomplete input',
|
|
|
|
type: 'boolean'
|
2016-06-07 08:23:52 +08:00
|
|
|
},
|
|
|
|
smartIndentationFix: {
|
2016-07-21 05:26:15 +08:00
|
|
|
defaultValue: false,
|
2016-11-11 15:57:56 +08:00
|
|
|
description: 'Tries to smartly fix indentation in es6 strings',
|
2016-06-07 08:23:52 +08:00
|
|
|
type: 'boolean'
|
2016-11-11 16:15:24 +08:00
|
|
|
},
|
|
|
|
disableForced4SpacesIndentedSublists: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: 'Disables the requirement of indenting nested sublists by 4 spaces',
|
|
|
|
type: 'boolean'
|
2016-12-01 02:04:17 +08:00
|
|
|
},
|
|
|
|
simpleLineBreaks: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: 'Parses simple line breaks as <br> (GFM Style)',
|
|
|
|
type: 'boolean'
|
2016-12-17 14:01:15 +08:00
|
|
|
},
|
|
|
|
requireSpaceBeforeHeadingText: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
|
|
|
|
type: 'boolean'
|
2017-01-06 12:33:12 +08:00
|
|
|
},
|
|
|
|
ghMentions: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: 'Enables github @mentions',
|
|
|
|
type: 'boolean'
|
2017-01-28 03:03:37 +08:00
|
|
|
},
|
|
|
|
encodeEmails: {
|
|
|
|
defaultValue: true,
|
|
|
|
description: 'Encode e-mail addresses through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities',
|
|
|
|
type: 'boolean'
|
2015-07-13 12:09:03 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (simple === false) {
|
|
|
|
return JSON.parse(JSON.stringify(defaultOptions));
|
|
|
|
}
|
|
|
|
var ret = {};
|
|
|
|
for (var opt in defaultOptions) {
|
|
|
|
if (defaultOptions.hasOwnProperty(opt)) {
|
2016-07-21 05:26:15 +08:00
|
|
|
ret[opt] = defaultOptions[opt].defaultValue;
|
2015-07-13 12:09:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-12-23 17:29:58 +08:00
|
|
|
|
|
|
|
function allOptionsOn() {
|
|
|
|
'use strict';
|
|
|
|
var options = getDefaultOpts(true),
|
|
|
|
ret = {};
|
|
|
|
for (var opt in options) {
|
|
|
|
if (options.hasOwnProperty(opt)) {
|
|
|
|
ret[opt] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-21 09:03:51 +08:00
|
|
|
|
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,
|
|
|
|
prefixHeaderId: 'user-content-',
|
|
|
|
simplifiedAutoLink: true,
|
2016-12-01 23:25:46 +08:00
|
|
|
excludeTrailingPunctuationFromURLs: true,
|
2016-11-11 16:15:24 +08:00
|
|
|
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,
|
|
|
|
ghMentions: true
|
2015-07-12 09:15:35 +08:00
|
|
|
},
|
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-03-02 02:15:32 +08:00
|
|
|
/**
|
|
|
|
* Set a global option
|
|
|
|
* @static
|
|
|
|
* @param {string} key
|
2015-05-27 23:36:53 +08:00
|
|
|
* @param {*} value
|
2015-03-02 02:15:32 +08:00
|
|
|
* @returns {showdown}
|
|
|
|
*/
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.setOption = function (key, value) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
globalOptions[key] = value;
|
|
|
|
return this;
|
2015-01-16 05:21:33 +08:00
|
|
|
};
|
|
|
|
|
2015-03-02 02:15:32 +08:00
|
|
|
/**
|
|
|
|
* Get a global option
|
|
|
|
* @static
|
|
|
|
* @param {string} key
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2015-01-18 10:12:32 +08:00
|
|
|
showdown.getOption = function (key) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
return globalOptions[key];
|
2015-01-18 10:12:32 +08:00
|
|
|
};
|
|
|
|
|
2015-03-02 02:15:32 +08:00
|
|
|
/**
|
|
|
|
* Get the global options
|
|
|
|
* @static
|
2015-06-15 21:56:47 +08:00
|
|
|
* @returns {{}}
|
2015-03-02 02:15:32 +08:00
|
|
|
*/
|
2015-01-18 10:12:32 +08:00
|
|
|
showdown.getOptions = function () {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
return globalOptions;
|
2015-01-18 10:12:32 +08:00
|
|
|
};
|
|
|
|
|
2015-06-15 21:56:47 +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');
|
|
|
|
}
|
|
|
|
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:56:47 +08:00
|
|
|
/**
|
|
|
|
* Get the default options
|
|
|
|
* @static
|
2015-07-13 12:09:03 +08:00
|
|
|
* @param {boolean} [simple=true]
|
2015-06-15 21:56:47 +08:00
|
|
|
* @returns {{}}
|
|
|
|
*/
|
2015-07-13 12:09:03 +08:00
|
|
|
showdown.getDefaultOptions = function (simple) {
|
2015-06-15 21:56:47 +08:00
|
|
|
'use strict';
|
2015-07-13 12:09:03 +08:00
|
|
|
return getDefaultOpts(simple);
|
2015-06-15 21:56:47 +08:00
|
|
|
};
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-03-02 02:15:32 +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 20:04:22 +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
|
|
|
}
|
2015-01-19 20:04:22 +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
|
|
|
|
* @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);
|
|
|
|
|
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-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-06-01 03:56:28 +08:00
|
|
|
* Removes all extensions
|
2015-01-16 05:21:33 +08:00
|
|
|
*/
|
2015-06-01 03:56:28 +08:00
|
|
|
showdown.resetExtensions = function () {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-06-01 03:56:28 +08:00
|
|
|
extensions = {};
|
|
|
|
};
|
2015-01-19 20:04:22 +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-06-08 02:02:45 +08:00
|
|
|
function validate(extension, name) {
|
2015-06-01 03:56:28 +08:00
|
|
|
'use strict';
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension',
|
2015-06-01 03:56:28 +08:00
|
|
|
ret = {
|
|
|
|
valid: true,
|
2015-06-08 02:02:45 +08:00
|
|
|
error: ''
|
2015-06-01 03:56:28 +08:00
|
|
|
};
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (!showdown.helper.isArray(extension)) {
|
|
|
|
extension = [extension];
|
2015-05-27 02:46:07 +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-01-16 05:21:33 +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-03-02 02:15:32 +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 20:04:22 +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');
|
|
|
|
}
|
|
|
|
if (!ext.regex instanceof RegExp) {
|
|
|
|
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 20:04:22 +08:00
|
|
|
}
|
2015-03-02 02:15:32 +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
|
|
|
};
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
2015-01-19 23:42:20 +08:00
|
|
|
* showdownjs helper functions
|
2015-01-16 05:21:33 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-19 23:42:20 +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 23:42:20 +08:00
|
|
|
* @param {string} a
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-01-28 09:19:07 +08:00
|
|
|
showdown.helper.isString = function (a) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
return (typeof a === 'string' || a instanceof String);
|
2015-01-19 23:42:20 +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 23:42:20 +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 23:42:20 +08:00
|
|
|
'use strict';
|
|
|
|
return typeof value === 'undefined';
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-01-19 23:42:20 +08:00
|
|
|
function escapeCharactersCallback(wholeMatch, m1) {
|
|
|
|
'use strict';
|
|
|
|
var charCodeToEscape = m1.charCodeAt(0);
|
|
|
|
return '~E' + charCodeToEscape + 'E';
|
|
|
|
}
|
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}
|
|
|
|
*/
|
|
|
|
showdown.helper.escapeCharactersCallback = escapeCharactersCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 23:42:20 +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') + '])';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 23:42:20 +08:00
|
|
|
if (afterBackslash) {
|
|
|
|
regexString = '\\\\' + regexString;
|
|
|
|
}
|
|
|
|
|
|
|
|
var regex = new RegExp(regexString, 'g');
|
|
|
|
text = text.replace(regex, escapeCharactersCallback);
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
var rgxFindMatchPos = function (str, left, right, flags) {
|
|
|
|
'use strict';
|
|
|
|
var f = flags || '',
|
|
|
|
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;
|
|
|
|
|
|
|
|
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),
|
|
|
|
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-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-06-01 03:56:28 +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-06-01 03:56:28 +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-06-01 03:56:28 +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
|
|
|
|
};
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* Created by Estevao on 31-05-2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Showdown Converter class
|
|
|
|
* @class
|
|
|
|
* @param {object} [converterOptions]
|
2015-08-23 09:21:00 +08:00
|
|
|
* @returns {Converter}
|
2015-06-01 03:56:28 +08:00
|
|
|
*/
|
|
|
|
showdown.Converter = function (converterOptions) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var
|
|
|
|
/**
|
|
|
|
* Options used by this converter
|
|
|
|
* @private
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
2015-07-11 22:45:58 +08:00
|
|
|
options = {},
|
2015-06-01 03:56:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Language extensions used by this converter
|
|
|
|
* @private
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
langExtensions = [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output modifiers extensions used by this converter
|
|
|
|
* @private
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
outputModifiers = [],
|
|
|
|
|
|
|
|
/**
|
2015-10-19 10:20:20 +08:00
|
|
|
* Event listeners
|
2015-06-01 03:56:28 +08:00
|
|
|
* @private
|
2015-10-19 10:20:20 +08:00
|
|
|
* @type {{}}
|
2015-06-01 03:56:28 +08:00
|
|
|
*/
|
2017-01-09 03:09:12 +08:00
|
|
|
listeners = {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The flavor set in this converter
|
|
|
|
*/
|
|
|
|
setConvFlavor = setFlavor;
|
2015-06-01 03:56:28 +08:00
|
|
|
|
|
|
|
_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];
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 02:02:45 +08:00
|
|
|
} else {
|
|
|
|
throw Error('Converter expects the passed parameter to be an object, but ' + typeof converterOptions +
|
|
|
|
' was passed instead.');
|
2015-06-01 03:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.extensions) {
|
|
|
|
showdown.helper.forEach(options.extensions, _parseExtension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse extension
|
|
|
|
* @param {*} ext
|
2015-07-13 12:09:03 +08:00
|
|
|
* @param {string} [name='']
|
2015-06-01 03:56:28 +08:00
|
|
|
* @private
|
|
|
|
*/
|
2015-07-13 12:09:03 +08:00
|
|
|
function _parseExtension(ext, name) {
|
2015-06-01 03:56:28 +08:00
|
|
|
|
2015-07-13 12:09:03 +08:00
|
|
|
name = name || null;
|
2015-06-01 03:56:28 +08:00
|
|
|
// If it's a string, the extension was previously loaded
|
|
|
|
if (showdown.helper.isString(ext)) {
|
|
|
|
ext = showdown.helper.stdExtName(ext);
|
2015-07-13 12:09:03 +08:00
|
|
|
name = ext;
|
2015-06-01 03:56:28 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
// LEGACY_SUPPORT CODE
|
|
|
|
if (showdown.extensions[ext]) {
|
|
|
|
console.warn('DEPRECATION WARNING: ' + ext + ' is an old extension that uses a deprecated loading method.' +
|
2015-06-01 03:56:28 +08:00
|
|
|
'Please inform the developer that the extension should be updated!');
|
2015-06-08 02:02:45 +08:00
|
|
|
legacyExtensionLoading(showdown.extensions[ext], ext);
|
|
|
|
return;
|
2015-06-01 03:56:28 +08:00
|
|
|
// 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.');
|
|
|
|
}
|
2015-06-08 02:02:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof ext === 'function') {
|
2015-06-01 03:56:28 +08:00
|
|
|
ext = ext();
|
|
|
|
}
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
if (!showdown.helper.isArray(ext)) {
|
|
|
|
ext = [ext];
|
|
|
|
}
|
|
|
|
|
2015-07-13 12:09:03 +08:00
|
|
|
var validExt = validate(ext, name);
|
|
|
|
if (!validExt.valid) {
|
|
|
|
throw Error(validExt.error);
|
2015-06-01 03:56:28 +08:00
|
|
|
}
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
for (var i = 0; i < ext.length; ++i) {
|
|
|
|
switch (ext[i].type) {
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
case 'lang':
|
|
|
|
langExtensions.push(ext[i]);
|
|
|
|
break;
|
2015-06-01 03:56:28 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
case 'output':
|
|
|
|
outputModifiers.push(ext[i]);
|
|
|
|
break;
|
2015-08-03 10:47:49 +08:00
|
|
|
}
|
2016-12-01 23:48:30 +08:00
|
|
|
if (ext[i].hasOwnProperty('listeners')) {
|
2015-08-03 10:47:49 +08:00
|
|
|
for (var ln in ext[i].listeners) {
|
|
|
|
if (ext[i].listeners.hasOwnProperty(ln)) {
|
|
|
|
listen(ln, ext[i].listeners[ln]);
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 02:02:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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!!!');
|
|
|
|
}
|
2015-06-01 03:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
/**
|
|
|
|
* Listen to an event
|
|
|
|
* @param {string} name
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
function listen(name, callback) {
|
|
|
|
if (!showdown.helper.isString(name)) {
|
|
|
|
throw Error('Invalid argument in converter.listen() method: name must be a string, but ' + typeof name + ' given');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw Error('Invalid argument in converter.listen() method: callback must be a function, but ' + typeof callback + ' given');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!listeners.hasOwnProperty(name)) {
|
|
|
|
listeners[name] = [];
|
|
|
|
}
|
|
|
|
listeners[name].push(callback);
|
|
|
|
}
|
|
|
|
|
2016-06-07 08:23:52 +08:00
|
|
|
function rTrimInputText(text) {
|
|
|
|
var rsp = text.match(/^\s*/)[0].length,
|
|
|
|
rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm');
|
|
|
|
return text.replace(rgx, '');
|
|
|
|
}
|
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
/**
|
|
|
|
* Dispatch an event
|
|
|
|
* @private
|
|
|
|
* @param {string} evtName Event name
|
|
|
|
* @param {string} text Text
|
|
|
|
* @param {{}} options Converter Options
|
2016-03-21 01:08:44 +08:00
|
|
|
* @param {{}} globals
|
2015-08-03 10:47:49 +08:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2016-03-21 01:08:44 +08:00
|
|
|
this._dispatch = function dispatch (evtName, text, options, globals) {
|
2015-08-03 10:47:49 +08:00
|
|
|
if (listeners.hasOwnProperty(evtName)) {
|
|
|
|
for (var ei = 0; ei < listeners[evtName].length; ++ei) {
|
2016-03-21 01:08:44 +08:00
|
|
|
var nText = listeners[evtName][ei](evtName, text, this, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
if (nText && typeof nText !== 'undefined') {
|
|
|
|
text = nText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to an event
|
|
|
|
* @param {string} name
|
|
|
|
* @param {function} callback
|
|
|
|
* @returns {showdown.Converter}
|
|
|
|
*/
|
|
|
|
this.listen = function (name, callback) {
|
|
|
|
listen(name, callback);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* 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: [],
|
2016-01-02 09:08:17 +08:00
|
|
|
gHtmlMdBlocks: [],
|
2015-10-19 08:55:35 +08:00
|
|
|
gHtmlSpans: [],
|
2015-06-01 03:56:28 +08:00
|
|
|
gUrls: {},
|
|
|
|
gTitles: {},
|
2015-06-17 08:22:05 +08:00
|
|
|
gDimensions: {},
|
2015-06-01 03:56:28 +08:00
|
|
|
gListLevel: 0,
|
|
|
|
hashLinkCounts: {},
|
|
|
|
langExtensions: langExtensions,
|
|
|
|
outputModifiers: outputModifiers,
|
2016-01-25 09:04:06 +08:00
|
|
|
converter: this,
|
|
|
|
ghCodeBlocks: []
|
2015-06-01 03:56:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2016-12-17 13:26:15 +08:00
|
|
|
// Stardardize line spaces (nbsp causes trouble in older browsers and some regex flavors)
|
|
|
|
text = text.replace(/\u00A0/g, ' ');
|
|
|
|
|
2016-06-07 08:23:52 +08:00
|
|
|
if (options.smartIndentationFix) {
|
|
|
|
text = rTrimInputText(text);
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
// run the sub parsers
|
2016-01-25 11:01:54 +08:00
|
|
|
text = showdown.subParser('hashPreCodeTags')(text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
text = showdown.subParser('githubCodeBlocks')(text, options, globals);
|
|
|
|
text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
|
2015-10-19 10:20:20 +08:00
|
|
|
text = showdown.subParser('hashHTMLSpans')(text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
text = showdown.subParser('stripLinkDefinitions')(text, options, globals);
|
|
|
|
text = showdown.subParser('blockGamut')(text, options, globals);
|
2015-10-19 10:20:20 +08:00
|
|
|
text = showdown.subParser('unhashHTMLSpans')(text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
text = showdown.subParser('unescapeSpecialChars')(text, options, globals);
|
2015-06-01 03:56:28 +08:00
|
|
|
|
|
|
|
// 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
|
2015-07-13 12:09:03 +08:00
|
|
|
* @param {string} [name=null]
|
2015-06-01 03:56:28 +08:00
|
|
|
*/
|
2015-07-13 12:09:03 +08:00
|
|
|
this.addExtension = function (extension, name) {
|
|
|
|
name = name || null;
|
|
|
|
_parseExtension(extension, name);
|
2015-06-01 03:56:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-08 02:02:45 +08:00
|
|
|
* Use a global registered extension with THIS converter
|
|
|
|
* @param {string} extensionName Name of the previously registered extension
|
|
|
|
*/
|
|
|
|
this.useExtension = function (extensionName) {
|
|
|
|
_parseExtension(extensionName);
|
|
|
|
};
|
|
|
|
|
2015-07-12 09:15:35 +08:00
|
|
|
/**
|
|
|
|
* Set the flavor THIS converter should use
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
this.setFlavor = function (name) {
|
2017-01-09 03:09:12 +08:00
|
|
|
if (!flavor.hasOwnProperty(name)) {
|
|
|
|
throw Error(name + ' flavor was not found');
|
|
|
|
}
|
|
|
|
var preset = flavor[name];
|
|
|
|
setConvFlavor = name;
|
|
|
|
for (var option in preset) {
|
|
|
|
if (preset.hasOwnProperty(option)) {
|
|
|
|
options[option] = preset[option];
|
2015-07-12 09:15:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-09 03:09:12 +08:00
|
|
|
/**
|
|
|
|
* Get the currently set flavor of this converter
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
this.getFlavor = function () {
|
|
|
|
return setConvFlavor;
|
|
|
|
};
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2015-06-01 03:56:28 +08:00
|
|
|
*/
|
|
|
|
this.removeExtension = function (extension) {
|
2015-06-08 02:02:45 +08:00
|
|
|
if (!showdown.helper.isArray(extension)) {
|
|
|
|
extension = [extension];
|
2015-06-01 03:56:28 +08:00
|
|
|
}
|
2015-06-08 02:02:45 +08:00
|
|
|
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);
|
|
|
|
}
|
2015-06-01 03:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all extension of THIS converter
|
|
|
|
* @returns {{language: Array, output: Array}}
|
|
|
|
*/
|
|
|
|
this.getAllExtensions = function () {
|
|
|
|
return {
|
|
|
|
language: langExtensions,
|
|
|
|
output: outputModifiers
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
|
|
|
*/
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('anchors', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('anchors.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
var writeAnchorTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
|
|
|
|
if (showdown.helper.isUndefined(m7)) {
|
|
|
|
m7 = '';
|
|
|
|
}
|
|
|
|
wholeMatch = m1;
|
|
|
|
var linkText = m2,
|
|
|
|
linkId = m3.toLowerCase(),
|
|
|
|
url = m4,
|
|
|
|
title = m7;
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
if (!linkId) {
|
|
|
|
// lower-case and turn embedded newlines into spaces
|
|
|
|
linkId = linkText.toLowerCase().replace(/ ?\n/g, ' ');
|
|
|
|
}
|
|
|
|
url = '#' + linkId;
|
|
|
|
|
|
|
|
if (!showdown.helper.isUndefined(globals.gUrls[linkId])) {
|
|
|
|
url = globals.gUrls[linkId];
|
|
|
|
if (!showdown.helper.isUndefined(globals.gTitles[linkId])) {
|
|
|
|
title = globals.gTitles[linkId];
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
} else {
|
|
|
|
if (wholeMatch.search(/\(\s*\)$/m) > -1) {
|
|
|
|
// Special case for explicit empty url
|
|
|
|
url = '';
|
|
|
|
} else {
|
|
|
|
return wholeMatch;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-01-28 10:46:34 +08:00
|
|
|
//url = showdown.helper.escapeCharacters(url, '*_', false); // replaced line to improve performance
|
|
|
|
url = url.replace(showdown.helper.regexes.asteriskAndDash, showdown.helper.escapeCharactersCallback);
|
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
var result = '<a href="' + url + '"';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
if (title !== '' && title !== null) {
|
|
|
|
title = title.replace(/"/g, '"');
|
2017-01-28 10:46:34 +08:00
|
|
|
//title = showdown.helper.escapeCharacters(title, '*_', false); // replaced line to improve performance
|
|
|
|
title = title.replace(showdown.helper.regexes.asteriskAndDash, showdown.helper.escapeCharactersCallback);
|
2015-01-19 20:04:22 +08:00
|
|
|
result += ' title="' + title + '"';
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
result += '>' + linkText + '</a>';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// First, handle reference-style links: [link text] [id]
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// Next, inline-style links: [link text](url "optional title")
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
|
2015-01-19 20:04:22 +08:00
|
|
|
writeAnchorTag);
|
|
|
|
|
2017-01-06 12:33:12 +08:00
|
|
|
// handle reference-style shortcuts: [link text]
|
2015-01-19 20:04:22 +08:00
|
|
|
// These must come last in case you've also got [link test][1]
|
|
|
|
// or [link test](/foo)
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2017-01-06 12:33:12 +08:00
|
|
|
// Lastly handle GithubMentions if option is enabled
|
|
|
|
if (options.ghMentions) {
|
2017-01-06 12:58:28 +08:00
|
|
|
text = text.replace(/(^|\s)(\\)?(@([a-z\d\-]+))(?=[.!?;,[\]()]|\s|$)/gmi, function (wm, st, escape, mentions, username) {
|
|
|
|
if (escape === '\\') {
|
|
|
|
return st + mentions;
|
|
|
|
}
|
|
|
|
return st + '<a href="https://www.github.com/' + username + '">' + mentions + '</a>';
|
|
|
|
});
|
2017-01-06 12:33:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('anchors.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('autoLinks', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('autoLinks.before', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-12-01 23:25:46 +08:00
|
|
|
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
|
|
|
|
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
|
2015-07-11 09:42:53 +08:00
|
|
|
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
|
2017-01-28 03:25:46 +08:00
|
|
|
simpleMailRegex = /(^|\s)(?:mailto:)?([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?=$|\s)/gmi,
|
|
|
|
delimMailRegex = /<()(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
|
2015-07-11 09:42:53 +08:00
|
|
|
|
2016-08-20 02:12:25 +08:00
|
|
|
text = text.replace(delimUrlRegex, replaceLink);
|
2015-07-11 09:42:53 +08:00
|
|
|
text = text.replace(delimMailRegex, replaceMail);
|
2016-08-20 02:12:25 +08:00
|
|
|
// simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
|
2015-01-19 20:04:22 +08:00
|
|
|
// Email addresses: <address@domain.foo>
|
|
|
|
|
2015-07-11 09:42:53 +08:00
|
|
|
if (options.simplifiedAutoLink) {
|
2016-12-01 23:25:46 +08:00
|
|
|
if (options.excludeTrailingPunctuationFromURLs) {
|
|
|
|
text = text.replace(simpleURLRegex2, replaceLink);
|
|
|
|
} else {
|
|
|
|
text = text.replace(simpleURLRegex, replaceLink);
|
|
|
|
}
|
2015-08-11 12:57:52 +08:00
|
|
|
text = text.replace(simpleMailRegex, replaceMail);
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 23:25:46 +08:00
|
|
|
function replaceLink(wm, link, m2, m3, trailingPunctuation) {
|
|
|
|
var lnkTxt = link,
|
|
|
|
append = '';
|
2016-08-20 02:12:25 +08:00
|
|
|
if (/^www\./i.test(link)) {
|
|
|
|
link = link.replace(/^www\./i, 'http://www.');
|
|
|
|
}
|
2016-12-01 23:25:46 +08:00
|
|
|
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
|
|
|
|
append = trailingPunctuation;
|
|
|
|
}
|
|
|
|
return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
|
2016-08-20 02:12:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-28 03:25:46 +08:00
|
|
|
function replaceMail(wholeMatch, b, mail) {
|
2017-01-28 03:03:37 +08:00
|
|
|
var href = 'mailto:';
|
2017-01-28 03:25:46 +08:00
|
|
|
b = b || '';
|
2017-01-28 03:03:37 +08:00
|
|
|
mail = showdown.subParser('unescapeSpecialChars')(mail);
|
|
|
|
if (options.encodeEmails) {
|
|
|
|
mail = showdown.helper.encodeEmailAddress(mail);
|
|
|
|
href = showdown.helper.encodeEmailAddress(href + mail);
|
|
|
|
} else {
|
|
|
|
href = href + mail;
|
|
|
|
}
|
2017-01-28 03:25:46 +08:00
|
|
|
return b + '<a href="' + href + '">' + mail + '</a>';
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('autoLinks.after', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* These are all the transformations that form block-level
|
|
|
|
* tags like paragraphs, headers, and list items.
|
|
|
|
*/
|
|
|
|
showdown.subParser('blockGamut', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('blockGamut.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-08-23 10:11:11 +08:00
|
|
|
// we parse blockquotes first so that we can have headings and hrs
|
|
|
|
// inside blockquotes
|
|
|
|
text = showdown.subParser('blockQuotes')(text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
text = showdown.subParser('headers')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Do Horizontal Rules:
|
2017-01-28 10:17:19 +08:00
|
|
|
text = showdown.subParser('horizontalRule')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text = showdown.subParser('lists')(text, options, globals);
|
|
|
|
text = showdown.subParser('codeBlocks')(text, options, globals);
|
2015-08-27 10:41:59 +08:00
|
|
|
text = showdown.subParser('tables')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// We already ran _HashHTMLBlocks() before, in Markdown(), but that
|
|
|
|
// was to escape raw HTML in the original Markdown source. This time,
|
|
|
|
// we're escaping the markup we've just created, so that we don't wrap
|
|
|
|
// <p> tags around block-level tags.
|
|
|
|
text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
|
|
|
|
text = showdown.subParser('paragraphs')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('blockGamut.after', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
return text;
|
2015-01-19 23:42:20 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('blockQuotes', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('blockQuotes.before', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
text = text.replace(/((^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var bq = m1;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: hack around Konqueror 3.5.4 bug:
|
|
|
|
// "----------bug".replace(/^-/g,"") == "bug"
|
|
|
|
bq = bq.replace(/^[ \t]*>[ \t]?/gm, '~0'); // trim one level of quoting
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: clean up hack
|
|
|
|
bq = bq.replace(/~0/g, '');
|
|
|
|
|
|
|
|
bq = bq.replace(/^[ \t]+$/gm, ''); // trim whitespace-only lines
|
2015-08-26 04:10:49 +08:00
|
|
|
bq = showdown.subParser('githubCodeBlocks')(bq, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
bq = showdown.subParser('blockGamut')(bq, options, globals); // recurse
|
|
|
|
|
|
|
|
bq = bq.replace(/(^|\n)/g, '$1 ');
|
|
|
|
// These leading spaces screw with <pre> content, so we need to fix that:
|
|
|
|
bq = bq.replace(/(\s*<pre>[^\r]+?<\/pre>)/gm, function (wholeMatch, m1) {
|
|
|
|
var pre = m1;
|
|
|
|
// attacklab: hack around Konqueror 3.5.4 bug:
|
|
|
|
pre = pre.replace(/^ /mg, '~0');
|
|
|
|
pre = pre.replace(/~0/g, '');
|
|
|
|
return pre;
|
|
|
|
});
|
|
|
|
|
|
|
|
return showdown.subParser('hashBlock')('<blockquote>\n' + bq + '\n</blockquote>', options, globals);
|
|
|
|
});
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('blockQuotes.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Process Markdown `<pre><code>` blocks.
|
|
|
|
*/
|
|
|
|
showdown.subParser('codeBlocks', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
// sentinel workarounds for lack of \A and \Z, safari\khtml bug
|
2015-01-19 20:04:22 +08:00
|
|
|
text += '~0';
|
|
|
|
|
|
|
|
var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
|
|
|
|
text = text.replace(pattern, function (wholeMatch, m1, m2) {
|
2015-05-27 08:37:01 +08:00
|
|
|
var codeblock = m1,
|
|
|
|
nextChar = m2,
|
|
|
|
end = '\n';
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
codeblock = showdown.subParser('outdent')(codeblock);
|
|
|
|
codeblock = showdown.subParser('encodeCode')(codeblock);
|
|
|
|
codeblock = showdown.subParser('detab')(codeblock);
|
|
|
|
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
|
2015-05-27 08:37:01 +08:00
|
|
|
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
|
|
|
|
|
|
|
|
if (options.omitExtraWLInCodeBlocks) {
|
|
|
|
end = '';
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-05-27 08:37:01 +08:00
|
|
|
codeblock = '<pre><code>' + codeblock + end + '</code></pre>';
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
|
|
|
|
});
|
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
// strip sentinel
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(/~0/, '');
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeBlocks.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* * Backtick quotes are used for <code></code> spans.
|
|
|
|
*
|
|
|
|
* * You can use multiple backticks as the delimiters if you want to
|
|
|
|
* include literal backticks in the code span. So, this input:
|
|
|
|
*
|
|
|
|
* Just type ``foo `bar` baz`` at the prompt.
|
|
|
|
*
|
|
|
|
* Will translate to:
|
|
|
|
*
|
|
|
|
* <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
|
|
|
|
*
|
|
|
|
* There's no arbitrary limit to the number of backticks you
|
|
|
|
* can use as delimters. If you need three consecutive backticks
|
|
|
|
* in your code, use four for delimiters, etc.
|
|
|
|
*
|
|
|
|
* * You can use spaces to get literal backticks at the edges:
|
|
|
|
*
|
|
|
|
* ... type `` `bar` `` ...
|
|
|
|
*
|
|
|
|
* Turns to:
|
|
|
|
*
|
|
|
|
* ... type <code>`bar`</code> ...
|
|
|
|
*/
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('codeSpans', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeSpans.before', text, options, globals);
|
2015-07-15 04:17:27 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
(^|[^\\]) // Character before opening ` can't be a backslash
|
|
|
|
(`+) // $2 = Opening run of `
|
|
|
|
( // $3 = The code block
|
|
|
|
[^\r]*?
|
|
|
|
[^`] // attacklab: work around lack of lookbehind
|
|
|
|
)
|
|
|
|
\2 // Matching closer
|
|
|
|
(?!`)
|
|
|
|
/gm, function(){...});
|
|
|
|
*/
|
2016-06-09 00:16:49 +08:00
|
|
|
|
|
|
|
if (typeof(text) === 'undefined') {
|
|
|
|
text = '';
|
|
|
|
}
|
2015-07-15 04:17:27 +08:00
|
|
|
text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
|
|
|
|
function (wholeMatch, m1, m2, m3) {
|
|
|
|
var c = m3;
|
|
|
|
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
|
|
|
|
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
|
|
|
|
c = showdown.subParser('encodeCode')(c);
|
|
|
|
return m1 + '<code>' + c + '</code>';
|
|
|
|
}
|
|
|
|
);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeSpans.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Convert all tabs to spaces
|
|
|
|
*/
|
|
|
|
showdown.subParser('detab', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// expand first n-1 tabs
|
|
|
|
text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// replace the nth with two sentinels
|
|
|
|
text = text.replace(/\t/g, '~A~B');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// use the sentinel to anchor our regex so it doesn't explode
|
|
|
|
text = text.replace(/~B(.+?)~A/g, function (wholeMatch, m1) {
|
|
|
|
var leadingText = m1,
|
|
|
|
numSpaces = 4 - leadingText.length % 4; // g_tab_width
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// there *must* be a better way to do this:
|
|
|
|
for (var i = 0; i < numSpaces; i++) {
|
|
|
|
leadingText += ' ';
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return leadingText;
|
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// clean up sentinels
|
|
|
|
text = text.replace(/~A/g, ' '); // g_tab_width
|
|
|
|
text = text.replace(/~B/g, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Smart processing for ampersands and angle brackets that need to be encoded.
|
|
|
|
*/
|
|
|
|
showdown.subParser('encodeAmpsAndAngles', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
// Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
|
|
|
|
// http://bumppo.net/projects/amputator/
|
|
|
|
text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, '&');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Encode naked <'s
|
|
|
|
text = text.replace(/<(?![a-z\/?\$!])/gi, '<');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Returns the string, with after processing the following backslash escape sequences.
|
|
|
|
*
|
|
|
|
* attacklab: The polite way to do this is with the new escapeCharacters() function:
|
|
|
|
*
|
|
|
|
* text = escapeCharacters(text,"\\",true);
|
|
|
|
* text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
|
|
|
|
*
|
|
|
|
* ...but we're sidestepping its use of the (slow) RegExp constructor
|
|
|
|
* as an optimization for Firefox. This function gets called a LOT.
|
|
|
|
*/
|
|
|
|
showdown.subParser('encodeBackslashEscapes', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback);
|
|
|
|
text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, showdown.helper.escapeCharactersCallback);
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Encode/escape certain characters inside Markdown code runs.
|
|
|
|
* The point is that in code, these characters are literals,
|
|
|
|
* and lose their special Markdown meanings.
|
|
|
|
*/
|
|
|
|
showdown.subParser('encodeCode', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Encode all ampersands; HTML entities are not
|
|
|
|
// entities within a Markdown code span.
|
2017-01-28 10:46:34 +08:00
|
|
|
text = text
|
|
|
|
.replace(/&/g, '&')
|
2015-01-19 20:04:22 +08:00
|
|
|
// Do the angle bracket song and dance:
|
2017-01-28 10:46:34 +08:00
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Now, escape characters that are magic in Markdown:
|
2017-01-28 10:46:34 +08:00
|
|
|
//text = showdown.helper.escapeCharacters(text, '*_{}[]\\', false); // replaced line to improve performance
|
|
|
|
.replace(/([*_{}\[\]\\])/g, showdown.helper.escapeCharactersCallback);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// jj the line above breaks this:
|
|
|
|
//---
|
|
|
|
//* Item
|
|
|
|
// 1. Subitem
|
|
|
|
// special char: *
|
|
|
|
// ---
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Within tags -- meaning between < and > -- encode [\ ` * _] so they
|
|
|
|
* don't conflict with their use in Markdown for code, italics and strong.
|
|
|
|
*/
|
|
|
|
showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Build a regex to find HTML tags and comments. See Friedl's
|
|
|
|
// "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
|
|
|
|
var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(regex, function (wholeMatch) {
|
2017-01-28 10:46:34 +08:00
|
|
|
return wholeMatch
|
|
|
|
.replace(/(.)<\/?code>(?=.)/g, '$1`')
|
|
|
|
//tag = showdown.helper.escapeCharacters(tag, '\\`*_', false);
|
|
|
|
.replace(/([\\`*_])/g, showdown.helper.escapeCharactersCallback);
|
2015-01-19 20:04:22 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Handle github codeblocks prior to running HashHTML so that
|
|
|
|
* HTML contained within the codeblock gets escaped properly
|
|
|
|
* Example:
|
|
|
|
* ```ruby
|
|
|
|
* def hello_world(x)
|
|
|
|
* puts "Hello, #{x}"
|
|
|
|
* end
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
showdown.subParser('githubCodeBlocks', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-12 03:33:11 +08:00
|
|
|
// early exit if option is not enabled
|
|
|
|
if (!options.ghCodeBlocks) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('githubCodeBlocks.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text += '~0';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-08-02 04:05:28 +08:00
|
|
|
text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, language, codeblock) {
|
|
|
|
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-25 09:04:06 +08:00
|
|
|
// First parse the github code block
|
2015-01-19 20:04:22 +08:00
|
|
|
codeblock = showdown.subParser('encodeCode')(codeblock);
|
|
|
|
codeblock = showdown.subParser('detab')(codeblock);
|
|
|
|
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
|
|
|
|
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-14 07:41:59 +08:00
|
|
|
codeblock = '<pre><code' + (language ? ' class="' + language + ' language-' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-25 09:04:06 +08:00
|
|
|
codeblock = showdown.subParser('hashBlock')(codeblock, options, globals);
|
|
|
|
|
|
|
|
// Since GHCodeblocks can be false positives, we need to
|
|
|
|
// store the primitive text and the parsed text in a global var,
|
|
|
|
// and then return a token
|
|
|
|
return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
|
2015-01-19 20:04:22 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: strip sentinel
|
|
|
|
text = text.replace(/~0/, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
return globals.converter._dispatch('githubCodeBlocks.after', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('hashBlock', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
text = text.replace(/(^\n+|\n+$)/g, '');
|
|
|
|
return '\n\n~K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('hashElement', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return function (wholeMatch, m1) {
|
|
|
|
var blockText = m1;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Undo double lines
|
|
|
|
blockText = blockText.replace(/\n\n/g, '\n');
|
|
|
|
blockText = blockText.replace(/^\n/, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// strip trailing blank lines
|
|
|
|
blockText = blockText.replace(/\n+$/g, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Replace the element text with a marker ("~KxK" where x is its key)
|
|
|
|
blockText = '\n\n~K' + (globals.gHtmlBlocks.push(blockText) - 1) + 'K\n\n';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return blockText;
|
|
|
|
};
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
var blockTags = [
|
|
|
|
'pre',
|
|
|
|
'div',
|
|
|
|
'h1',
|
|
|
|
'h2',
|
|
|
|
'h3',
|
|
|
|
'h4',
|
|
|
|
'h5',
|
|
|
|
'h6',
|
|
|
|
'blockquote',
|
|
|
|
'table',
|
|
|
|
'dl',
|
|
|
|
'ol',
|
|
|
|
'ul',
|
|
|
|
'script',
|
|
|
|
'noscript',
|
|
|
|
'form',
|
|
|
|
'fieldset',
|
|
|
|
'iframe',
|
|
|
|
'math',
|
|
|
|
'style',
|
|
|
|
'section',
|
|
|
|
'header',
|
|
|
|
'footer',
|
|
|
|
'nav',
|
|
|
|
'article',
|
|
|
|
'aside',
|
|
|
|
'address',
|
|
|
|
'audio',
|
|
|
|
'canvas',
|
|
|
|
'figure',
|
|
|
|
'hgroup',
|
|
|
|
'output',
|
|
|
|
'video',
|
|
|
|
'p'
|
|
|
|
],
|
2016-01-02 09:08:17 +08:00
|
|
|
repFunc = function (wholeMatch, match, left, right) {
|
|
|
|
var txt = wholeMatch;
|
|
|
|
// check if this html element is marked as markdown
|
|
|
|
// if so, it's contents should be parsed as markdown
|
|
|
|
if (left.search(/\bmarkdown\b/) !== -1) {
|
|
|
|
txt = left + globals.converter.makeHtml(match) + right;
|
|
|
|
}
|
|
|
|
return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
|
2016-01-02 07:33:33 +08:00
|
|
|
};
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
for (var i = 0; i < blockTags.length; ++i) {
|
2016-08-30 13:24:19 +08:00
|
|
|
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^ {0,3}<' + blockTags[i] + '\\b[^>]*>', '</' + blockTags[i] + '>', 'gim');
|
2016-01-02 07:33:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-01-02 07:33:33 +08:00
|
|
|
// HR SPECIAL CASE
|
2016-08-30 13:07:57 +08:00
|
|
|
text = text.replace(/(\n {0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
|
2016-01-02 07:33:33 +08:00
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-08-30 13:07:57 +08:00
|
|
|
// Special case for standalone HTML comments
|
|
|
|
text = showdown.helper.replaceRecursiveRegExp(text, function (txt) {
|
|
|
|
return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
|
2016-08-30 13:24:19 +08:00
|
|
|
}, '^ {0,3}<!--', '-->', 'gm');
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// PHP and ASP-style processor instructions (<?...?> and <%...%>)
|
2016-08-30 13:24:19 +08:00
|
|
|
text = text.replace(/(?:\n\n)( {0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
|
2016-01-02 07:33:33 +08:00
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-10-19 08:55:35 +08:00
|
|
|
/**
|
|
|
|
* Hash span elements that should not be parsed as markdown
|
|
|
|
*/
|
|
|
|
showdown.subParser('hashHTMLSpans', function (text, config, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
|
|
|
|
|
|
|
|
for (var i = 0; i < matches.length; ++i) {
|
2016-12-21 07:57:10 +08:00
|
|
|
text = text.replace(matches[i][0], '~C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
|
2015-10-19 08:55:35 +08:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unhash HTML spans
|
|
|
|
*/
|
|
|
|
showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
|
2016-12-21 07:57:10 +08:00
|
|
|
text = text.replace('~C' + i + 'C', globals.gHtmlSpans[i]);
|
2015-10-19 08:55:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2016-01-25 11:01:54 +08:00
|
|
|
/**
|
|
|
|
* Hash span elements that should not be parsed as markdown
|
|
|
|
*/
|
|
|
|
showdown.subParser('hashPreCodeTags', function (text, config, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var repFunc = function (wholeMatch, match, left, right) {
|
|
|
|
// encode html entities
|
|
|
|
var codeblock = left + showdown.subParser('encodeCode')(match) + right;
|
|
|
|
return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
|
|
|
|
};
|
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^ {0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^ {0,3}</code>\\s*</pre>', 'gim');
|
2016-01-25 11:01:54 +08:00
|
|
|
return text;
|
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('headers', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('headers.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-07-13 12:09:03 +08:00
|
|
|
var prefixHeader = options.prefixHeaderId,
|
2015-07-15 00:10:52 +08:00
|
|
|
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
|
2016-12-31 03:01:44 +08:00
|
|
|
ghHeaderId = options.ghCompatibleHeaderId,
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// Set text-style headers:
|
|
|
|
// Header 1
|
|
|
|
// ========
|
|
|
|
//
|
|
|
|
// Header 2
|
|
|
|
// --------
|
|
|
|
//
|
2015-07-15 00:10:52 +08:00
|
|
|
setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
|
|
|
|
setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
|
|
|
|
|
|
|
|
text = text.replace(setextRegexH1, function (wholeMatch, m1) {
|
2015-06-08 10:41:14 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
|
2015-06-08 10:57:18 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart,
|
2015-06-17 09:19:44 +08:00
|
|
|
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(hashBlock, options, globals);
|
|
|
|
});
|
|
|
|
|
2015-07-15 00:10:52 +08:00
|
|
|
text = text.replace(setextRegexH2, function (matchFound, m1) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
|
2015-06-08 10:57:18 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart + 1,
|
2015-06-17 09:19:44 +08:00
|
|
|
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(hashBlock, options, globals);
|
|
|
|
});
|
|
|
|
|
|
|
|
// atx-style headers:
|
|
|
|
// # Header 1
|
|
|
|
// ## Header 2
|
|
|
|
// ## Header 2 with closing hashes ##
|
|
|
|
// ...
|
|
|
|
// ###### Header 6
|
|
|
|
//
|
2016-12-17 14:01:15 +08:00
|
|
|
var atxStyle = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
|
|
|
|
|
|
|
|
text = text.replace(atxStyle, function (wholeMatch, m1, m2) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var span = showdown.subParser('spanGamut')(m2, options, globals),
|
2015-06-08 10:57:18 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart - 1 + m1.length,
|
2015-06-17 09:19:44 +08:00
|
|
|
header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
return showdown.subParser('hashBlock')(header, options, globals);
|
|
|
|
});
|
|
|
|
|
|
|
|
function headerId(m) {
|
2016-12-31 03:01:44 +08:00
|
|
|
var title, escapedId;
|
|
|
|
|
|
|
|
if (ghHeaderId) {
|
|
|
|
escapedId = m
|
|
|
|
.replace(/ /g, '-')
|
|
|
|
//replace previously escaped chars (&, ~ and $)
|
|
|
|
.replace(/&/g, '')
|
|
|
|
.replace(/~T/g, '')
|
|
|
|
.replace(/~D/g, '')
|
|
|
|
//replace rest of the chars (&~$ are repeated as they might have been escaped)
|
2016-12-31 03:46:46 +08:00
|
|
|
// borrowed from github's redcarpet (some they should produce similar results)
|
|
|
|
.replace(/[&+$,\/:;=?@"#{}|^~\[\]`\\*)(%.!'<>]/g, '')
|
2016-12-31 03:01:44 +08:00
|
|
|
.toLowerCase();
|
|
|
|
} else {
|
|
|
|
escapedId = m.replace(/[^\w]/g, '').toLowerCase();
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
if (globals.hashLinkCounts[escapedId]) {
|
|
|
|
title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
|
|
|
|
} else {
|
|
|
|
title = escapedId;
|
|
|
|
globals.hashLinkCounts[escapedId] = 1;
|
|
|
|
}
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Prefix id to prevent causing inadvertent pre-existing style matches.
|
|
|
|
if (prefixHeader === true) {
|
|
|
|
prefixHeader = 'section';
|
|
|
|
}
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
if (showdown.helper.isString(prefixHeader)) {
|
|
|
|
return prefixHeader + title;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
return title;
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('headers.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2017-01-28 10:17:19 +08:00
|
|
|
/**
|
|
|
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
|
|
|
*/
|
|
|
|
showdown.subParser('horizontalRule', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
text = globals.converter._dispatch('horizontalRule.before', text, options, globals);
|
|
|
|
|
|
|
|
var key = showdown.subParser('hashBlock')('<hr />', options, globals);
|
|
|
|
text = text.replace(/^ {0,2}( ?-){3,}[ \t]*$/gm, key);
|
|
|
|
text = text.replace(/^ {0,2}( ?\*){3,}[ \t]*$/gm, key);
|
|
|
|
text = text.replace(/^ {0,2}( ?_){3,}[ \t]*$/gm, key);
|
|
|
|
|
|
|
|
text = globals.converter._dispatch('horizontalRule.after', text, options, globals);
|
|
|
|
return text;
|
|
|
|
});
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Turn Markdown image shortcuts into <img> tags.
|
|
|
|
*/
|
|
|
|
showdown.subParser('images', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('images.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
var inlineRegExp = /!\[(.*?)]\s?\([ \t]*()<?(\S+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(['"])(.*?)\6[ \t]*)?\)/g,
|
2016-06-09 00:16:49 +08:00
|
|
|
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
|
|
|
|
|
|
|
|
var gUrls = globals.gUrls,
|
|
|
|
gTitles = globals.gTitles,
|
|
|
|
gDims = globals.gDimensions;
|
|
|
|
|
|
|
|
linkId = linkId.toLowerCase();
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
if (!title) {
|
|
|
|
title = '';
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
if (url === '' || url === null) {
|
|
|
|
if (linkId === '' || linkId === null) {
|
|
|
|
// lower-case and turn embedded newlines into spaces
|
|
|
|
linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
|
|
|
|
}
|
|
|
|
url = '#' + linkId;
|
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
if (!showdown.helper.isUndefined(gUrls[linkId])) {
|
2015-01-19 20:04:22 +08:00
|
|
|
url = gUrls[linkId];
|
2015-06-17 08:22:05 +08:00
|
|
|
if (!showdown.helper.isUndefined(gTitles[linkId])) {
|
2015-01-19 20:04:22 +08:00
|
|
|
title = gTitles[linkId];
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-06-17 08:22:05 +08:00
|
|
|
if (!showdown.helper.isUndefined(gDims[linkId])) {
|
|
|
|
width = gDims[linkId].width;
|
|
|
|
height = gDims[linkId].height;
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
} else {
|
|
|
|
return wholeMatch;
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-01-28 10:46:34 +08:00
|
|
|
altText = altText
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
//altText = showdown.helper.escapeCharacters(altText, '*_', false);
|
|
|
|
.replace(showdown.helper.regexes.asteriskAndDash, showdown.helper.escapeCharactersCallback);
|
|
|
|
//url = showdown.helper.escapeCharacters(url, '*_', false);
|
|
|
|
url = url.replace(showdown.helper.regexes.asteriskAndDash, showdown.helper.escapeCharactersCallback);
|
2015-01-19 20:04:22 +08:00
|
|
|
var result = '<img src="' + url + '" alt="' + altText + '"';
|
|
|
|
|
2015-06-11 08:29:42 +08:00
|
|
|
if (title) {
|
2017-01-28 10:46:34 +08:00
|
|
|
title = title
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
//title = showdown.helper.escapeCharacters(title, '*_', false);
|
|
|
|
.replace(showdown.helper.regexes.asteriskAndDash, showdown.helper.escapeCharactersCallback);
|
2015-06-08 10:57:18 +08:00
|
|
|
result += ' title="' + title + '"';
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
if (width && height) {
|
|
|
|
width = (width === '*') ? 'auto' : width;
|
|
|
|
height = (height === '*') ? 'auto' : height;
|
|
|
|
|
|
|
|
result += ' width="' + width + '"';
|
|
|
|
result += ' height="' + height + '"';
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
result += ' />';
|
|
|
|
|
|
|
|
return result;
|
2015-06-17 08:22:05 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// First, handle reference-style labeled images: ![alt text][id]
|
2015-06-17 08:22:05 +08:00
|
|
|
text = text.replace(referenceRegExp, writeImageTag);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
|
|
|
|
text = text.replace(inlineRegExp, writeImageTag);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('images.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('italicsAndBold', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('italicsAndBold.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-07-11 22:45:58 +08:00
|
|
|
if (options.literalMidWordUnderscores) {
|
|
|
|
//underscores
|
|
|
|
// Since we are consuming a \s character, we need to add it
|
2016-06-27 23:58:35 +08:00
|
|
|
text = text.replace(/(^|\s|>|\b)__(?=\S)([\s\S]+?)__(?=\b|<|\s|$)/gm, '$1<strong>$2</strong>');
|
|
|
|
text = text.replace(/(^|\s|>|\b)_(?=\S)([\s\S]+?)_(?=\b|<|\s|$)/gm, '$1<em>$2</em>');
|
2015-07-11 22:45:58 +08:00
|
|
|
//asterisks
|
2015-10-19 10:09:50 +08:00
|
|
|
text = text.replace(/(\*\*)(?=\S)([^\r]*?\S[*]*)\1/g, '<strong>$2</strong>');
|
|
|
|
text = text.replace(/(\*)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-11 22:45:58 +08:00
|
|
|
} else {
|
|
|
|
// <strong> must go first:
|
|
|
|
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
|
|
|
|
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
|
|
|
|
}
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Form HTML ordered (numbered) and unordered (bulleted) lists.
|
|
|
|
*/
|
|
|
|
showdown.subParser('lists', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('lists.before', text, options, globals);
|
2016-12-21 07:57:10 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
/**
|
|
|
|
* Process the contents of a single ordered or unordered list, splitting it
|
|
|
|
* into individual list items.
|
2015-01-19 23:42:20 +08:00
|
|
|
* @param {string} listStr
|
2015-08-23 09:46:04 +08:00
|
|
|
* @param {boolean} trimTrailing
|
2015-06-13 21:59:14 +08:00
|
|
|
* @returns {string}
|
2015-01-19 20:04:22 +08:00
|
|
|
*/
|
2015-08-02 04:05:28 +08:00
|
|
|
function processListItems (listStr, trimTrailing) {
|
2015-01-19 20:04:22 +08:00
|
|
|
// The $g_list_level global keeps track of when we're inside a list.
|
|
|
|
// Each time we enter a list, we increment it; when we leave a list,
|
|
|
|
// we decrement. If it's zero, we're not in a list anymore.
|
|
|
|
//
|
|
|
|
// We do this because when we're not inside a list, we want to treat
|
|
|
|
// something like this:
|
|
|
|
//
|
|
|
|
// I recommend upgrading to version
|
|
|
|
// 8. Oops, now this line is treated
|
|
|
|
// as a sub-list.
|
|
|
|
//
|
|
|
|
// As a single paragraph, despite the fact that the second line starts
|
|
|
|
// with a digit-period-space sequence.
|
|
|
|
//
|
|
|
|
// Whereas when we're inside a list (or sub-list), that line will be
|
|
|
|
// treated as the start of a sub-list. What a kludge, huh? This is
|
|
|
|
// an aspect of Markdown's syntax that's hard to parse perfectly
|
|
|
|
// without resorting to mind-reading. Perhaps the solution is to
|
|
|
|
// change the syntax rules such that sub-lists must start with a
|
|
|
|
// starting cardinal number; e.g. "1." or "a.".
|
|
|
|
globals.gListLevel++;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// trim trailing blank lines:
|
|
|
|
listStr = listStr.replace(/\n{2,}$/, '\n');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: add sentinel to emulate \z
|
|
|
|
listStr += '~0';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
var rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0| {0,3}([*+-]|\d+[.])[ \t]+))/gm,
|
2015-08-02 04:05:28 +08:00
|
|
|
isParagraphed = (/\n[ \t]*\n(?!~0)/.test(listStr));
|
2015-07-12 06:02:02 +08:00
|
|
|
|
2016-11-11 16:15:24 +08:00
|
|
|
// Since version 1.5, nesting sublists requires 4 spaces (or 1 tab) indentation,
|
|
|
|
// which is a syntax breaking change
|
|
|
|
// activating this option reverts to old behavior
|
|
|
|
if (options.disableForced4SpacesIndentedSublists) {
|
|
|
|
rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm;
|
|
|
|
}
|
|
|
|
|
2015-07-12 06:02:02 +08:00
|
|
|
listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) {
|
|
|
|
checked = (checked && checked.trim() !== '');
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
|
2015-08-02 04:05:28 +08:00
|
|
|
var item = showdown.subParser('outdent')(m4, options, globals),
|
|
|
|
bulletStyle = '';
|
2015-07-12 06:02:02 +08:00
|
|
|
|
2015-08-02 04:05:28 +08:00
|
|
|
// Support for github tasklists
|
|
|
|
if (taskbtn && options.tasklists) {
|
|
|
|
bulletStyle = ' class="task-list-item" style="list-style-type: none;"';
|
2016-05-13 23:28:47 +08:00
|
|
|
item = item.replace(/^[ \t]*\[(x|X| )?]/m, function () {
|
2015-08-02 04:05:28 +08:00
|
|
|
var otp = '<input type="checkbox" disabled style="margin: 0px 0.35em 0.25em -1.6em; vertical-align: middle;"';
|
|
|
|
if (checked) {
|
|
|
|
otp += ' checked';
|
|
|
|
}
|
|
|
|
otp += '>';
|
|
|
|
return otp;
|
|
|
|
});
|
|
|
|
}
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
|
2016-12-17 13:20:23 +08:00
|
|
|
// ISSUE #312
|
|
|
|
// This input: - - - a
|
|
|
|
// causes trouble to the parser, since it interprets it as:
|
|
|
|
// <ul><li><li><li>a</li></li></li></ul>
|
|
|
|
// instead of:
|
|
|
|
// <ul><li>- - a</li></ul>
|
|
|
|
// So, to prevent it, we will put a marker (~A)in the beginning of the line
|
|
|
|
// Kind of hackish/monkey patching, but seems more effective than overcomplicating the list parser
|
2016-12-19 20:13:30 +08:00
|
|
|
item = item.replace(/^([-*+]|\d\.)[ \t]+[\S\n ]*/g, function (wm2) {
|
2016-12-17 13:20:23 +08:00
|
|
|
return '~A' + wm2;
|
|
|
|
});
|
|
|
|
|
2015-08-02 04:05:28 +08:00
|
|
|
// m1 - Leading line or
|
|
|
|
// Has a double return (multi paragraph) or
|
|
|
|
// Has sublist
|
2015-06-13 21:59:14 +08:00
|
|
|
if (m1 || (item.search(/\n{2,}/) > -1)) {
|
2015-08-02 04:05:28 +08:00
|
|
|
item = showdown.subParser('githubCodeBlocks')(item, options, globals);
|
2015-06-13 21:59:14 +08:00
|
|
|
item = showdown.subParser('blockGamut')(item, options, globals);
|
|
|
|
} else {
|
|
|
|
// Recursion for sub-lists:
|
|
|
|
item = showdown.subParser('lists')(item, options, globals);
|
|
|
|
item = item.replace(/\n$/, ''); // chomp(item)
|
2016-12-21 07:57:10 +08:00
|
|
|
item = showdown.subParser('hashHTMLBlocks')(item, options, globals);
|
2017-01-06 11:51:12 +08:00
|
|
|
// Colapse double linebreaks
|
2016-12-21 07:57:10 +08:00
|
|
|
item = item.replace(/\n\n+/g, '\n\n');
|
2017-01-06 11:51:12 +08:00
|
|
|
// replace double linebreaks with a placeholder
|
|
|
|
item = item.replace(/\n\n/g, '~B');
|
2015-08-02 04:05:28 +08:00
|
|
|
if (isParagraphed) {
|
|
|
|
item = showdown.subParser('paragraphs')(item, options, globals);
|
|
|
|
} else {
|
|
|
|
item = showdown.subParser('spanGamut')(item, options, globals);
|
|
|
|
}
|
2017-01-06 11:51:12 +08:00
|
|
|
item = item.replace(/~B/g, '\n\n');
|
2015-07-12 06:02:02 +08:00
|
|
|
}
|
2016-12-17 13:20:23 +08:00
|
|
|
|
|
|
|
// now we need to remove the marker (~A)
|
|
|
|
item = item.replace('~A', '');
|
|
|
|
// we can finally wrap the line in list item tags
|
2016-09-29 08:12:27 +08:00
|
|
|
item = '<li' + bulletStyle + '>' + item + '</li>\n';
|
2016-12-21 07:57:10 +08:00
|
|
|
|
2015-08-02 04:05:28 +08:00
|
|
|
return item;
|
2015-06-13 21:59:14 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: strip sentinel
|
|
|
|
listStr = listStr.replace(/~0/g, '');
|
|
|
|
|
|
|
|
globals.gListLevel--;
|
2015-08-02 04:05:28 +08:00
|
|
|
|
|
|
|
if (trimTrailing) {
|
|
|
|
listStr = listStr.replace(/\s+$/, '');
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return listStr;
|
2015-06-13 21:59:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-02 04:05:28 +08:00
|
|
|
* Check and parse consecutive lists (better fix for issue #142)
|
|
|
|
* @param {string} list
|
2015-06-13 21:59:14 +08:00
|
|
|
* @param {string} listType
|
2015-08-23 09:46:04 +08:00
|
|
|
* @param {boolean} trimTrailing
|
2015-08-02 04:05:28 +08:00
|
|
|
* @returns {string}
|
2015-06-13 21:59:14 +08:00
|
|
|
*/
|
2015-08-02 04:05:28 +08:00
|
|
|
function parseConsecutiveLists(list, listType, trimTrailing) {
|
|
|
|
// check if we caught 2 or more consecutive lists by mistake
|
2016-11-09 10:54:18 +08:00
|
|
|
// we use the counterRgx, meaning if listType is UL we look for OL and vice versa
|
2016-11-11 16:15:24 +08:00
|
|
|
var olRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?\d+\.[ \t]/gm : /^ {0,3}\d+\.[ \t]/gm,
|
|
|
|
ulRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?[*+-][ \t]/gm : /^ {0,3}[*+-][ \t]/gm,
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
counterRxg = (listType === 'ul') ? olRgx : ulRgx,
|
|
|
|
result = '';
|
2015-08-02 04:05:28 +08:00
|
|
|
|
|
|
|
if (list.search(counterRxg) !== -1) {
|
|
|
|
(function parseCL(txt) {
|
|
|
|
var pos = txt.search(counterRxg);
|
|
|
|
if (pos !== -1) {
|
|
|
|
// slice
|
2016-09-29 08:12:27 +08:00
|
|
|
result += '\n<' + listType + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
|
2015-08-02 04:05:28 +08:00
|
|
|
|
|
|
|
// invert counterType and listType
|
|
|
|
listType = (listType === 'ul') ? 'ol' : 'ul';
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
counterRxg = (listType === 'ul') ? olRgx : ulRgx;
|
2015-08-02 04:05:28 +08:00
|
|
|
|
|
|
|
//recurse
|
|
|
|
parseCL(txt.slice(pos));
|
|
|
|
} else {
|
2016-09-29 08:12:27 +08:00
|
|
|
result += '\n<' + listType + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
|
2015-06-13 21:59:14 +08:00
|
|
|
}
|
2015-08-02 04:05:28 +08:00
|
|
|
})(list);
|
|
|
|
} else {
|
2016-09-29 08:12:27 +08:00
|
|
|
result = '\n<' + listType + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
|
2015-06-13 21:59:14 +08:00
|
|
|
}
|
2015-08-02 04:05:28 +08:00
|
|
|
|
|
|
|
return result;
|
2015-06-13 21:59:14 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
// add sentinel to hack around khtml/safari bug:
|
2015-01-19 20:04:22 +08:00
|
|
|
// http://bugs.webkit.org/show_bug.cgi?id=11231
|
|
|
|
text += '~0';
|
|
|
|
|
|
|
|
if (globals.gListLevel) {
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
text = text.replace(/^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
|
|
|
|
function (wholeMatch, list, m2) {
|
|
|
|
var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
|
|
|
|
return parseConsecutiveLists(list, listType, true);
|
|
|
|
}
|
|
|
|
);
|
2015-01-19 20:04:22 +08:00
|
|
|
} else {
|
fix(lists): enforce 4 space indentation in sublists
Acording to the spec, multi paragraph (or block) list item requires subblocks
to be indented 4 spaces (or 1 tab). Although, this is mentioned in the documentation,
Showdown didn't enforce this rule in sublists because other implementations,
such as GFM also didn't. However, in some edge cases, this led to inconsistent behavior,
as shown in issue #299. This commit makes 4 space indentation in sublists
mandatory.
BREAKING CHANGE: syntax for sublists is more restrictive. Before, sublists SHOULD be
indented by 4 spaces, but indenting 2 spaces would work. Now, sublists MUST be
indented 4 spaces or they won't work.
With this input:
```md
* one
* two
* three
```
Before (ouput):
```html
<ul>
<li>one
<ul>
<li>two
<ul><li>three</li></ul>
<li>
</ul>
</li>
<ul>
```
After (output):
```html
<ul>
<li>one</li>
<li>two
<ul><li>three</li></ul>
</li>
</ul>
```
To migrate either fix source md files or activate the option `disableForced4SpacesIndentedSublists` (coming in v1.5.0):
```md
showdown.setOption('disableForced4SpacesIndentedSublists', true);
```
2016-11-11 15:56:29 +08:00
|
|
|
text = text.replace(/(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
|
|
|
|
function (wholeMatch, m1, list, m3) {
|
|
|
|
var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
|
|
|
|
return parseConsecutiveLists(list, listType, false);
|
|
|
|
}
|
|
|
|
);
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
// strip sentinel
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(/~0/, '');
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('lists.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Remove one level of line-leading tabs or spaces
|
|
|
|
*/
|
|
|
|
showdown.subParser('outdent', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: hack around Konqueror 3.5.4 bug:
|
|
|
|
// "----------bug".replace(/^-/g,"") == "bug"
|
|
|
|
text = text.replace(/^(\t|[ ]{1,4})/gm, '~0'); // attacklab: g_tab_width
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: clean up hack
|
|
|
|
text = text.replace(/~0/g, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
showdown.subParser('paragraphs', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('paragraphs.before', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
// Strip leading and trailing lines:
|
|
|
|
text = text.replace(/^\n+/g, '');
|
|
|
|
text = text.replace(/\n+$/g, '');
|
|
|
|
|
2015-01-19 23:42:20 +08:00
|
|
|
var grafs = text.split(/\n{2,}/g),
|
|
|
|
grafsOut = [],
|
|
|
|
end = grafs.length; // Wrap <p> tags
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < end; i++) {
|
|
|
|
var str = grafs[i];
|
|
|
|
// if this is an HTML marker, copy it
|
2016-01-25 09:04:06 +08:00
|
|
|
if (str.search(/~(K|G)(\d+)\1/g) >= 0) {
|
2015-01-19 20:04:22 +08:00
|
|
|
grafsOut.push(str);
|
2016-01-25 09:04:06 +08:00
|
|
|
} else {
|
2015-01-19 20:04:22 +08:00
|
|
|
str = showdown.subParser('spanGamut')(str, options, globals);
|
|
|
|
str = str.replace(/^([ \t]*)/g, '<p>');
|
|
|
|
str += '</p>';
|
|
|
|
grafsOut.push(str);
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Unhashify HTML blocks */
|
|
|
|
end = grafsOut.length;
|
|
|
|
for (i = 0; i < end; i++) {
|
2016-01-25 09:04:06 +08:00
|
|
|
var blockText = '',
|
|
|
|
grafsOutIt = grafsOut[i],
|
|
|
|
codeFlag = false;
|
2015-01-19 20:04:22 +08:00
|
|
|
// if this is a marker for an html block...
|
2016-01-25 09:04:06 +08:00
|
|
|
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
|
|
|
|
var delim = RegExp.$1,
|
|
|
|
num = RegExp.$2;
|
|
|
|
|
|
|
|
if (delim === 'K') {
|
|
|
|
blockText = globals.gHtmlBlocks[num];
|
|
|
|
} else {
|
|
|
|
// we need to check if ghBlock is a false positive
|
2016-01-25 11:01:54 +08:00
|
|
|
if (codeFlag) {
|
|
|
|
// use encoded version of all text
|
|
|
|
blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
|
|
|
|
} else {
|
|
|
|
blockText = globals.ghCodeBlocks[num].codeblock;
|
|
|
|
}
|
2016-01-25 09:04:06 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
|
2016-01-25 09:04:06 +08:00
|
|
|
|
|
|
|
grafsOutIt = grafsOutIt.replace(/(\n\n)?~(K|G)\d+\2(\n\n)?/, blockText);
|
|
|
|
// Check if grafsOutIt is a pre->code
|
|
|
|
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
|
|
|
|
codeFlag = true;
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2016-01-25 09:04:06 +08:00
|
|
|
grafsOut[i] = grafsOutIt;
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
2016-09-29 07:50:58 +08:00
|
|
|
text = grafsOut.join('\n');
|
2016-01-25 09:04:06 +08:00
|
|
|
// Strip leading and trailing lines:
|
|
|
|
text = text.replace(/^\n+/g, '');
|
|
|
|
text = text.replace(/\n+$/g, '');
|
2016-03-21 01:08:44 +08:00
|
|
|
return globals.converter._dispatch('paragraphs.after', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-03-02 02:15:32 +08:00
|
|
|
/**
|
2015-06-01 03:56:28 +08:00
|
|
|
* Run extension
|
2015-03-02 02:15:32 +08:00
|
|
|
*/
|
2015-06-01 03:56:28 +08:00
|
|
|
showdown.subParser('runExtension', function (ext, text, options, globals) {
|
2015-03-02 02:15:32 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
if (ext.filter) {
|
|
|
|
text = ext.filter(text, globals.converter, options);
|
|
|
|
|
|
|
|
} else if (ext.regex) {
|
|
|
|
// TODO remove this when old extension loading mechanism is deprecated
|
|
|
|
var re = ext.regex;
|
|
|
|
if (!re instanceof RegExp) {
|
|
|
|
re = new RegExp(re, 'g');
|
|
|
|
}
|
|
|
|
text = text.replace(re, ext.replace);
|
2015-03-02 02:15:32 +08:00
|
|
|
}
|
2015-06-01 03:56:28 +08:00
|
|
|
|
|
|
|
return text;
|
2015-03-02 02:15:32 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* These are all the transformations that occur *within* block-level
|
|
|
|
* tags like paragraphs, headers, and list items.
|
|
|
|
*/
|
|
|
|
showdown.subParser('spanGamut', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('spanGamut.before', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
text = showdown.subParser('codeSpans')(text, options, globals);
|
|
|
|
text = showdown.subParser('escapeSpecialCharsWithinTagAttributes')(text, options, globals);
|
|
|
|
text = showdown.subParser('encodeBackslashEscapes')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Process anchor and image tags. Images must come first,
|
|
|
|
// because ![foo][f] looks like an anchor.
|
|
|
|
text = showdown.subParser('images')(text, options, globals);
|
|
|
|
text = showdown.subParser('anchors')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Make links out of things like `<http://example.com/>`
|
|
|
|
// Must come after _DoAnchors(), because you can use < and >
|
|
|
|
// delimiters in inline links like [this](<url>).
|
|
|
|
text = showdown.subParser('autoLinks')(text, options, globals);
|
|
|
|
text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals);
|
|
|
|
text = showdown.subParser('italicsAndBold')(text, options, globals);
|
2015-07-11 22:59:06 +08:00
|
|
|
text = showdown.subParser('strikethrough')(text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-12-01 02:04:17 +08:00
|
|
|
// Do hard breaks
|
|
|
|
if (options.simpleLineBreaks) {
|
2016-12-21 07:57:10 +08:00
|
|
|
// GFM style hard breaks
|
2017-01-06 11:51:12 +08:00
|
|
|
text = text.replace(/\n/g, '<br />\n');
|
2016-12-01 02:04:17 +08:00
|
|
|
} else {
|
2016-12-21 07:57:10 +08:00
|
|
|
// Vanilla hard breaks
|
2017-01-06 11:51:12 +08:00
|
|
|
text = text.replace(/ +\n/g, '<br />\n');
|
2016-12-01 02:04:17 +08:00
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('spanGamut.after', text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('strikethrough', function (text, options, globals) {
|
2015-07-11 22:59:06 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if (options.strikethrough) {
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('strikethrough.before', text, options, globals);
|
2015-10-30 08:58:56 +08:00
|
|
|
text = text.replace(/(?:~T){2}([\s\S]+?)(?:~T){2}/g, '<del>$1</del>');
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('strikethrough.after', text, options, globals);
|
2015-07-11 22:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Strip any lines consisting only of spaces and tabs.
|
|
|
|
* This makes subsequent regexs easier to write, because we can
|
|
|
|
* match consecutive blank lines with /\n+/ instead of something
|
|
|
|
* contorted like /[ \t]*\n+/
|
|
|
|
*/
|
|
|
|
showdown.subParser('stripBlankLines', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
return text.replace(/^[ \t]+$/mg, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Strips link definitions from text, stores the URLs and titles in
|
|
|
|
* hash references.
|
|
|
|
* Link defs are in the form: ^[id]: url "optional title"
|
|
|
|
*/
|
|
|
|
showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
var regex = /^ {0,3}\[(.+)]:[ \t]*\n?[ \t]*<?(\S+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=~0))/gm;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
|
|
|
|
text += '~0';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
text = text.replace(regex, function (wholeMatch, linkId, url, width, height, blankLines, title) {
|
|
|
|
linkId = linkId.toLowerCase();
|
|
|
|
globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url); // Link IDs are case-insensitive
|
|
|
|
|
|
|
|
if (blankLines) {
|
2015-01-19 20:04:22 +08:00
|
|
|
// Oops, found blank lines, so it's not a title.
|
|
|
|
// Put back the parenthetical statement we stole.
|
2015-06-17 08:22:05 +08:00
|
|
|
return blankLines + title;
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-06-17 08:22:05 +08:00
|
|
|
} else {
|
|
|
|
if (title) {
|
|
|
|
globals.gTitles[linkId] = title.replace(/"|'/g, '"');
|
|
|
|
}
|
|
|
|
if (options.parseImgDimensions && width && height) {
|
|
|
|
globals.gDimensions[linkId] = {
|
|
|
|
width: width,
|
|
|
|
height: height
|
|
|
|
};
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
|
|
|
// Completely remove the definition from the text
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
|
|
|
// attacklab: strip sentinel
|
|
|
|
text = text.replace(/~0/, '');
|
|
|
|
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
showdown.subParser('tables', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
if (!options.tables) {
|
|
|
|
return text;
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{0,3}\|?[ \t]*:?[ \t]*(?:-|=){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:-|=){2,}[\s\S]+?(?:\n\n|~0)/gm;
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
function parseStyles(sLine) {
|
2016-05-18 04:27:40 +08:00
|
|
|
if (/^:[ \t]*--*$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:left;"';
|
2016-05-18 04:27:40 +08:00
|
|
|
} else if (/^--*[ \t]*:[ \t]*$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:right;"';
|
2016-05-18 04:27:40 +08:00
|
|
|
} else if (/^:[ \t]*--*[ \t]*:$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:center;"';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
function parseHeaders(header, style) {
|
|
|
|
var id = '';
|
|
|
|
header = header.trim();
|
|
|
|
if (options.tableHeaderId) {
|
|
|
|
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
|
|
|
|
}
|
|
|
|
header = showdown.subParser('spanGamut')(header, options, globals);
|
|
|
|
|
2016-02-02 08:29:06 +08:00
|
|
|
return '<th' + id + style + '>' + header + '</th>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseCells(cell, style) {
|
|
|
|
var subText = showdown.subParser('spanGamut')(cell, options, globals);
|
2016-02-02 08:29:06 +08:00
|
|
|
return '<td' + style + '>' + subText + '</td>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildTable(headers, cells) {
|
|
|
|
var tb = '<table>\n<thead>\n<tr>\n',
|
|
|
|
tblLgn = headers.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < tblLgn; ++i) {
|
|
|
|
tb += headers[i];
|
|
|
|
}
|
|
|
|
tb += '</tr>\n</thead>\n<tbody>\n';
|
|
|
|
|
|
|
|
for (i = 0; i < cells.length; ++i) {
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '<tr>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
for (var ii = 0; ii < tblLgn; ++ii) {
|
|
|
|
tb += cells[i][ii];
|
2015-07-12 01:21:43 +08:00
|
|
|
}
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '</tr>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '</tbody>\n</table>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
return tb;
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('tables.before', text, options, globals);
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
text = text.replace(tableRgx, function (rawTable) {
|
|
|
|
|
|
|
|
var i, tableLines = rawTable.split('\n');
|
|
|
|
|
|
|
|
// strip wrong first and last column if wrapped tables are used
|
|
|
|
for (i = 0; i < tableLines.length; ++i) {
|
2016-08-30 13:24:19 +08:00
|
|
|
if (/^ {0,3}\|/.test(tableLines[i])) {
|
|
|
|
tableLines[i] = tableLines[i].replace(/^ {0,3}\|/, '');
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
if (/\|[ \t]*$/.test(tableLines[i])) {
|
|
|
|
tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, '');
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
var rawHeaders = tableLines[0].split('|').map(function (s) { return s.trim();}),
|
|
|
|
rawStyles = tableLines[1].split('|').map(function (s) { return s.trim();}),
|
|
|
|
rawCells = [],
|
|
|
|
headers = [],
|
|
|
|
styles = [],
|
|
|
|
cells = [];
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
tableLines.shift();
|
|
|
|
tableLines.shift();
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
for (i = 0; i < tableLines.length; ++i) {
|
|
|
|
if (tableLines[i].trim() === '') {
|
|
|
|
continue;
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
rawCells.push(
|
|
|
|
tableLines[i]
|
|
|
|
.split('|')
|
|
|
|
.map(function (s) {
|
|
|
|
return s.trim();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
if (rawHeaders.length < rawStyles.length) {
|
|
|
|
return rawTable;
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
for (i = 0; i < rawStyles.length; ++i) {
|
|
|
|
styles.push(parseStyles(rawStyles[i]));
|
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
for (i = 0; i < rawHeaders.length; ++i) {
|
|
|
|
if (showdown.helper.isUndefined(styles[i])) {
|
|
|
|
styles[i] = '';
|
|
|
|
}
|
|
|
|
headers.push(parseHeaders(rawHeaders[i], styles[i]));
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
for (i = 0; i < rawCells.length; ++i) {
|
|
|
|
var row = [];
|
|
|
|
for (var ii = 0; ii < headers.length; ++ii) {
|
|
|
|
if (showdown.helper.isUndefined(rawCells[i][ii])) {
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
row.push(parseCells(rawCells[i][ii], styles[ii]));
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
cells.push(row);
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
return buildTable(headers, cells);
|
|
|
|
});
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('tables.after', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
|
|
|
return text;
|
2015-07-11 23:44:24 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Swap back in all the special characters we've hidden.
|
|
|
|
*/
|
|
|
|
showdown.subParser('unescapeSpecialChars', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(/~E(\d+)E/g, function (wholeMatch, m1) {
|
|
|
|
var charCodeToReplace = parseInt(m1);
|
|
|
|
return String.fromCharCode(charCodeToReplace);
|
|
|
|
});
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
2016-06-21 09:03:51 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
var root = this;
|
|
|
|
|
|
|
|
// CommonJS/nodeJS Loader
|
|
|
|
if (typeof module !== 'undefined' && module.exports) {
|
2015-01-19 20:04:22 +08:00
|
|
|
module.exports = showdown;
|
2015-01-19 23:42:20 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
// AMD Loader
|
2015-01-19 23:42:20 +08:00
|
|
|
} else if (typeof define === 'function' && define.amd) {
|
2015-12-23 12:24:53 +08:00
|
|
|
define(function () {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
return showdown;
|
|
|
|
});
|
2015-01-19 23:42:20 +08:00
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
// Regular Browser loader
|
2015-01-19 23:42:20 +08:00
|
|
|
} else {
|
2015-01-19 20:04:22 +08:00
|
|
|
root.showdown = showdown;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2016-06-21 09:03:51 +08:00
|
|
|
}).call(this);
|
2015-10-15 04:44:19 +08:00
|
|
|
|
2016-06-21 09:03:51 +08:00
|
|
|
//# sourceMappingURL=showdown.js.map
|