2015-07-13 12:09:03 +08:00
|
|
|
;/*! showdown 13-07-2015 */
|
2015-01-16 05:21:33 +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: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Omit the default extra whiteline added to code blocks',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
noHeaderId: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off generated header id',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
prefixHeaderId: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Specify a prefix to generated header ids',
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
headerLevelStart: {
|
|
|
|
default: false,
|
|
|
|
describe: 'The header blocks level start',
|
|
|
|
type: 'integer'
|
|
|
|
},
|
|
|
|
parseImgDimensions: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off image dimension parsing',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
simplifiedAutoLink: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off GFM autolink style',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
literalMidWordUnderscores: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Parse midword underscores as literal underscores',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
strikethrough: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off strikethrough support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tables: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off tables support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tablesHeaderId: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Add an id to table headers',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
ghCodeBlocks: {
|
|
|
|
default: true,
|
|
|
|
describe: 'Turn on/off GFM fenced code blocks support',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
tasklists: {
|
|
|
|
default: false,
|
|
|
|
describe: 'Turn on/off GFM tasklist support',
|
|
|
|
type: 'boolean'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (simple === false) {
|
|
|
|
return JSON.parse(JSON.stringify(defaultOptions));
|
|
|
|
}
|
|
|
|
var ret = {};
|
|
|
|
for (var opt in defaultOptions) {
|
|
|
|
if (defaultOptions.hasOwnProperty(opt)) {
|
|
|
|
ret[opt] = defaultOptions[opt].default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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),
|
2015-07-12 09:15:35 +08:00
|
|
|
flavor = {
|
|
|
|
github: {
|
|
|
|
omitExtraWLInCodeBlocks: true,
|
|
|
|
prefixHeaderId: 'user-content-',
|
|
|
|
simplifiedAutoLink: true,
|
|
|
|
literalMidWordUnderscores: true,
|
|
|
|
strikethrough: true,
|
|
|
|
tables: true,
|
|
|
|
tablesHeaderId: true,
|
|
|
|
ghCodeBlocks: true,
|
|
|
|
tasklists: true
|
|
|
|
},
|
2015-07-13 12:09:03 +08:00
|
|
|
vanilla: getDefaultOpts(true)
|
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';
|
|
|
|
if (flavor.hasOwnProperty(name)) {
|
|
|
|
var preset = flavor[name];
|
|
|
|
for (var option in preset) {
|
|
|
|
if (preset.hasOwnProperty(option)) {
|
|
|
|
globalOptions[option] = preset[option];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-06-08 02:02:45 +08:00
|
|
|
if (type !== 'lang' && type !== 'output') {
|
2015-06-01 03:56:28 +08:00
|
|
|
ret.valid = false;
|
2015-06-08 02:02:45 +08:00
|
|
|
ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang" or "output"';
|
2015-06-01 03:56:28 +08:00
|
|
|
return ret;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' +
|
|
|
|
typeof ext.regex + ' given';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (showdown.helper.isUndefined(ext.replace)) {
|
|
|
|
ret.valid = false;
|
|
|
|
ret.error = baseMsg + '"regex" extensions must implement a replace string or function';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-06-01 03:56:28 +08:00
|
|
|
ret.valid = false;
|
2015-06-08 02:02:45 +08:00
|
|
|
ret.error = baseMsg + 'extensions must define either a "regex" property or a "filter" method';
|
2015-06-01 03:56:28 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2015-06-08 02:02:45 +08:00
|
|
|
|
|
|
|
if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) {
|
2015-06-01 03:56:28 +08:00
|
|
|
ret.valid = false;
|
2015-06-08 02:02:45 +08:00
|
|
|
ret.error = baseMsg + 'output extensions must define a filter property';
|
2015-06-01 03:56:28 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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}
|
|
|
|
*/
|
|
|
|
showdown.helper.isString = function isString(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
|
|
|
|
2015-01-19 23:42:20 +08:00
|
|
|
/**
|
|
|
|
* ForEach helper function
|
2015-03-02 02:15:32 +08:00
|
|
|
* @static
|
2015-01-19 23:42:20 +08:00
|
|
|
* @param {*} obj
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
showdown.helper.forEach = function forEach(obj, callback) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
if (typeof obj.forEach === 'function') {
|
|
|
|
obj.forEach(callback);
|
|
|
|
} else {
|
2015-03-02 02:15:32 +08:00
|
|
|
for (var i = 0; i < obj.length; i++) {
|
2015-01-19 20:04:22 +08:00
|
|
|
callback(obj[i], i, obj);
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
}
|
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}
|
|
|
|
*/
|
2015-01-19 23:42:20 +08:00
|
|
|
showdown.helper.isArray = function isArray(a) {
|
|
|
|
'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`.
|
|
|
|
*/
|
2015-01-19 23:42:20 +08:00
|
|
|
showdown.helper.isUndefined = function isUndefined(value) {
|
|
|
|
'use strict';
|
|
|
|
return typeof value === 'undefined';
|
|
|
|
};
|
|
|
|
|
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';
|
|
|
|
return s.replace(/[_-]||\s/g, '').toLowerCase();
|
|
|
|
};
|
|
|
|
|
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|*}
|
|
|
|
*/
|
2015-01-19 23:42:20 +08:00
|
|
|
showdown.helper.escapeCharacters = function escapeCharacters(text, charsToEscape, afterBackslash) {
|
|
|
|
'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
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
/**
|
|
|
|
* POLYFILLS
|
|
|
|
*/
|
|
|
|
if (showdown.helper.isUndefined(console)) {
|
|
|
|
console = {
|
|
|
|
warn: function (msg) {
|
|
|
|
'use strict';
|
|
|
|
alert(msg);
|
|
|
|
},
|
|
|
|
log: function (msg) {
|
|
|
|
'use strict';
|
|
|
|
alert(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Estevao on 31-05-2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Showdown Converter class
|
|
|
|
* @class
|
|
|
|
* @param {object} [converterOptions]
|
|
|
|
* @returns {
|
|
|
|
* {makeHtml: Function},
|
|
|
|
* {setOption: Function},
|
|
|
|
* {getOption: Function},
|
|
|
|
* {getOptions: Function}
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
showdown.Converter = function (converterOptions) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var
|
|
|
|
/**
|
|
|
|
* Options used by this converter
|
|
|
|
* @private
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
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 = [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parser Order
|
|
|
|
* @private
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
parserOrder = [
|
|
|
|
'githubCodeBlocks',
|
|
|
|
'hashHTMLBlocks',
|
|
|
|
'stripLinkDefinitions',
|
|
|
|
'blockGamut',
|
|
|
|
'unescapeSpecialChars'
|
|
|
|
];
|
|
|
|
|
|
|
|
_constructor();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converter constructor
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _constructor() {
|
|
|
|
converterOptions = converterOptions || {};
|
|
|
|
|
|
|
|
for (var gOpt in globalOptions) {
|
|
|
|
if (globalOptions.hasOwnProperty(gOpt)) {
|
|
|
|
options[gOpt] = globalOptions[gOpt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge options
|
|
|
|
if (typeof converterOptions === 'object') {
|
|
|
|
for (var opt in converterOptions) {
|
|
|
|
if (converterOptions.hasOwnProperty(opt)) {
|
|
|
|
options[opt] = converterOptions[opt];
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
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-06-01 03:56:28 +08:00
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
default:
|
|
|
|
// should never reach here
|
|
|
|
throw Error('Extension loader error: Type unrecognized!!!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LEGACY_SUPPORT
|
|
|
|
* @param {*} ext
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
function legacyExtensionLoading(ext, name) {
|
|
|
|
if (typeof ext === 'function') {
|
|
|
|
ext = ext(new showdown.Converter());
|
|
|
|
}
|
|
|
|
if (!showdown.helper.isArray(ext)) {
|
|
|
|
ext = [ext];
|
|
|
|
}
|
|
|
|
var valid = validate(ext, name);
|
|
|
|
|
|
|
|
if (!valid.valid) {
|
|
|
|
throw Error(valid.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < ext.length; ++i) {
|
|
|
|
switch (ext[i].type) {
|
|
|
|
case 'lang':
|
|
|
|
langExtensions.push(ext[i]);
|
|
|
|
break;
|
|
|
|
case 'output':
|
|
|
|
outputModifiers.push(ext[i]);
|
|
|
|
break;
|
|
|
|
default:// should never reach here
|
|
|
|
throw Error('Extension loader error: Type unrecognized!!!');
|
|
|
|
}
|
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: [],
|
|
|
|
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,
|
|
|
|
converter: this
|
|
|
|
};
|
|
|
|
|
|
|
|
// attacklab: Replace ~ with ~T
|
|
|
|
// This lets us use tilde as an escape char to avoid md5 hashes
|
|
|
|
// The choice of character is arbitrary; anything that isn't
|
|
|
|
// magic in Markdown will work.
|
|
|
|
text = text.replace(/~/g, '~T');
|
|
|
|
|
|
|
|
// attacklab: Replace $ with ~D
|
|
|
|
// RegExp interprets $ as a special character
|
|
|
|
// when it's in a replacement string
|
|
|
|
text = text.replace(/\$/g, '~D');
|
|
|
|
|
|
|
|
// Standardize line endings
|
|
|
|
text = text.replace(/\r\n/g, '\n'); // DOS to Unix
|
|
|
|
text = text.replace(/\r/g, '\n'); // Mac to Unix
|
|
|
|
|
|
|
|
// Make sure text begins and ends with a couple of newlines:
|
|
|
|
text = '\n\n' + text + '\n\n';
|
|
|
|
|
|
|
|
// detab
|
|
|
|
text = showdown.subParser('detab')(text, options, globals);
|
|
|
|
|
|
|
|
// stripBlankLines
|
|
|
|
text = showdown.subParser('stripBlankLines')(text, options, globals);
|
|
|
|
|
|
|
|
//run languageExtensions
|
|
|
|
showdown.helper.forEach(langExtensions, function (ext) {
|
|
|
|
text = showdown.subParser('runExtension')(ext, text, options, globals);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Run all registered parsers
|
|
|
|
for (var i = 0; i < parserOrder.length; ++i) {
|
|
|
|
var name = parserOrder[i];
|
|
|
|
text = parsers[name](text, options, globals);
|
|
|
|
}
|
|
|
|
|
|
|
|
// attacklab: Restore dollar signs
|
|
|
|
text = text.replace(/~D/g, '$$');
|
|
|
|
|
|
|
|
// attacklab: Restore tildes
|
|
|
|
text = text.replace(/~T/g, '~');
|
|
|
|
|
|
|
|
// Run output modifiers
|
|
|
|
showdown.helper.forEach(outputModifiers, function (ext) {
|
|
|
|
text = showdown.subParser('runExtension')(ext, text, options, globals);
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an option of this Converter instance
|
|
|
|
* @param {string} key
|
|
|
|
* @param {*} value
|
|
|
|
*/
|
|
|
|
this.setOption = function (key, value) {
|
|
|
|
options[key] = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the option of this Converter instance
|
|
|
|
* @param {string} key
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
this.getOption = function (key) {
|
|
|
|
return options[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the options of this Converter instance
|
|
|
|
* @returns {{}}
|
|
|
|
*/
|
|
|
|
this.getOptions = function () {
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add extension to THIS converter
|
|
|
|
* @param {{}} extension
|
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) {
|
|
|
|
if (flavor.hasOwnProperty(name)) {
|
|
|
|
var preset = flavor[name];
|
|
|
|
for (var option in preset) {
|
|
|
|
if (preset.hasOwnProperty(option)) {
|
|
|
|
options[option] = preset[option];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
|
|
|
*/
|
|
|
|
showdown.subParser('anchors', function (text, config, 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
|
|
|
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
|
|
|
|
2015-01-19 23:42:20 +08:00
|
|
|
url = showdown.helper.escapeCharacters(url, '*_', false);
|
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, '"');
|
2015-01-19 23:42:20 +08:00
|
|
|
title = showdown.helper.escapeCharacters(title, '*_', false);
|
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]
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
( // wrap whole match in $1
|
|
|
|
\[
|
|
|
|
(
|
|
|
|
(?:
|
|
|
|
\[[^\]]*\] // allow brackets nested one level
|
|
|
|
|
|
|
|
|
[^\[] // or anything else
|
|
|
|
)*
|
|
|
|
)
|
|
|
|
\]
|
|
|
|
|
|
|
|
[ ]? // one optional space
|
|
|
|
(?:\n[ ]*)? // one optional newline followed by spaces
|
|
|
|
|
|
|
|
\[
|
|
|
|
(.*?) // id = $3
|
|
|
|
\]
|
|
|
|
)()()()() // pad remaining backreferences
|
|
|
|
/g,_DoAnchors_callback);
|
|
|
|
*/
|
|
|
|
text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Next, inline-style links: [link text](url "optional title")
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
( // wrap whole match in $1
|
|
|
|
\[
|
|
|
|
(
|
|
|
|
(?:
|
|
|
|
\[[^\]]*\] // allow brackets nested one level
|
|
|
|
|
|
|
|
|
[^\[\]] // or anything else
|
|
|
|
)
|
|
|
|
)
|
|
|
|
\]
|
|
|
|
\( // literal paren
|
|
|
|
[ \t]*
|
|
|
|
() // no id, so leave $3 empty
|
|
|
|
<?(.*?)>? // href = $4
|
|
|
|
[ \t]*
|
|
|
|
( // $5
|
|
|
|
(['"]) // quote char = $6
|
|
|
|
(.*?) // Title = $7
|
|
|
|
\6 // matching quote
|
|
|
|
[ \t]* // ignore any spaces/tabs between closing quote and )
|
|
|
|
)? // title is optional
|
|
|
|
\)
|
|
|
|
)
|
|
|
|
/g,writeAnchorTag);
|
|
|
|
*/
|
|
|
|
text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
|
|
|
|
writeAnchorTag);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Last, handle reference-style shortcuts: [link text]
|
|
|
|
// These must come last in case you've also got [link test][1]
|
|
|
|
// or [link test](/foo)
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
2015-01-19 23:42:20 +08:00
|
|
|
( // wrap whole match in $1
|
2015-01-19 20:04:22 +08:00
|
|
|
\[
|
2015-01-19 23:42:20 +08:00
|
|
|
([^\[\]]+) // link text = $2; can't contain '[' or ']'
|
2015-01-19 20:04:22 +08:00
|
|
|
\]
|
2015-01-19 23:42:20 +08:00
|
|
|
)()()()()() // pad rest of backreferences
|
2015-01-19 20:04:22 +08:00
|
|
|
/g, writeAnchorTag);
|
|
|
|
*/
|
|
|
|
text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
|
|
|
|
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-07-11 09:42:53 +08:00
|
|
|
showdown.subParser('autoLinks', function (text, options) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-11 09:42:53 +08:00
|
|
|
//simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
|
2015-01-19 20:04:22 +08:00
|
|
|
|
2015-07-11 09:42:53 +08:00
|
|
|
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi,
|
|
|
|
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
|
|
|
|
simpleMailRegex = /\b(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)\b/gi,
|
|
|
|
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
|
|
|
|
|
|
|
|
text = text.replace(delimUrlRegex, '<a href=\"$1\">$1</a>');
|
|
|
|
text = text.replace(delimMailRegex, replaceMail);
|
|
|
|
//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) {
|
|
|
|
text = text.replace(simpleURLRegex, '<a href=\"$1\">$1</a>');
|
|
|
|
text = text.replace(simpleMailRegex, '<a href=\"$1\">$1</a>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceMail(wholeMatch, m1) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
|
|
|
|
return showdown.subParser('encodeEmailAddress')(unescapedStr);
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
return text;
|
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
|
|
|
|
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:
|
|
|
|
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);
|
2015-07-11 23:44:24 +08:00
|
|
|
text = text.replace(/^[ ]{0,2}([ ]?_[ ]?){3,}[ \t]*$/gm, key);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-12 02:50:26 +08:00
|
|
|
text = showdown.subParser('tables')(text, options, globals);
|
2015-01-19 20:04:22 +08:00
|
|
|
text = showdown.subParser('lists')(text, options, globals);
|
|
|
|
text = showdown.subParser('codeBlocks')(text, options, globals);
|
|
|
|
text = showdown.subParser('blockQuotes')(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
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 23:42:20 +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';
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
( // Wrap whole match in $1
|
|
|
|
(
|
|
|
|
^[ \t]*>[ \t]? // '>' at the start of a line
|
|
|
|
.+\n // rest of the first line
|
|
|
|
(.+\n)* // subsequent consecutive lines
|
|
|
|
\n* // blanks
|
|
|
|
)+
|
|
|
|
)
|
|
|
|
/gm, function(){...});
|
|
|
|
*/
|
|
|
|
|
|
|
|
text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
return text;
|
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';
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(text,
|
|
|
|
/(?:\n\n|^)
|
|
|
|
( // $1 = the code block -- one or more lines, starting with a space/tab
|
|
|
|
(?:
|
|
|
|
(?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
|
|
|
|
.*\n+
|
|
|
|
)+
|
|
|
|
)
|
|
|
|
(\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
|
|
|
|
/g,function(){...});
|
|
|
|
*/
|
|
|
|
|
|
|
|
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
// attacklab: strip sentinel
|
|
|
|
text = text.replace(/~0/, '');
|
|
|
|
|
|
|
|
return text;
|
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> ...
|
|
|
|
*/
|
|
|
|
showdown.subParser('codeSpans', function (text) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
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(){...});
|
|
|
|
*/
|
|
|
|
|
|
|
|
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>';
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
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
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
text = text.replace(/&/g, '&');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Do the angle bracket song and dance:
|
|
|
|
text = text.replace(/</g, '<');
|
|
|
|
text = text.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:
|
|
|
|
text = showdown.helper.escapeCharacters(text, '*_{}[]\\', false);
|
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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input: an email address, e.g. "foo@example.com"
|
|
|
|
*
|
|
|
|
* Output: the email address as a mailto link, with each character
|
|
|
|
* of the address encoded as either a decimal or hex entity, in
|
|
|
|
* the hopes of foiling most address harvesting spam bots. E.g.:
|
|
|
|
*
|
|
|
|
* <a href="mailto:foo@e
|
|
|
|
* xample.com">foo
|
|
|
|
* @example.com</a>
|
|
|
|
*
|
|
|
|
* Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
|
|
|
|
* mailing list: <http://tinyurl.com/yu7ue>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
showdown.subParser('encodeEmailAddress', function (addr) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var encode = [
|
|
|
|
function (ch) {
|
|
|
|
return '&#' + ch.charCodeAt(0) + ';';
|
|
|
|
},
|
|
|
|
function (ch) {
|
|
|
|
return '&#x' + ch.charCodeAt(0).toString(16) + ';';
|
|
|
|
},
|
|
|
|
function (ch) {
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
addr = 'mailto:' + addr;
|
|
|
|
|
|
|
|
addr = addr.replace(/./g, function (ch) {
|
|
|
|
if (ch === '@') {
|
|
|
|
// this *must* be encoded. I insist.
|
|
|
|
ch = encode[Math.floor(Math.random() * 2)](ch);
|
|
|
|
} else if (ch !== ':') {
|
|
|
|
// leave ':' alone (to spot mailto: later)
|
|
|
|
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;
|
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
addr = '<a href="' + addr + '">' + addr + '</a>';
|
|
|
|
addr = addr.replace(/">.+:/g, '">'); // strip the mailto: from the visible part
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return addr;
|
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) {
|
|
|
|
var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, '$1`');
|
2015-01-19 23:42:20 +08:00
|
|
|
tag = showdown.helper.escapeCharacters(tag, '\\`*_', false);
|
2015-01-19 20:04:22 +08:00
|
|
|
return tag;
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text += '~0';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, m1, m2) {
|
|
|
|
var language = m1,
|
|
|
|
codeblock = m2,
|
|
|
|
end = '\n';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
if (options.omitExtraWLInCodeBlocks) {
|
|
|
|
end = '';
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
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-01-19 20:04:22 +08:00
|
|
|
codeblock = '<pre><code' + (language ? ' class="' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(codeblock, options, globals);
|
|
|
|
});
|
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
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return text;
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// attacklab: Double up blank lines to reduce lookaround
|
|
|
|
text = text.replace(/\n/g, '\n\n');
|
|
|
|
|
|
|
|
// Hashify HTML blocks:
|
|
|
|
// We only want to do this for block-level HTML tags, such as headers,
|
|
|
|
// lists, and tables. That's because we still want to wrap <p>s around
|
|
|
|
// "paragraphs" that are wrapped in non-block-level tags, such as anchors,
|
|
|
|
// phrase emphasis, and spans. The list of tags we're looking for is
|
|
|
|
// hard-coded:
|
|
|
|
//var block_tags_a =
|
|
|
|
// 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del|style|section|header|footer|nav|article|aside';
|
|
|
|
// var block_tags_b =
|
|
|
|
// 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside';
|
|
|
|
|
|
|
|
// First, look for nested blocks, e.g.:
|
|
|
|
// <div>
|
|
|
|
// <div>
|
|
|
|
// tags for inner block must be indented.
|
|
|
|
// </div>
|
|
|
|
// </div>
|
|
|
|
//
|
|
|
|
// The outermost tags must start at the left margin for this to match, and
|
|
|
|
// the inner nested divs must be indented.
|
|
|
|
// We need to do this before the next, more liberal match, because the next
|
|
|
|
// match will start at the first `<div>` and stop at the first `</div>`.
|
|
|
|
|
|
|
|
// attacklab: This regex can be expensive when it fails.
|
|
|
|
/*
|
|
|
|
var text = text.replace(/
|
|
|
|
( // save in $1
|
|
|
|
^ // start of line (with /m)
|
|
|
|
<($block_tags_a) // start tag = $2
|
|
|
|
\b // word break
|
|
|
|
// attacklab: hack around khtml/pcre bug...
|
|
|
|
[^\r]*?\n // any number of lines, minimally matching
|
|
|
|
</\2> // the matching end tag
|
|
|
|
[ \t]* // trailing spaces/tabs
|
|
|
|
(?=\n+) // followed by a newline
|
|
|
|
) // attacklab: there are sentinel newlines at end of document
|
|
|
|
/gm,function(){...}};
|
|
|
|
*/
|
|
|
|
text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm,
|
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Now match more liberally, simply from `\n<tag>` to `</tag>\n`
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
var text = text.replace(/
|
|
|
|
( // save in $1
|
|
|
|
^ // start of line (with /m)
|
|
|
|
<($block_tags_b) // start tag = $2
|
|
|
|
\b // word break
|
|
|
|
// attacklab: hack around khtml/pcre bug...
|
|
|
|
[^\r]*? // any number of lines, minimally matching
|
|
|
|
</\2> // the matching end tag
|
|
|
|
[ \t]* // trailing spaces/tabs
|
|
|
|
(?=\n+) // followed by a newline
|
|
|
|
) // attacklab: there are sentinel newlines at end of document
|
|
|
|
/gm,function(){...}};
|
|
|
|
*/
|
2015-04-24 04:51:32 +08:00
|
|
|
text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside|address|audio|canvas|figure|hgroup|output|video)\b[^\r]*?<\/\2>[ \t]*(?=\n+)\n)/gm,
|
2015-01-19 20:04:22 +08:00
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
|
|
|
|
// Special case just for <hr />. It was easier to make a special case than
|
|
|
|
// to make the other regex more complicated.
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
( // save in $1
|
|
|
|
\n\n // Starting after a blank line
|
|
|
|
[ ]{0,3}
|
|
|
|
(<(hr) // start tag = $2
|
|
|
|
\b // word break
|
|
|
|
([^<>])*? //
|
|
|
|
\/?>) // the matching end tag
|
|
|
|
[ \t]*
|
|
|
|
(?=\n{2,}) // followed by a blank line
|
|
|
|
)
|
|
|
|
/g,showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
*/
|
|
|
|
text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
|
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
|
|
|
|
// Special case for standalone HTML comments:
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
( // save in $1
|
|
|
|
\n\n // Starting after a blank line
|
|
|
|
[ ]{0,3} // attacklab: g_tab_width - 1
|
|
|
|
<!
|
|
|
|
(--[^\r]*?--\s*)+
|
|
|
|
>
|
|
|
|
[ \t]*
|
|
|
|
(?=\n{2,}) // followed by a blank line
|
|
|
|
)
|
|
|
|
/g,showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
*/
|
|
|
|
text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g,
|
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
|
|
|
|
// PHP and ASP-style processor instructions (<?...?> and <%...%>)
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
(?:
|
|
|
|
\n\n // Starting after a blank line
|
|
|
|
)
|
|
|
|
( // save in $1
|
|
|
|
[ ]{0,3} // attacklab: g_tab_width - 1
|
|
|
|
(?:
|
|
|
|
<([?%]) // $2
|
|
|
|
[^\r]*?
|
|
|
|
\2>
|
|
|
|
)
|
|
|
|
[ \t]*
|
|
|
|
(?=\n{2,}) // followed by a blank line
|
|
|
|
)
|
|
|
|
/g,showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
*/
|
|
|
|
text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
|
|
|
|
showdown.subParser('hashElement')(text, options, globals));
|
|
|
|
|
|
|
|
// attacklab: Undo double lines (see comment at top of this function)
|
|
|
|
text = text.replace(/\n\n/g, '\n');
|
|
|
|
return text;
|
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';
|
|
|
|
|
2015-07-13 12:09:03 +08:00
|
|
|
var prefixHeader = options.prefixHeaderId,
|
|
|
|
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// Set text-style headers:
|
|
|
|
// Header 1
|
|
|
|
// ========
|
|
|
|
//
|
|
|
|
// Header 2
|
|
|
|
// --------
|
|
|
|
//
|
|
|
|
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, 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);
|
|
|
|
});
|
|
|
|
|
|
|
|
text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
text = text.replace(/
|
|
|
|
^(\#{1,6}) // $1 = string of #'s
|
|
|
|
[ \t]*
|
|
|
|
(.+?) // $2 = Header text
|
|
|
|
[ \t]*
|
|
|
|
\#* // optional closing #'s (not counted)
|
|
|
|
\n+
|
|
|
|
/gm, function() {...});
|
|
|
|
*/
|
|
|
|
|
2015-06-08 10:57:18 +08:00
|
|
|
text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, 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) {
|
|
|
|
var title, escapedId = m.replace(/[^\w]/g, '').toLowerCase();
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
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
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
altText = altText.replace(/"/g, '"');
|
2015-01-19 23:42:20 +08:00
|
|
|
url = showdown.helper.escapeCharacters(url, '*_', false);
|
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) {
|
2015-06-08 10:57:18 +08:00
|
|
|
title = title.replace(/"/g, '"');
|
|
|
|
title = showdown.helper.escapeCharacters(title, '*_', false);
|
|
|
|
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
|
|
|
|
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|
|
|
|
|
2015-07-11 22:45:58 +08:00
|
|
|
showdown.subParser('italicsAndBold', function (text, options) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +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
|
|
|
|
text = text.replace(/(\s)__(?=\S)([^]+?)__(?=\s)/g, '$1<strong>$2</strong>');
|
|
|
|
text = text.replace(/(\s)_(?=\S)([^]+?)_(?=\s)/g, '$1<em>$2</em>');
|
|
|
|
//asterisks
|
|
|
|
text = text.replace(/\*\*(?=\S)([^]+?)\*\*/g, '<strong>$1</strong>');
|
|
|
|
text = text.replace(/\*(?=\S)([^]+?)\*/g, '<em>$1</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-01-19 20:04:22 +08:00
|
|
|
return text;
|
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';
|
|
|
|
|
2015-06-13 21:59:14 +08:00
|
|
|
var spl = '~1';
|
|
|
|
|
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-06-13 21:59:14 +08:00
|
|
|
* @returns {string}
|
2015-01-19 20:04:22 +08:00
|
|
|
*/
|
2015-06-13 21:59:14 +08:00
|
|
|
function processListItems (listStr) {
|
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.".
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
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
|
|
|
|
2015-07-12 06:02:02 +08:00
|
|
|
var rgx = /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+((\[(x| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm;
|
|
|
|
|
|
|
|
listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) {
|
|
|
|
checked = (checked && checked.trim() !== '');
|
|
|
|
|
2015-06-13 21:59:14 +08:00
|
|
|
var item = showdown.subParser('outdent')(m4, options, globals);
|
|
|
|
|
2015-07-12 06:02:02 +08:00
|
|
|
//m1 - LeadingLine
|
2015-06-13 21:59:14 +08:00
|
|
|
if (m1 || (item.search(/\n{2,}/) > -1)) {
|
|
|
|
item = showdown.subParser('blockGamut')(item, options, globals);
|
|
|
|
} else {
|
2015-07-12 06:02:02 +08:00
|
|
|
if (taskbtn && options.tasklists) {
|
|
|
|
item = item.replace(taskbtn, function () {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-13 21:59:14 +08:00
|
|
|
// Recursion for sub-lists:
|
|
|
|
item = showdown.subParser('lists')(item, options, globals);
|
|
|
|
item = item.replace(/\n$/, ''); // chomp(item)
|
|
|
|
item = showdown.subParser('spanGamut')(item, options, globals);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a "hack" to differentiate between ordered and unordered lists
|
|
|
|
// related to issue #142
|
2015-07-12 06:02:02 +08:00
|
|
|
var tp = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol',
|
|
|
|
bulletStyle = '';
|
|
|
|
|
|
|
|
if (taskbtn) {
|
2015-07-12 06:09:47 +08:00
|
|
|
bulletStyle = ' class="task-list-item" style="list-style-type: none;"';
|
2015-07-12 06:02:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return spl + tp + '<li' + bulletStyle + '>' + item + '</li>\n';
|
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--;
|
|
|
|
return listStr;
|
2015-06-13 21:59:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Slit consecutive ol/ul lists (related to issue 142)
|
|
|
|
* @param {Array} results
|
|
|
|
* @param {string} listType
|
|
|
|
* @returns {string|*}
|
|
|
|
*/
|
|
|
|
function splitConsecutiveLists (results, listType) {
|
2015-07-12 06:02:02 +08:00
|
|
|
// parsing html with regex...
|
|
|
|
// This will surely fail if some extension decides to change paragraph markup directly
|
2015-06-13 21:59:14 +08:00
|
|
|
var cthulhu = /(<p[^>]+?>|<p>|<\/p>)/img,
|
|
|
|
holder = [[]],
|
|
|
|
res = '',
|
|
|
|
y = 0;
|
|
|
|
|
|
|
|
// Initialize first sublist
|
|
|
|
holder[0].type = listType;
|
|
|
|
|
|
|
|
for (var i = 0; i < results.length; ++i) {
|
|
|
|
var txt = results[i].slice(2),
|
|
|
|
nListType = results[i].slice(0, 2);
|
|
|
|
|
2015-06-17 08:26:50 +08:00
|
|
|
if (listType !== nListType) {
|
2015-06-13 21:59:14 +08:00
|
|
|
y++;
|
|
|
|
holder[y] = [];
|
|
|
|
holder[y].type = nListType;
|
|
|
|
listType = nListType;
|
|
|
|
}
|
|
|
|
holder[y].push(txt);
|
|
|
|
}
|
|
|
|
for (i = 0; i < holder.length; ++i) {
|
|
|
|
res += '<' + holder[i].type + '>\n';
|
|
|
|
for (var ii = 0; ii < holder[i].length; ++ii) {
|
|
|
|
if (holder[i].length > 1 && ii === holder[i].length - 1 && !cthulhu.test(holder[i][ii - 1])) {
|
|
|
|
//holder[i][ii] = holder[i][ii].replace(cthulhu, '');
|
|
|
|
}
|
|
|
|
res += holder[i][ii];
|
|
|
|
}
|
|
|
|
res += '</' + holder[i].type + '>\n';
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// attacklab: add sentinel to hack around khtml/safari bug:
|
|
|
|
// http://bugs.webkit.org/show_bug.cgi?id=11231
|
|
|
|
text += '~0';
|
|
|
|
|
2015-06-13 21:59:14 +08:00
|
|
|
// Re-usable pattern to match any entire ul or ol list:
|
2015-05-30 07:10:21 +08:00
|
|
|
var wholeList = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
if (globals.gListLevel) {
|
|
|
|
text = text.replace(wholeList, function (wholeMatch, m1, m2) {
|
2015-06-13 21:59:14 +08:00
|
|
|
var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol',
|
|
|
|
result = processListItems(m1);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// Turn double returns into triple returns, so that we can make a
|
|
|
|
// paragraph for the last item in a list, if necessary:
|
2015-06-13 21:59:14 +08:00
|
|
|
//list = list.replace(/\n{2,}/g, '\n\n\n');
|
|
|
|
//result = processListItems(list);
|
2015-01-19 20:04:22 +08:00
|
|
|
|
|
|
|
// Trim any trailing whitespace, to put the closing `</$list_type>`
|
|
|
|
// up on the preceding line, to get it past the current stupid
|
|
|
|
// HTML block parser. This is a hack to work around the terrible
|
|
|
|
// hack that is the HTML block parser.
|
|
|
|
result = result.replace(/\s+$/, '');
|
2015-06-13 21:59:14 +08:00
|
|
|
var splRes = result.split(spl);
|
|
|
|
splRes.shift();
|
|
|
|
result = splitConsecutiveLists(splRes, listType);
|
2015-01-19 20:04:22 +08:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
} else {
|
2015-05-30 07:10:21 +08:00
|
|
|
wholeList = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
|
2015-06-13 21:59:14 +08:00
|
|
|
//wholeList = /(\n\n|^\n?)( {0,3}([*+-]|\d+\.)[ \t]+[\s\S]+?)(?=(~0)|(\n\n(?!\t| {2,}| {0,3}([*+-]|\d+\.)[ \t])))/g;
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
text = text.replace(wholeList, function (wholeMatch, m1, m2, m3) {
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Turn double returns into triple returns, so that we can make a
|
|
|
|
// paragraph for the last item in a list, if necessary:
|
|
|
|
var list = m2.replace(/\n{2,}/g, '\n\n\n'),
|
2015-06-13 21:59:14 +08:00
|
|
|
//var list = (m2.slice(-2) !== '~0') ? m2 + '\n' : m2, //add a newline after the list
|
2015-01-19 20:04:22 +08:00
|
|
|
listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol',
|
2015-06-13 21:59:14 +08:00
|
|
|
result = processListItems(list),
|
|
|
|
splRes = result.split(spl);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-06-13 21:59:14 +08:00
|
|
|
splRes.shift();
|
|
|
|
return m1 + splitConsecutiveLists(splRes, listType) + '\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/, '');
|
|
|
|
|
|
|
|
return text;
|
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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
showdown.subParser('paragraphs', function (text, options, globals) {
|
2015-01-19 20:04:22 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (str.search(/~K(\d+)K/g) >= 0) {
|
|
|
|
grafsOut.push(str);
|
|
|
|
} else if (str.search(/\S/) >= 0) {
|
|
|
|
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++) {
|
|
|
|
// if this is a marker for an html block...
|
|
|
|
while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
|
|
|
|
var blockText = globals.gHtmlBlocks[RegExp.$1];
|
|
|
|
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
|
|
|
|
grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
|
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-01-19 20:04:22 +08:00
|
|
|
return grafsOut.join('\n\n');
|
2015-01-16 05:21:33 +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
|
|
|
});
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
// Do hard breaks:
|
|
|
|
text = text.replace(/ +\n/g, ' <br />\n');
|
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
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-07-11 22:59:06 +08:00
|
|
|
showdown.subParser('strikethrough', function (text, options) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if (options.strikethrough) {
|
|
|
|
text = text.replace(/(?:~T){2}([^~]+)(?:~T){2}/g, '<del>$1</del>');
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips link definitions from text, stores the URLs and titles in
|
|
|
|
* hash references.
|
|
|
|
* Link defs are in the form: ^[id]: url "optional title"
|
|
|
|
*
|
|
|
|
* ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
|
|
|
|
* [ \t]*
|
|
|
|
* \n? // maybe *one* newline
|
|
|
|
* [ \t]*
|
|
|
|
* <?(\S+?)>? // url = $2
|
|
|
|
* [ \t]*
|
|
|
|
* \n? // maybe one newline
|
|
|
|
* [ \t]*
|
|
|
|
* (?:
|
|
|
|
* (\n*) // any lines skipped = $3 attacklab: lookbehind removed
|
|
|
|
* ["(]
|
|
|
|
* (.+?) // title = $4
|
|
|
|
* [")]
|
|
|
|
* [ \t]*
|
|
|
|
* )? // title is optional
|
|
|
|
* (?:\n+|$)
|
|
|
|
* /gm,
|
|
|
|
* function(){...});
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
showdown.subParser('tables', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var table = function () {
|
|
|
|
|
|
|
|
var tables = {},
|
|
|
|
filter;
|
|
|
|
|
2015-07-12 01:21:43 +08:00
|
|
|
tables.th = function (header, style) {
|
|
|
|
var id = '';
|
|
|
|
header = header.trim();
|
|
|
|
if (header === '') {
|
2015-07-11 23:44:24 +08:00
|
|
|
return '';
|
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
if (options.tableHeaderId) {
|
|
|
|
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
|
|
|
|
}
|
|
|
|
if (style.trim() === '') {
|
|
|
|
style = '';
|
|
|
|
} else {
|
|
|
|
style = ' style="' + style + '"';
|
|
|
|
}
|
|
|
|
return '<th' + id + style + '>' + header + '</th>';
|
2015-07-11 23:44:24 +08:00
|
|
|
};
|
|
|
|
|
2015-07-12 01:21:43 +08:00
|
|
|
tables.td = function (cell, style) {
|
|
|
|
var subText = showdown.subParser('spanGamut')(cell.trim(), options, globals);
|
|
|
|
if (style.trim() === '') {
|
|
|
|
style = '';
|
|
|
|
} else {
|
|
|
|
style = ' style="' + style + '"';
|
|
|
|
}
|
|
|
|
return '<td' + style + '>' + subText + '</td>';
|
2015-07-11 23:44:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
tables.ths = function () {
|
|
|
|
var out = '',
|
2015-07-12 01:21:43 +08:00
|
|
|
i = 0,
|
|
|
|
hs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < hs.length; i += 1) {
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.th(hs[i], style[i]) + '\n';
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.tds = function () {
|
2015-07-12 01:21:43 +08:00
|
|
|
var out = '',
|
|
|
|
i = 0,
|
|
|
|
ds = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < ds.length; i += 1) {
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.td(ds[i], style[i]) + '\n';
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.thead = function () {
|
|
|
|
var out,
|
2015-07-12 01:21:43 +08:00
|
|
|
hs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
out = '<thead>\n';
|
|
|
|
out += '<tr>\n';
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.ths.apply(this, [hs, style]);
|
2015-07-11 23:44:24 +08:00
|
|
|
out += '</tr>\n';
|
|
|
|
out += '</thead>\n';
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.tr = function () {
|
|
|
|
var out,
|
2015-07-12 01:21:43 +08:00
|
|
|
cs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
out = '<tr>\n';
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.tds.apply(this, [cs, style]);
|
2015-07-11 23:44:24 +08:00
|
|
|
out += '</tr>\n';
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
filter = function (text) {
|
|
|
|
var i = 0,
|
|
|
|
lines = text.split('\n'),
|
|
|
|
line,
|
|
|
|
hs,
|
|
|
|
out = [];
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < lines.length; i += 1) {
|
|
|
|
line = lines[i];
|
|
|
|
// looks like a table heading
|
|
|
|
if (line.trim().match(/^[|].*[|]$/)) {
|
|
|
|
line = line.trim();
|
2015-07-12 01:21:43 +08:00
|
|
|
|
|
|
|
var tbl = [],
|
|
|
|
align = lines[i + 1].trim(),
|
|
|
|
styles = [],
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
if (align.match(/^[|][-=|: ]+[|]$/)) {
|
|
|
|
styles = align.substring(1, align.length - 1).split('|');
|
|
|
|
for (j = 0; j < styles.length; ++j) {
|
|
|
|
styles[j] = styles[j].trim();
|
|
|
|
if (styles[j].match(/^[:][-=| ]+[:]$/)) {
|
|
|
|
styles[j] = 'text-align:center;';
|
|
|
|
|
|
|
|
} else if (styles[j].match(/^[-=| ]+[:]$/)) {
|
|
|
|
styles[j] = 'text-align:right;';
|
|
|
|
|
|
|
|
} else if (styles[j].match(/^[:][-=| ]+$/)) {
|
|
|
|
styles[j] = 'text-align:left;';
|
|
|
|
} else {
|
|
|
|
styles[j] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
tbl.push('<table>');
|
|
|
|
hs = line.substring(1, line.length - 1).split('|');
|
2015-07-12 01:21:43 +08:00
|
|
|
|
|
|
|
if (styles.length === 0) {
|
|
|
|
for (j = 0; j < hs.length; ++j) {
|
|
|
|
styles.push('text-align:left');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tbl.push(tables.thead.apply(this, [hs, styles]));
|
2015-07-11 23:44:24 +08:00
|
|
|
line = lines[++i];
|
|
|
|
if (!line.trim().match(/^[|][-=|: ]+[|]$/)) {
|
|
|
|
// not a table rolling back
|
|
|
|
line = lines[--i];
|
|
|
|
} else {
|
|
|
|
line = lines[++i];
|
|
|
|
tbl.push('<tbody>');
|
|
|
|
while (line.trim().match(/^[|].*[|]$/)) {
|
|
|
|
line = line.trim();
|
2015-07-12 01:21:43 +08:00
|
|
|
tbl.push(tables.tr.apply(this, [line.substring(1, line.length - 1).split('|'), styles]));
|
2015-07-11 23:44:24 +08:00
|
|
|
line = lines[++i];
|
|
|
|
}
|
|
|
|
tbl.push('</tbody>');
|
|
|
|
tbl.push('</table>');
|
|
|
|
// we are done with this table and we move along
|
|
|
|
out.push(tbl.join('\n'));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.push(line);
|
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
};
|
|
|
|
return {parse: filter};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.tables) {
|
|
|
|
var tableParser = table();
|
|
|
|
return tableParser.parse(text);
|
|
|
|
} else {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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-01-19 20:04:22 +08:00
|
|
|
define('showdown', function () {
|
|
|
|
'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
|
|
|
}
|
2015-05-28 19:50:57 +08:00
|
|
|
}).call(this);
|
2015-01-16 05:21:33 +08:00
|
|
|
//# sourceMappingURL=showdown.js.map
|