feat(makehtml.events): implements event system refactor for converter.makeHtml (#919)

* startrefactoring the event system

* refactor: blockquotes, code blocks and links refactored

* refactor codeblock to new event system

* refactor subparser until ghcode to new events

* finish adating ghcodeblock to new event

* add headings to new events

* add image to event system

* add emphasisAndStrong to event system

* fix wrong event name in emphasisAndStrong onEnd event

* spanGamut and build

* showdown.helper.event refactored to showdown.Event

* partial

* add links

* add metadata

* add strikethrough and table

* build

* add underline

* add unescapeSpecialChars

* small refactoring

* remove old tables parser

* add lists

* add simple event trigger tests

* build

* fix browserstack

* fix browserstack

* remove testing for ie11 and bumped firefox min version to 45

* fixes and closes #920

* build
This commit is contained in:
Estevão Soares dos Santos 2022-04-27 21:42:24 +01:00 committed by GitHub
parent fcbccd6448
commit 9f8c7199ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
74 changed files with 3946 additions and 1823 deletions

View File

@ -2,6 +2,9 @@
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
"indent": [2, 2, {"SwitchCase": 1, "VariableDeclarator": 2}],
"curly": [2, "all"],
@ -10,7 +13,6 @@
"quotes": [2, "single"],
"no-multi-str": 2,
"no-mixed-spaces-and-tabs": 2,
"no-trailing-spaces": 2,
"space-unary-ops": [2,
{
"nonwords": false,
@ -24,7 +26,6 @@
"eol-last": 2,
"space-before-function-paren": [2, "always"],
"array-bracket-spacing": [2, "never", {"singleValue": false}],
"space-in-parens": [2, "never"],
"no-multiple-empty-lines": 2
"space-in-parens": [2, "never"]
}
}

View File

@ -21,10 +21,10 @@ jobs:
- name: '🚚 Upgrade NPM'
run: npm install -g npm
- name: '⚙ Setup Node.js v17.x'
- name: '⚙ Setup Node.js v16.x'
uses: actions/setup-node@v2
with:
node-version: 17.x
node-version: 16.x
cache: 'npm'
- name: '📖 Get current package version'

View File

@ -33,6 +33,7 @@ module.exports = function (grunt) {
'src/options.js',
'src/showdown.js',
'src/helpers.js',
'src/event.js',
'src/subParsers/makehtml/*.js',
'src/subParsers/makemarkdown/*.js',
'src/converter.js',

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -1,102 +1,164 @@
# Event System
# Event System )
## Introduction
The Event System is the basis of the new Listener extensions, replacing the old extension system altogether.
The life cycle can be summarized as this:
- A subparsers emmits an event
- Each subparser can emmit a bunch of events (see list below)
- Extension A, which is a _Listener Extension_ registers and listens to a specific event
- An extension can only register to a specific event
- Extension A receives an event object and modifies it
- Certain properties of the event object can be changed, ehich will change the behavior or output of the subparser
- The extension then returns the event object
- The converter passes this event object to the next extension in the chain
Note:
## The Event Object
### Properties
## Events
#### matches (type: **object**)
This object holds the text captured by the subparser. The properties of this object
are different for each subparser.
**Properties whose name starts with `_` are readonly**.
Example:
```js
// blockquote onCapture event
{
_wholeMatch: "> some awesome quote",
blockquote: "some awesome quote"
}
```
## Basic Event
## Event Types
Events are raised when a subparser is run (or about to be run).
Within a subparser, the events always follow a certain order (sequence). For instance, **.before** events always run before **.captureStart**.
Each subparser raises several events sequentially:
1. **.start**: **always runs** except it subparser is disabled
### onStart
Raised when the **subparser has started**, but no capturing or any modification to the text was done.
**`<converter>.<subparser>.onStart`**: **always runs** except if the subparser is disabled
Raised when the **subparser has started**, but no capturing or any modification to the text was done.
**Always runs** (except if the subparser is deactivated through options).
**Always runs** (except if the subparser is deactivated through options).
***Properties***:
***Properties***:
| property | type | access | description |
|----------|-----------|------------|--------------------------------------------------------------------|
| input | string | read | The full text that was passed to the subparser |
| output | string | write | The full text with modification that will be passed along the chain|
| regexp | null | | |
| matches | null | | |
| property | type | access | description |
|------------|-----------|------------|--------------------------------------------------------------------|
| input | string | read | The full text that was passed to the subparser |
| output | string | write | The full text with modification that will be passed along the chain|
| regexp | null | | |
| matches | null | | |
| attributes | null | | |
Usually you would want to use this event if you wish to change the input to the subparser
2. **.captureStart**: *might not be run*;
Usually you would want to use this event if you wish to change the input to the subparser. Please note,
however, that the input text is the **complete text** that was fed to the converter.
### onCapture
**`<converter>.<subparser>.onCapture`**: *might not be run*;
Raised when a regex match is found and a capture was successful. Some normalization and modification
of the regex captured groups might be performed.
Might not be run if no regex match is found.
***Properties***:
| property | type | access | description |
|----------|-----------|------------|--------------------------------------------------------------------|
| input | string | read | The captured text |
| output | string | write | The text that will be passed to the subparser/other listeners |
| regexp | RegExp | readonly | Regular Expression used to capture groups |
| matches | object | read/write | Matches groups. Changes to this object are reflected in the output |
Usually you would want to use this event if you wish to modify a certain subparser behavior.
Exs: remove all title attributes from links; change indentation of code blocks; etc...
3. **.captureEnd**: *might not be run*;
Raised after the modifications to the captured text are done but before the replacement is introduced in the document.
Raised when a regex match is found and a capture was successful. Some normalization and modification
of the regex captured groups might be performed.
Might not be run if no regex match is found.
***Properties***:
| property | type | access | description |
|------------|----------|------------|--------------------------------------------------------------------|
| input | string | readonly | The captured text |
| output | string | write | null or well formed HTML (see note) |
| regexp | RegExp | readonly | Regular Expression used to capture groups |
| matches | object | read/write | Matches groups. Changes to this object are reflected in the output |
| attributes | object | read/write | Attributes to add to the HTML output |
Might not be run if no regex match is found.
***Properties***:
| property | type | access | description |
|------------|-----------|------------|--------------------------------------------------------------------------------|
| input | string | read | The captured text |
| output | string | write | The text that will be passed to the subparser/other listeners |
| regexp | RegExp | readonly | Regular Expression used to capture groups |
| matches | object | read/write | Keypairs of matches groups. Changes to this object are reflected in the output |
| attributes | object | read/write | Attributes to add to the HTML output |
Usually you would want to use this event if you wish to modify the subparser behavior, text or modify the HTML output
of the subparser.
**IMPORTANT NOTE**: Extensions listening to onCapture event should try to AVOID changing the output property.
Instead, they should modify the values of the matches and attributes objects. This is because
the **output property takes precedence over the matches objects** and **prevents showdown to call other subparsers**
inside the captured fragment.This means 3 very important things:
1. If something is passed as the output property, any changes to the matches and attributes objects will be ignored.
2. Changes made by other extensions to the matches or attributes objects will also be ignored
3. Showdown will not call other subparsers, such as encode code or span gamut in the text fragment, which may lead to
weird results.
```javascript
// showdown extension 1 that is listening to makehtml.blockquote.onCapture
function extension_1 (showdownEvent) {
// i'm bad and write to output
showdownEvent.output = '<blockquote>foo</blockquote>'; // must be an well formed html string
showdownEvent.matches.blockquote = 'some nice quote';
}
// showdown extension 1 that is also listening to makehtml.blockquote.onCapture
function extension_2 (showdownEvent) {
// I make blockquotes become bold
let quote = showdownEvent.matches.blockquote;
showdownEvent.matches.blockquote = '<strong>' + quote + '</strong>';
}
// the result will always be <blockquote>foo</blockquote>, regardless of the order of extension loading and call
```
### onHash
**`<converter>.<subparser>.onHash`**: *always runs*;
4. **.beforeHash**: *might not be run*;
Raised before the output is hashed.
Raised before the output is hashed. **Always runs** (except if the subparser was deactivated through options),
even if no hashing is performed.
Always run (except if the subparser was deactivated through options), even if no hashing is performed.
***Properties***:
***Properties***:
| property | type | access | description |
|----------|------------|------------|--------------------------------------------------------------------|
| input | string | read | The captured text |
| output | string | write | The text that will be passed to the subparser/other listeners |
| regexp | null | | |
| matches | null | | |
| property | type | access | description |
|------------|------------|------------|--------------------------------------------------------------------|
| input | string | read | The captured text |
| output | string | write | The text that will be passed to the subparser/other listeners |
| regexp | null | | |
| matches | null | | |
| attributes | null | | |
Usually you would want to use this event if you wish change the subparser's raw output before it is hashed.
### onEnd
Usually you would want to use this event if you wish change the subparser output before it is hashed
**`<converter>.<subparser>.onEnd`**: *always runs*;
5. **.end**: *always runs*;
Raised when the subparser has finished its work and is about to exit.
Raised when the subparser has finished its work and is about to exit.
Always runs (except if the subparser is deactivated through options).
Always runs (except if the subparser is deactivated through options).
***Properties***:
***Properties***:
| property | type | access | description |
|----------|-----------|------------|--------------------------------------------------------------------|
| input | string | read | The partial/full text with the subparser modifications |
| output | string | write | The text that will be passed to other subparsers |
| regexp | null | | |
| matches | null | | |
| property | type | access | description |
|----------|-----------|------------|--------------------------------------------------------------------|
| input | string | read | The full text with the subparser modifications (contains hashes) |
| output | string | write | The text that will be passed to other subparsers |
| regexp | null | | |
| matches | null | | |
Usually you would want to use this event if you wish change the subparser hashed output
Usually you would want to use this event if you want to run code or perform changes to the text after the subparser was
run and it's output was hashed. Keep in mind that the input is the full text and might contain hashed elements.
### Special Events
@ -147,3 +209,20 @@ These events are always guaranteed to be called, regardless of options or circum
As a rule of thumb,
## Events List
### blockquote
####
### metadata
## Common pitfalls
### Infinite loop
If you pass the input as output in a `onCapture` event, you might trigger an infinite loop.

View File

@ -18,10 +18,17 @@ module.exports = function (config) {
os: 'Windows',
os_version: '10'
},
bstack_firefox_windows: {
bstack_firefox_old_windows: {
base: 'BrowserStack',
browser: 'firefox',
browser_version: '44',
browser_version: '45',
os: 'Windows',
os_version: '10'
},
bstack_firefox_latest_windows: {
base: 'BrowserStack',
browser: 'firefox',
browser_version: '99',
os: 'Windows',
os_version: '10'
},
@ -32,6 +39,7 @@ module.exports = function (config) {
os: 'Windows',
os_version: '10'
},
/*
bstack_ie11_windows: {
base: 'BrowserStack',
browser: 'ie',
@ -39,6 +47,7 @@ module.exports = function (config) {
os: 'Windows',
os_version: '10'
},
*/
bstack_macos_safari: {
base: 'BrowserStack',
browser: 'safari',
@ -64,7 +73,7 @@ module.exports = function (config) {
}
},
browsers: ['bstack_chrome_windows', 'bstack_firefox_windows', 'bstack_ie11_windows', 'bstack_edge_windows', 'bstack_iphoneX', 'bstack_macos_safari', 'bstack_android'],
browsers: ['bstack_chrome_windows', 'bstack_firefox_old_windows', 'bstack_firefox_latest_windows', /*'bstack_ie11_windows',*/ 'bstack_edge_windows', 'bstack_iphoneX', 'bstack_macos_safari', 'bstack_android'],
frameworks: ['mocha', 'chai'],
reporters: ['dots', 'BrowserStack'],
files: [

View File

@ -15,7 +15,8 @@ module.exports = function (config) {
{ pattern: 'src/options.js'},
// tests
{ pattern: 'test/unit/showdown*.js' },
{ pattern: 'test/functional/showdown*.js' },
{ pattern: 'test/unit/showdown.events.js' },
//{ pattern: 'test/functional/makehtml/testsuite.*.js' },
],
reporters: ['progress'],
port: 9876, // karma web server port

View File

@ -11,7 +11,7 @@
showdown.Converter = function (converterOptions) {
'use strict';
var
let
/**
* Options used by this converter
* @private
@ -64,7 +64,7 @@ showdown.Converter = function (converterOptions) {
function _constructor () {
converterOptions = converterOptions || {};
for (var gOpt in globalOptions) {
for (let gOpt in globalOptions) {
if (globalOptions.hasOwnProperty(gOpt)) {
options[gOpt] = globalOptions[gOpt];
}
@ -72,7 +72,7 @@ showdown.Converter = function (converterOptions) {
// Merge options
if (typeof converterOptions === 'object') {
for (var opt in converterOptions) {
for (let opt in converterOptions) {
if (converterOptions.hasOwnProperty(opt)) {
options[opt] = converterOptions[opt];
}
@ -85,6 +85,8 @@ showdown.Converter = function (converterOptions) {
if (options.extensions) {
showdown.helper.forEach(options.extensions, _parseExtension);
}
options = showdown.helper.validateOptions(options);
}
/**
@ -205,35 +207,29 @@ showdown.Converter = function (converterOptions) {
}
function rTrimInputText (text) {
var rsp = text.match(/^\s*/)[0].length,
let rsp = text.match(/^\s*/)[0].length,
rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm');
return text.replace(rgx, '');
}
/**
*
* @param {string} evtName Event name
* @param {string} text Text
* @param {{}} options Converter Options
* @param {{}} globals Converter globals
* @param {{}} [pParams] extra params for event
* @returns showdown.helper.Event
* @private
* @param {showdown.Event} event
* @returns showdown.Event
*/
this._dispatch = function dispatch (evtName, text, options, globals, pParams) {
evtName = evtName.toLowerCase();
var params = pParams || {};
params.converter = this;
params.text = text;
params.options = options;
params.globals = globals;
var event = new showdown.helper.Event(evtName, text, params);
if (listeners.hasOwnProperty(evtName)) {
for (var ei = 0; ei < listeners[evtName].length; ++ei) {
var nText = listeners[evtName][ei](event);
if (nText && typeof nText !== 'undefined') {
event.setText(nText);
this.dispatch = function (event) {
if (!(event instanceof showdown.Event)) {
throw new TypeError('dispatch only accepts showdown.Event objects as param, but ' + typeof event + ' given');
}
event.converter = this;
if (listeners.hasOwnProperty(event.name)) {
for (let i = 0; i < listeners[event.name].length; ++i) {
let listRet = listeners[event.name][i](event);
if (showdown.helper.isString(listRet)) {
event.output = listRet;
event.input = listRet;
} else if (listRet instanceof showdown.Event && listRet.name === event.name) {
event = listRet;
}
}
}
@ -325,7 +321,7 @@ showdown.Converter = function (converterOptions) {
// run the sub parsers
text = showdown.subParser('makehtml.metadata')(text, options, globals);
text = showdown.subParser('makehtml.hashPreCodeTags')(text, options, globals);
text = showdown.subParser('makehtml.githubCodeBlocks')(text, options, globals);
text = showdown.subParser('makehtml.githubCodeBlock')(text, options, globals);
text = showdown.subParser('makehtml.hashHTMLBlocks')(text, options, globals);
text = showdown.subParser('makehtml.hashCodeTags')(text, options, globals);
text = showdown.subParser('makehtml.stripLinkDefinitions')(text, options, globals);

225
src/event.js Normal file
View File

@ -0,0 +1,225 @@
/**
* Created by Estevao on 31-05-2015.
*/
showdown.Event = class {
/**
* Creates a new showdown Event object
* @param {string} name
* @param {string} input
* @param {{}} [params]
* @param {string} params.output
* @param {RegExp} params.regexp
* @param {{}} params.matches
* @param {{}} params.attributes
* @param {{}} params.globals
* @param {{}} params.options
* @param {showdown.Converter} params.converter
*/
constructor (name, input, params) {
params = params || {};
let {output, regexp, matches, attributes, globals, options, converter} = params;
if (!showdown.helper.isString(name)) {
if (!showdown.helper.isString(name)) {
throw new TypeError('Event.name must be a string but ' + typeof name + ' given');
}
}
this._name = name.toLowerCase();
this.input = input;
this.output = output || input;
this.regexp = regexp || null;
this.matches = matches || {};
this.attributes = attributes || {};
this._globals = globals || {};
this._options = showdown.helper.cloneObject(options, true) || {};
this._converter = converter || undefined;
}
/** @returns {string} */
get name () {
return this._name;
}
/** @returns {string} */
get input () {
return this._input;
}
/** @param {string} value */
set input (value) {
if (!showdown.helper.isString(value)) {
throw new TypeError('Event.input must be a string but ' + typeof value + ' given');
}
this._input = value;
}
/** @returns {string} */
get output () {
return this._output;
}
/** @param {string|null} value */
set output (value) {
if (!showdown.helper.isString(value) && value !== null) {
throw new TypeError('Event.output must be a string but ' + typeof value + ' given');
}
this._output = value;
}
/** @returns {null|RegExp} */
get regexp () {
return this._regexp;
}
/** @param {null|RegExp} value */
set regexp (value) {
if (!(value instanceof RegExp) && value !== null) {
throw new TypeError('Event.regexp must be a RegExp object (or null) but ' + typeof value + ' given');
}
this._regexp = value;
}
/** @returns {{}} */
get matches () {
return this._matches;
}
/** @param {{}}value */
set matches (value) {
if (typeof value !== 'object') {
throw new TypeError('Event.matches must be an object (or null) but ' + typeof value + ' given');
}
this._matches = {};
for (let prop in value) {
if (value.hasOwnProperty(prop)) {
let descriptor = {};
if (/^_(.+)/.test(prop)) {
descriptor = {
enumerable: true,
configurable: false,
writable: false,
value: value[prop]
};
} else {
descriptor = {
enumerable: true,
configurable: false,
writable: true,
value: value[prop]
};
}
Object.defineProperty(this._matches, prop, descriptor);
}
}
}
/** @returns {{}} */
get attributes () {
return this._attributes;
}
/** @param {{}} value */
set attributes (value) {
if (typeof value !== 'object') {
throw new TypeError('Event.attributes must be an object (or null) but ' + typeof value + ' given');
}
this._attributes = value;
}
/** @param {showdown.Converter} converter */
set converter (converter) {
this._converter = converter;
}
/** @returns {showdown.Converter} */
get converter () {
return this._converter;
}
get options () {
return this._options;
}
get globals () {
return this._globals;
}
// FLUID INTERFACE
/**
*
* @param {string} value
* @returns {showdown.Event}
*/
setInput (value) {
this.input = value;
return this;
}
/**
*
* @param {string|null} value
* @returns {showdown.Event}
*/
setOutput (value) {
this.output = value;
return this;
}
/**
*
* @param {RegExp} value
* @returns {showdown.Event}
*/
setRegexp (value) {
this.regexp = value;
return this;
}
/**
*
* @param {{}}value
* @returns {showdown.Event}
*/
setMatches (value) {
this.matches = value;
return this;
}
/**
*
* @param {{}}value
* @returns {showdown.Event}
*/
setAttributes (value) {
this.attributes = value;
return this;
}
_setOptions (value) {
this._options = value;
return this;
}
_setGlobals (value) {
this._globals = value;
return this;
}
_setConverter (value) {
this.converter = value;
return this;
}
/**
* Legacy: Return the output text
* @returns {string}
*/
getText () {
return this.output;
}
getMatches () {
return this.matches;
}
};

View File

@ -10,7 +10,7 @@ if (typeof this === 'undefined' && typeof window !== 'undefined') {
showdown.helper.document = window.document;
} else {
if (typeof this.document === 'undefined' && typeof this.window === 'undefined') {
var jsdom = require('jsdom');
let jsdom = require('jsdom');
this.window = new jsdom.JSDOM('', {}).window; // jshint ignore:line
}
showdown.helper.document = this.window.document;
@ -19,7 +19,7 @@ if (typeof this === 'undefined' && typeof window !== 'undefined') {
/**
* Check if var is string
* @static
* @param {string} a
* @param {*} a
* @returns {boolean}
*/
showdown.helper.isString = function (a) {
@ -27,6 +27,16 @@ showdown.helper.isString = function (a) {
return (typeof a === 'string' || a instanceof String);
};
/**
* Check if var is a number
* @static
* @param {*} a
* @returns {boolean}
*/
showdown.helper.isNumber = function (a) {
return !isNaN(a);
};
/**
* Check if var is a function
* @static
@ -35,7 +45,7 @@ showdown.helper.isString = function (a) {
*/
showdown.helper.isFunction = function (a) {
'use strict';
var getType = {};
let getType = {};
return a && getType.toString.call(a) === '[object Function]';
};
@ -69,6 +79,20 @@ showdown.helper.isUndefined = function (value) {
return typeof value === 'undefined';
};
/**
* Check if value is an object (excluding arrays)
* @param {*} value
* @returns {boolean}
*/
showdown.helper.isObject = function (value) {
'use strict';
return (
typeof value === 'object' &&
!Array.isArray(value) &&
value !== null
);
};
/**
* ForEach helper function
* Iterates over Arrays and Objects (own properties only)
@ -94,11 +118,11 @@ showdown.helper.forEach = function (obj, callback) {
if (typeof obj.forEach === 'function') {
obj.forEach(callback);
} else if (showdown.helper.isArray(obj)) {
for (var i = 0; i < obj.length; i++) {
for (let i = 0; i < obj.length; i++) {
callback(obj[i], i, obj);
}
} else if (typeof (obj) === 'object') {
for (var prop in obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
callback(obj[prop], prop, obj);
}
@ -109,7 +133,7 @@ showdown.helper.forEach = function (obj, callback) {
};
/**
* Standardidize extension name
* Standardize extension name
* @static
* @param {string} s extension name
* @returns {string}
@ -121,7 +145,7 @@ showdown.helper.stdExtName = function (s) {
function escapeCharactersCallback (wholeMatch, m1) {
'use strict';
var charCodeToEscape = m1.charCodeAt(0);
let charCodeToEscape = m1.charCodeAt(0);
return '¨E' + charCodeToEscape + 'E';
}
@ -146,21 +170,21 @@ showdown.helper.escapeCharacters = function (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') + '])';
let regexString = '([' + charsToEscape.replace(/([\[\]\\])/g, '\\$1') + '])';
if (afterBackslash) {
regexString = '\\\\' + regexString;
}
var regex = new RegExp(regexString, 'g');
let regex = new RegExp(regexString, 'g');
text = text.replace(regex, escapeCharactersCallback);
return text;
};
var rgxFindMatchPos = function (str, left, right, flags) {
let rgxFindMatchPos = function (str, left, right, flags) {
'use strict';
var f = flags || '',
let 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, '')),
@ -178,7 +202,7 @@ var rgxFindMatchPos = function (str, left, right, flags) {
} else if (t) {
if (!--t) {
end = m.index + m[0].length;
var obj = {
let obj = {
left: {start: start, end: s},
match: {start: s, end: m.index},
right: {start: m.index, end: end},
@ -228,10 +252,10 @@ var rgxFindMatchPos = function (str, left, right, flags) {
showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) {
'use strict';
var matchPos = rgxFindMatchPos (str, left, right, flags),
let matchPos = rgxFindMatchPos (str, left, right, flags),
results = [];
for (var i = 0; i < matchPos.length; ++i) {
for (let 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),
@ -255,22 +279,22 @@ showdown.helper.replaceRecursiveRegExp = function (str, replacement, left, right
'use strict';
if (!showdown.helper.isFunction(replacement)) {
var repStr = replacement;
let repStr = replacement;
replacement = function () {
return repStr;
};
}
var matchPos = rgxFindMatchPos(str, left, right, flags),
let matchPos = rgxFindMatchPos(str, left, right, flags),
finalStr = str,
lng = matchPos.length;
if (lng > 0) {
var bits = [];
let bits = [];
if (matchPos[0].wholeMatch.start !== 0) {
bits.push(str.slice(0, matchPos[0].wholeMatch.start));
}
for (var i = 0; i < lng; ++i) {
for (let i = 0; i < lng; ++i) {
bits.push(
replacement(
str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end),
@ -309,7 +333,7 @@ showdown.helper.regexIndexOf = function (str, regex, fromIndex) {
if (!(regex instanceof RegExp)) {
throw 'InvalidArgumentError: second parameter of showdown.helper.regexIndexOf function must be an instance of RegExp';
}
var indexOf = str.substring(fromIndex || 0).search(regex);
let indexOf = str.substring(fromIndex || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (fromIndex || 0)) : indexOf;
};
@ -338,7 +362,8 @@ showdown.helper.splitAtIndex = function (str, index) {
*/
/*jshint bitwise: false*/
function xmur3 (str) {
for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) {
let h;
for (let i = 0, h = 1779033703 ^ str.length; i < str.length; i++) {
h = Math.imul(h ^ str.charCodeAt(i), 3432918353);
h = h << 13 | h >>> 19;
}
@ -359,7 +384,7 @@ function xmur3 (str) {
/*jshint bitwise: false*/
function mulberry32 (a) {
return function () {
var t = a += 0x6D2B79F5;
let t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
@ -376,7 +401,7 @@ function mulberry32 (a) {
*/
showdown.helper.encodeEmailAddress = function (mail) {
'use strict';
var encode = [
let encode = [
function (ch) {
return '&#' + ch.charCodeAt(0) + ';';
},
@ -389,14 +414,14 @@ showdown.helper.encodeEmailAddress = function (mail) {
];
// RNG seeded with mail, so that we can get determined results for each email.
var rand = mulberry32(xmur3(mail));
let rand = mulberry32(xmur3(mail));
mail = mail.replace(/./g, function (ch) {
if (ch === '@') {
// this *must* be encoded. I insist.
ch = encode[Math.floor(rand() * 2)](ch);
} else {
var r = rand();
let r = rand();
// roughly 10% raw, 45% hex, 45% dec
ch = (
r > 0.9 ? encode[2](ch) : r > 0.45 ? encode[1](ch) : encode[0](ch)
@ -440,7 +465,7 @@ showdown.helper.repeat = function (str, count) {
throw new RangeError('repeat count must not overflow maximum string size');
}
/*jshint bitwise: true*/
var maxCount = str.length * count;
let maxCount = str.length * count;
count = Math.floor(Math.log(count) / Math.log(2));
while (count) {
str += str;
@ -504,9 +529,9 @@ showdown.helper._hashHTMLSpan = function (html, globals) {
*/
showdown.helper.applyBaseUrl = function (baseUrl, url) {
// Only prepend if given a base URL and the path is not absolute.
if (baseUrl && !this.isAbsolutePath(url)) {
var urlResolve = require('url').resolve;
url = urlResolve(baseUrl, url);
if (baseUrl && baseUrl !== '' && !showdown.helper.isAbsolutePath(url)) {
let urlResolve = new showdown.helper.URLUtils(url, baseUrl);
url = urlResolve.href;
}
return url;
@ -523,77 +548,212 @@ showdown.helper.isAbsolutePath = function (path) {
return /(^([a-z]+:)?\/\/)|(^#)/i.test(path);
};
showdown.helper.URLUtils = function (url, baseURL) {
const pattern2 = /^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/;
let m = String(url)
.trim()
.match(pattern2);
if (!m) {
throw new RangeError();
}
let protocol = m[1] || '';
let username = m[2] || '';
let password = m[3] || '';
let host = m[4] || '';
let hostname = m[5] || '';
let port = m[6] || '';
let pathname = m[7] || '';
let search = m[8] || '';
let hash = m[9] || '';
if (baseURL !== undefined) {
let base = new showdown.helper.URLUtils(baseURL);
let flag = protocol === '' && host === '' && username === '';
if (flag && pathname === '' && search === '') {
search = base.search;
}
if (flag && pathname.charAt(0) !== '/') {
pathname = (pathname !== '' ? (((base.host !== '' || base.username !== '') && base.pathname === '' ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + pathname) : base.pathname);
}
// dot segments removal
let output = [];
pathname.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
pathname = output.join('').replace(/^\//, pathname.charAt(0) === '/' ? '/' : '');
if (flag) {
port = base.port;
hostname = base.hostname;
host = base.host;
password = base.password;
username = base.username;
}
if (protocol === '') {
protocol = base.protocol;
}
}
this.origin = protocol + (protocol !== '' || host !== '' ? '//' : '') + host;
this.href = protocol + (protocol !== '' || host !== '' ? '//' : '') + (username !== '' ? username + (password !== '' ? ':' + password : '') + '@' : '') + host + pathname + search + hash;
this.protocol = protocol;
this.username = username;
this.password = password;
this.host = host;
this.hostname = hostname;
this.port = port;
this.pathname = pathname;
this.search = search;
this.hash = hash;
};
/**
* Showdown's Event Object
* @param {string} name Name of the event
* @param {string} text Text
* @param {{}} params optional. params of the event
* @constructor
* Clones an object . If the second parameter is true, it deep clones the object.
* Note: It should not be used in other contexts than showdown, since this algorithm might fail for
* cyclic references, and dataypes such as Dates, RegExps, Typed Arrays, etc...
* @param {{}} obj Object to clone
* @param {boolean} [deep] [optional] If it should deep clone the object. Default is false
*/
showdown.helper.Event = function (name, text, params) {
showdown.helper.cloneObject = function (obj, deep) {
deep = !!deep;
if (obj === null || typeof (obj) !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj);
}
if (!deep) {
let newObj = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
}
if (typeof structuredClone === 'function') {
return structuredClone(obj);
} else {
// note: This is not a real deep clone, and might work in weird ways if used in a dif
//this is costly and should be used sparsly
return JSON.parse(JSON.stringify(obj));
}
};
/**
* Populate attributes in output text
* @param {{}} attributes
* @returns {string}
*/
showdown.helper._populateAttributes = function (attributes) {
let text = '';
if (!attributes || !showdown.helper.isObject(attributes)) {
return text;
}
for (let attr in attributes) {
if (attributes.hasOwnProperty(attr)) {
let key = attr,
val;
// since class is a javascript reserved word we use classes
if (attr === 'classes') {
key = 'class';
}
if (attributes[attr] === null || showdown.helper.isUndefined(attributes[attr])) {
val = null;
} else if (showdown.helper.isArray(attributes[attr])) {
val = attributes[attr].join(' ');
if (val === '') {
val = null;
}
} else if (showdown.helper.isString(attributes[attr])) {
val = attributes[attr];
} else if (showdown.helper.isNumber(attributes[attr])) {
val = String(attributes[attr]);
} else {
throw new TypeError('Attribute "' + attr + '" must be either an array or string but ' + typeof attributes[attr] + ' given');
}
// special attributes
switch (attr) {
// these attributes don't expect a value. If they are false, they should be removed. If they are true,
// they should be present but without any value
case 'checked':
case 'disabled':
if (val === true || val === 'true' || val === attr) {
text += ' ' + key;
}
// if falsy, they are just ignored
break;
// default behavior for all other attributes types
default:
text += (val === null) ? '' : ' ' + key + '="' + val + '"';
break;
}
}
}
return text;
};
/**
* Remove one level of line-leading tabs or spaces
* @param {string} text
* @returns {string}
*/
showdown.helper.outdent = function (text) {
'use strict';
var regexp = params.regexp || null;
var matches = params.matches || {};
var options = params.options || {};
var converter = params.converter || null;
var globals = params.globals || {};
/**
* Get the name of the event
* @returns {string}
*/
this.getName = function () {
return name;
};
this.getEventName = function () {
return name;
};
this._stopExecution = false;
this.parsedText = params.parsedText || null;
this.getRegexp = function () {
return regexp;
};
this.getOptions = function () {
return options;
};
this.getConverter = function () {
return converter;
};
this.getGlobals = function () {
return globals;
};
this.getCapturedText = function () {
if (!showdown.helper.isString(text)) {
return text;
};
}
return text.replace(/^(\t| {1,4})/gm, '');
};
this.getText = function () {
return text;
};
/**
* Validate options
* @param {{}} options
* @returns {{}}
*/
showdown.helper.validateOptions = function (options) {
if (!showdown.helper.isObject(options)) {
throw new TypeError('Options must be an object, but ' + typeof options + ' given');
}
this.setText = function (newText) {
text = newText;
};
let defaultOptions = getDefaultOpts(false);
this.getMatches = function () {
return matches;
};
for (let opt in defaultOptions) {
if (!defaultOptions.hasOwnProperty(opt)) {
continue;
}
this.setMatches = function (newMatches) {
matches = newMatches;
};
if (!options.hasOwnProperty(opt)) {
options[opt] = defaultOptions[opt].defaultValue;
}
this.preventDefault = function (bool) {
this._stopExecution = !bool;
};
// TODO: dirty code. think about this we refactoring options
switch (opt) {
case 'prefixHeaderId':
if (typeof options[opt] !== 'boolean' && !showdown.helper.isString(options[opt])) {
throw new TypeError('Option prefixHeaderId must be of type boolean or string but ' + typeof options[opt] + ' given');
}
break;
default:
if (typeof options[opt] !== defaultOptions[opt].type) {
throw new TypeError('Option ' + opt + ' must be of type ' + defaultOptions[opt].type + ' but ' + typeof options[opt] + ' given');
}
}
}
//options.headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
return options;
};
/**
@ -626,7 +786,7 @@ if (!Math.imul) {
// automatically handled for our convienence:
// 1. 0x003fffff /*opA & 0x000fffff*/ * 0x7fffffff /*opB*/ = 0x1fffff7fc00001
// 0x1fffff7fc00001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/
var result = (opA & 0x003fffff) * opB;
let result = (opA & 0x003fffff) * opB;
// 2. We can remove an integer coersion from the statement above because:
// 0x1fffff7fc00001 + 0xffc00000 = 0x1fffffff800001
// 0x1fffffff800001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/
@ -2432,24 +2592,24 @@ showdown.helper.emojis = {
'zzz': '\ud83d\udca4',
/* special emojis :P */
'atom': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/atom.png?v8">',
'basecamp': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/basecamp.png?v8">',
'basecampy': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/basecampy.png?v8">',
'bowtie': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/bowtie.png?v8">',
'electron': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/electron.png?v8">',
'feelsgood': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/feelsgood.png?v8">',
'finnadie': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/finnadie.png?v8">',
'goberserk': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/goberserk.png?v8">',
'godmode': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/godmode.png?v8">',
'hurtrealbad': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/hurtrealbad.png?v8">',
'neckbeard': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/neckbeard.png?v8">',
'octocat': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/octocat.png?v8">',
'rage1': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage1.png?v8">',
'rage2': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage2.png?v8">',
'rage3': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage3.png?v8">',
'rage4': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage4.png?v8">',
'shipit': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/shipit.png?v8">',
'suspect': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/suspect.png?v8">',
'trollface': '<img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/trollface.png?v8">',
'showdown': '<img width="20" height="20" align="absmiddle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAS1BMVEX///8jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS3b1q3b1q3b1q3b1q3b1q3b1q3b1q3b1q0565CIAAAAGXRSTlMAQHCAYCCw/+DQwPCQUBAwoHCAEP+wwFBgS2fvBgAAAUZJREFUeAHs1cGy7BAUheFFsEDw/k97VTq3T6ge2EmdM+pvrP6Iwd74XV9Kb52xuMU4/uc1YNgZLFOeV8FGdhGrNk5SEgUyPxAEdj4LlMRDyhVAMVEa2M7TBSeVZAFPdqHgzSZJwPKgcLFLAooHDJo4EDCw4gAtBoJA5UFj4Ng5LOGLwVXZuoIlji/jeQHFk7+baHxrCjeUwB9+s88KndvlhcyBN5BSkYNQIVVb4pV+Npm7hhuKDs/uMP5KxT3WzSNNLIuuoDpMmuAVMruMSeDyQBi24DTr43LAY7ILA1QYaWkgfHzFthYYzg67SQsCbB8GhJUEGCtO9n0rSaCLxgJQjS/JSgMTg2eBDEHAJ+H350AsjYNYscrErgI2e/l+mdR967TCX/v6N0EhPECYCP0i+IAoYQOE8BogNhQMEMdrgAQWHaMAAGi5I5euoY9NAAAAAElFTkSuQmCC">'
'atom': '<img alt="atom" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/atom.png?v8">',
'basecamp': '<img alt="basecamp" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/basecamp.png?v8">',
'basecampy': '<img alt="basecampy" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/basecampy.png?v8">',
'bowtie': '<img alt="bowtie" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/bowtie.png?v8">',
'electron': '<img alt="electron" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/electron.png?v8">',
'feelsgood': '<img alt="feelsgood" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/feelsgood.png?v8">',
'finnadie': '<img alt="finnadie" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/finnadie.png?v8">',
'goberserk': '<img alt="goberserk" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/goberserk.png?v8">',
'godmode': '<img alt="godmode" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/godmode.png?v8">',
'hurtrealbad': '<img alt="hurtrealbad" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/hurtrealbad.png?v8">',
'neckbeard': '<img alt="neckbeard" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/neckbeard.png?v8">',
'octocat': '<img alt="octocat" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/octocat.png?v8">',
'rage1': '<img alt="rage1" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage1.png?v8">',
'rage2': '<img alt="rage2" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage2.png?v8">',
'rage3': '<img alt="rage3" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage3.png?v8">',
'rage4': '<img alt="rage4" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/rage4.png?v8">',
'shipit': '<img alt="shipit" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/shipit.png?v8">',
'suspect': '<img alt="suspect" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/suspect.png?v8">',
'trollface': '<img alt="trollface" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/trollface.png?v8">',
'showdown': '<img alt="showdown" width="20" height="20" align="absmiddle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAS1BMVEX///8jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS3b1q3b1q3b1q3b1q3b1q3b1q3b1q3b1q0565CIAAAAGXRSTlMAQHCAYCCw/+DQwPCQUBAwoHCAEP+wwFBgS2fvBgAAAUZJREFUeAHs1cGy7BAUheFFsEDw/k97VTq3T6ge2EmdM+pvrP6Iwd74XV9Kb52xuMU4/uc1YNgZLFOeV8FGdhGrNk5SEgUyPxAEdj4LlMRDyhVAMVEa2M7TBSeVZAFPdqHgzSZJwPKgcLFLAooHDJo4EDCw4gAtBoJA5UFj4Ng5LOGLwVXZuoIlji/jeQHFk7+baHxrCjeUwB9+s88KndvlhcyBN5BSkYNQIVVb4pV+Npm7hhuKDs/uMP5KxT3WzSNNLIuuoDpMmuAVMruMSeDyQBi24DTr43LAY7ILA1QYaWkgfHzFthYYzg67SQsCbB8GhJUEGCtO9n0rSaCLxgJQjS/JSgMTg2eBDEHAJ+H350AsjYNYscrErgI2e/l+mdR967TCX/v6N0EhPECYCP0i+IAoYQOE8BogNhQMEMdrgAQWHaMAAGi5I5euoY9NAAAAAElFTkSuQmCC">'
};

View File

@ -5,7 +5,7 @@
function getDefaultOpts (simple) {
'use strict';
var defaultOptions = {
let defaultOptions = {
omitExtraWLInCodeBlocks: {
defaultValue: false,
describe: 'Omit the default extra whiteline added to code blocks',
@ -37,9 +37,9 @@ function getDefaultOpts (simple) {
type: 'boolean'
},
headerLevelStart: {
defaultValue: false,
defaultValue: 1,
describe: 'The header blocks level start',
type: 'integer'
type: 'number'
},
parseImgDimensions: {
defaultValue: false,
@ -172,7 +172,7 @@ function getDefaultOpts (simple) {
type: 'boolean'
},
relativePathBaseUrl: {
defaultValue: false,
defaultValue: '',
describe: 'Prepends a base URL to relative paths',
type: 'string'
},
@ -180,8 +180,8 @@ function getDefaultOpts (simple) {
if (simple === false) {
return JSON.parse(JSON.stringify(defaultOptions));
}
var ret = {};
for (var opt in defaultOptions) {
let ret = {};
for (let opt in defaultOptions) {
if (defaultOptions.hasOwnProperty(opt)) {
ret[opt] = defaultOptions[opt].defaultValue;
}
@ -191,9 +191,9 @@ function getDefaultOpts (simple) {
function allOptionsOn () {
'use strict';
var options = getDefaultOpts(true),
let options = getDefaultOpts(true),
ret = {};
for (var opt in options) {
for (let opt in options) {
if (options.hasOwnProperty(opt)) {
ret[opt] = true;
}

View File

@ -183,6 +183,14 @@ showdown.subParser = function (name, func) {
}
};
/**
*
* @returns {{}}
*/
showdown.getSubParserList = function () {
return parsers;
};
/**
* Gets or registers an extension
* @static

View File

@ -1,23 +1,37 @@
/**
* These are all the transformations that form block-level
* tags like paragraphs, headers, and list items.
*/
////
// makehtml/blockGamut.js
// Copyright (c) 2018 ShowdownJS
//
// These are all the transformations that form block-level
// tags like paragraphs, headers, and list items.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.blockGamut', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.blockGamut.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.blockGamut.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// we parse blockquotes first so that we can have headings and hrs
// inside blockquotes
text = showdown.subParser('makehtml.blockQuotes')(text, options, globals);
text = showdown.subParser('makehtml.headers')(text, options, globals);
text = showdown.subParser('makehtml.blockquote')(text, options, globals);
text = showdown.subParser('makehtml.heading')(text, options, globals);
// Do Horizontal Rules:
text = showdown.subParser('makehtml.horizontalRule')(text, options, globals);
text = showdown.subParser('makehtml.lists')(text, options, globals);
text = showdown.subParser('makehtml.codeBlocks')(text, options, globals);
text = showdown.subParser('makehtml.tables')(text, options, globals);
text = showdown.subParser('makehtml.list')(text, options, globals);
text = showdown.subParser('makehtml.codeBlock')(text, options, globals);
text = showdown.subParser('makehtml.table')(text, options, globals);
// We already ran _HashHTMLBlocks() before, in Markdown(), but that
// was to escape raw HTML in the original Markdown source. This time,
@ -26,7 +40,11 @@ showdown.subParser('makehtml.blockGamut', function (text, options, globals) {
text = showdown.subParser('makehtml.hashHTMLBlocks')(text, options, globals);
text = showdown.subParser('makehtml.paragraphs')(text, options, globals);
text = globals.converter._dispatch('makehtml.blockGamut.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.blockGamut.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,42 +0,0 @@
showdown.subParser('makehtml.blockQuotes', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.blockQuotes.before', text, options, globals).getText();
// add a couple extra lines after the text and endtext mark
text = text + '\n\n';
var rgx = /(^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+/gm;
if (options.splitAdjacentBlockquotes) {
rgx = /^ {0,3}>[\s\S]*?(?:\n\n)/gm;
}
text = text.replace(rgx, function (bq) {
// attacklab: hack around Konqueror 3.5.4 bug:
// "----------bug".replace(/^-/g,"") == "bug"
bq = bq.replace(/^[ \t]*>[ \t]?/gm, ''); // trim one level of quoting
// attacklab: clean up hack
bq = bq.replace(/¨0/g, '');
bq = bq.replace(/^[ \t]+$/gm, ''); // trim whitespace-only lines
bq = showdown.subParser('makehtml.githubCodeBlocks')(bq, options, globals);
bq = showdown.subParser('makehtml.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('makehtml.hashBlock')('<blockquote>\n' + bq + '\n</blockquote>', options, globals);
});
text = globals.converter._dispatch('makehtml.blockQuotes.after', text, options, globals).getText();
return text;
});

View File

@ -0,0 +1,94 @@
////
// makehtml/blockquote.js
// Copyright (c) 2018 ShowdownJS
//
// Transforms MD blockquotes into `<blockquote>` html entities
//
// Markdown uses email-style > characters for blockquoting.
// Markdown allows you to be lazy and only put the > before the first line of a hard-wrapped paragraph but
// it looks best if the text is hard wrapped with a > before every line.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.blockquote', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.blockquote.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// add a couple extra lines after the text and endtext mark
text = text + '\n\n';
let pattern = /(^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+/gm;
if (options.splitAdjacentBlockquotes) {
pattern = /^ {0,3}>[\s\S]*?\n\n/gm;
}
text = text.replace(pattern, function (bq) {
let otp,
attributes = {},
wholeMatch = bq;
// attacklab: hack around Konqueror 3.5.4 bug:
// "----------bug".replace(/^-/g,"") == "bug"
bq = bq.replace(/^[ \t]*>[ \t]?/gm, ''); // trim one level of quoting
// attacklab: clean up hack
bq = bq.replace(/¨0/g, '');
bq = bq.replace(/^[ \t]+$/gm, ''); // trim whitespace-only lines
let captureStartEvent = new showdown.Event('makehtml.blockquote.onCapture', bq);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
blockquote: bq
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
bq = captureStartEvent.matches.blockquote;
bq = showdown.subParser('makehtml.githubCodeBlock')(bq, options, globals);
bq = showdown.subParser('makehtml.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 (wm, m1) {
return m1.replace(/^ {2}/mg, '');
});
attributes = captureStartEvent.attributes;
otp = '<blockquote' + showdown.helper._populateAttributes(attributes) + '>\n' + bq + '\n</blockquote>';
}
let beforeHashEvent = new showdown.Event('makehtml.blockquote.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return showdown.subParser('makehtml.hashBlock')(otp, options, globals);
});
let afterEvent = new showdown.Event('makehtml.blockquote.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -0,0 +1,95 @@
////
// makehtml/codeBlock.js
// Copyright (c) 2022 ShowdownJS
//
// Process Markdown `<pre><code>` blocks.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.codeBlock', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.codeBlock.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '¨0';
let pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=¨0))/g;
text = text.replace(pattern, function (wholeMatch, m1, m2) {
let codeblock = m1,
nextChar = m2,
end = '\n',
otp,
attributes = {
pre: {},
code: {}
};
let captureStartEvent = new showdown.Event('makehtml.codeBlock.onCapture', codeblock);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
codeblock: codeblock
})
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
codeblock = captureStartEvent.matches.codeblock;
codeblock = showdown.helper.outdent(codeblock);
codeblock = showdown.subParser('makehtml.encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('makehtml.detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
attributes = captureStartEvent.attributes;
otp = '<pre><code>';
if (!showdown.helper.isUndefined(attributes)) {
otp = '<pre' + showdown.helper._populateAttributes(attributes.pre) + '>';
otp += '<code' + showdown.helper._populateAttributes(attributes.code) + '>';
}
if (options.omitExtraWLInCodeBlocks) {
end = '';
}
otp += codeblock + end + '</code></pre>';
}
let beforeHashEvent = new showdown.Event('makehtml.codeBlock.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return showdown.subParser('makehtml.hashBlock')(otp, options, globals) + nextChar;
});
// strip sentinel
text = text.replace(/¨0/, '');
let afterEvent = new showdown.Event('makehtml.codeBlock.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,38 +0,0 @@
/**
* Process Markdown `<pre><code>` blocks.
*/
showdown.subParser('makehtml.codeBlocks', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.codeBlocks.before', text, options, globals).getText();
// 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) {
var codeblock = m1,
nextChar = m2,
end = '\n';
codeblock = showdown.subParser('makehtml.outdent')(codeblock, options, globals);
codeblock = showdown.subParser('makehtml.encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('makehtml.detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
if (options.omitExtraWLInCodeBlocks) {
end = '';
}
codeblock = '<pre><code>' + codeblock + end + '</code></pre>';
return showdown.subParser('makehtml.hashBlock')(codeblock, options, globals) + nextChar;
});
// strip sentinel
text = text.replace(/¨0/, '');
text = globals.converter._dispatch('makehtml.codeBlocks.after', text, options, globals).getText();
return text;
});

View File

@ -0,0 +1,101 @@
////
// makehtml/codeSpan.js
// Copyright (c) 2018 ShowdownJS
//
// Transforms MD code spans into `<code>` html entities
//
// 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> ...
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.codeSpan', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.codeSpan.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
if (showdown.helper.isUndefined((text))) {
text = '';
}
let pattern = /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm;
text = text.replace(pattern, function (wholeMatch, m1, m2, c) {
let otp,
attributes = {};
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
let captureStartEvent = new showdown.Event('makehtml.codeSpan.onCapture', c);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
code: c
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = m1 + captureStartEvent.output;
} else {
c = captureStartEvent.matches.code;
c = showdown.subParser('makehtml.encodeCode')(c, options, globals);
otp = m1 + '<code' + showdown.helper._populateAttributes(attributes) + '>' + c + '</code>';
}
let beforeHashEvent = new showdown.Event('makehtml.codeSpan.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return showdown.subParser('makehtml.hashHTMLSpans')(otp, options, globals);
}
);
let afterEvent = new showdown.Event('makehtml.codeSpan.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,48 +0,0 @@
/**
*
* * 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('makehtml.codeSpans', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.codeSpans.before', text, options, globals).getText();
if (typeof (text) === 'undefined') {
text = '';
}
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('makehtml.encodeCode')(c, options, globals);
c = m1 + '<code>' + c + '</code>';
c = showdown.subParser('makehtml.hashHTMLSpans')(c, options, globals);
return c;
}
);
text = globals.converter._dispatch('makehtml.codeSpans.after', text, options, globals).getText();
return text;
});

View File

@ -1,6 +1,14 @@
/**
* Create a full HTML document from the processed markdown
*/
////
// makehtml/completeHTMLDocument.js
// Copyright (c) 2018 ShowdownJS
//
// Create a full HTML document from the processed markdown
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.completeHTMLDocument', function (text, options, globals) {
'use strict';
@ -8,9 +16,15 @@ showdown.subParser('makehtml.completeHTMLDocument', function (text, options, glo
return text;
}
text = globals.converter._dispatch('makehtml.completeHTMLDocument.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.completeHTMLDocument.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var doctype = 'html',
let doctype = 'html',
doctypeParsed = '<!DOCTYPE HTML>\n',
title = '',
charset = '<meta charset="utf-8">\n',
@ -25,7 +39,7 @@ showdown.subParser('makehtml.completeHTMLDocument', function (text, options, glo
}
}
for (var meta in globals.metadata.parsed) {
for (let meta in globals.metadata.parsed) {
if (globals.metadata.parsed.hasOwnProperty(meta)) {
switch (meta.toLowerCase()) {
case 'doctype':
@ -57,6 +71,11 @@ showdown.subParser('makehtml.completeHTMLDocument', function (text, options, glo
text = doctypeParsed + '<html' + lang + '>\n<head>\n' + title + charset + metadata + '</head>\n<body>\n' + text.trim() + '\n</body>\n</html>';
text = globals.converter._dispatch('makehtml.completeHTMLDocument.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.completeHTMLDocument.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,9 +1,24 @@
/**
* Convert all tabs to spaces
*/
////
// makehtml/detab.js
// Copyright (c) 2018 ShowdownJS
//
// Convert all tabs to spaces
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.detab', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.detab.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.detab.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// expand first n-1 tabs
text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
@ -28,6 +43,11 @@ showdown.subParser('makehtml.detab', function (text, options, globals) {
text = text.replace(/¨A/g, ' '); // g_tab_width
text = text.replace(/¨B/g, '');
text = globals.converter._dispatch('makehtml.detab.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.detab.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,3 +1,14 @@
////
// makehtml/ellipsis.js
// Copyright (c) 2018 ShowdownJS
//
// transform three dots (...) into ellipsis (…)
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.ellipsis', function (text, options, globals) {
'use strict';
@ -5,11 +16,21 @@ showdown.subParser('makehtml.ellipsis', function (text, options, globals) {
return text;
}
text = globals.converter._dispatch('makehtml.ellipsis.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.ellipsis.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
text = text.replace(/\.\.\./g, '…');
text = globals.converter._dispatch('makehtml.ellipsis.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.ellipsis.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,8 +1,15 @@
/**
* Turn emoji codes into emojis
*
* List of supported emojis: https://github.com/showdownjs/showdown/wiki/Emojis
*/
////
// makehtml/emoji.js
// Copyright (c) 2018 ShowdownJS
//
// Turn emoji codes into emojis
// List of supported emojis: https://github.com/showdownjs/showdown/wiki/Emojis
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.emoji', function (text, options, globals) {
'use strict';
@ -10,18 +17,56 @@ showdown.subParser('makehtml.emoji', function (text, options, globals) {
return text;
}
text = globals.converter._dispatch('makehtml.emoji.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.emoji.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var emojiRgx = /:([\S]+?):/g;
let pattern = /:(\S+?):/g;
text = text.replace(emojiRgx, function (wm, emojiCode) {
if (showdown.helper.emojis.hasOwnProperty(emojiCode)) {
return showdown.helper.emojis[emojiCode];
text = text.replace(pattern, function (wholeMatch, emojiCode) {
if (!showdown.helper.emojis.hasOwnProperty(emojiCode)) {
return wholeMatch;
}
return wm;
let otp = '';
let captureStartEvent = new showdown.Event('makehtml.emoji.onCapture', emojiCode);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
emojiCode: emojiCode
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
otp = showdown.helper.emojis[emojiCode];
}
let beforeHashEvent = new showdown.Event('makehtml.emoji.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
return beforeHashEvent.output;
});
text = globals.converter._dispatch('makehtml.emoji.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.emoji.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -0,0 +1,173 @@
////
// makehtml/emphasisAndStrong.js
// Copyright (c) 2022 ShowdownJS
//
// Transforms MD emphasis and strong into `<em>` and `<strong>` html entities
//
// Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.
// Text wrapped with one * or _ will be wrapped with an HTML <em> tag;
// double *s or _s will be wrapped with an HTML <strong> tag
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.emphasisAndStrong', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.emphasisAndStrong.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
/**
* @param {string} txt
* @param {string} tags
* @param {string} wholeMatch
* @param {RegExp} pattern
* @returns {string}
*/
function parseInside (txt, tags, wholeMatch, pattern) {
let otp = 'ERROR',
attributes,
subEventName;
switch (tags) {
case '<em>':
attributes = {
em: {}
};
subEventName = 'emphasis';
break;
case '<strong>':
attributes = {
strong: {}
};
subEventName = 'strong';
break;
case '<strong><em>':
attributes = {
em: {},
strong: {}
};
subEventName = 'emphasisAndStrong';
break;
default:
attributes = {};
subEventName = 'emphasisAndStrong';
break;
}
let captureStartEvent = new showdown.Event('makehtml.emphasisAndStrong.' + subEventName + '.onCapture', txt);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
text: txt
})
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
if (showdown.helper.isUndefined(attributes.em)) {
attributes.em = {};
}
if (showdown.helper.isUndefined(attributes.strong)) {
attributes.strong = {};
}
switch (tags) {
case '<em>':
otp = '<em' + showdown.helper._populateAttributes(attributes.em) + '>' + txt + '</em>';
break;
case '<strong>':
otp = '<strong' + showdown.helper._populateAttributes(attributes.strong) + '>' + txt + '</strong>';
break;
case '<strong><em>':
otp = '<strong' + showdown.helper._populateAttributes(attributes.strong) + '>' +
'<em' + showdown.helper._populateAttributes(attributes.em) + '>' +
txt +
'</em>' +
'</strong>';
break;
}
}
let beforeHashEvent = new showdown.Event('makehtml.emphasisAndStrong.' + subEventName + '.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return otp;
}
// it's faster to have 3 separate regexes for each case than have just one
// because of backtracking, in some cases, it could lead to an exponential effect
// called "catastrophic backtrace". Ominous!
const lmwuStrongEmRegex = /\b___(\S[\s\S]*?)___\b/g,
lmwuStrongRegex = /\b__(\S[\s\S]*?)__\b/g,
lmwuEmRegex = /\b_(\S[\s\S]*?)_\b/g,
underscoreStrongEmRegex = /___(\S[\s\S]*?)___/g,
unserscoreStrongRegex = /__(\S[\s\S]*?)__/g,
unserscoreEmRegex = /_([^\s_][\s\S]*?)_/g,
asteriskStrongEm = /\*\*\*(\S[\s\S]*?)\*\*\*/g,
asteriskStrong = /\*\*(\S[\s\S]*?)\*\*/g,
asteriskEm = /\*([^\s*][\s\S]*?)\*/g;
// Parse underscores
if (options.literalMidWordUnderscores) {
text = text.replace(lmwuStrongEmRegex, function (wm, txt) {
return parseInside (txt, '<strong><em>', wm, lmwuStrongEmRegex);
});
text = text.replace(lmwuStrongRegex, function (wm, txt) {
return parseInside (txt, '<strong>', wm, lmwuStrongRegex);
});
text = text.replace(lmwuEmRegex, function (wm, txt) {
return parseInside (txt, '<em>', wm, lmwuEmRegex);
});
} else {
text = text.replace(underscoreStrongEmRegex, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', wm, underscoreStrongEmRegex) : wm;
});
text = text.replace(unserscoreStrongRegex, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong>', wm, unserscoreStrongRegex) : wm;
});
text = text.replace(unserscoreEmRegex, function (wm, m) {
// !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? parseInside (m, '<em>', wm, unserscoreEmRegex) : wm;
});
}
// Now parse asterisks
text = text.replace(asteriskStrongEm, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', wm, asteriskStrongEm) : wm;
});
text = text.replace(asteriskStrong, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong>', wm, asteriskStrong) : wm;
});
text = text.replace(asteriskEm, function (wm, m) {
// !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? parseInside (m, '<em>', wm, asteriskEm) : wm;
});
//}
let afterEvent = new showdown.Event('makehtml.emphasisAndStrong.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,13 +1,28 @@
/**
* Smart processing for ampersands and angle brackets that need to be encoded.
*/
////
// makehtml/encodeAmpsAndAngles.js
// Copyright (c) 2018 ShowdownJS
//
// Smart processing for ampersands and angle brackets that need to be encoded.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.encodeAmpsAndAngles', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.encodeAmpsAndAngles.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.encodeAmpsAndAngles.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// 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, '&amp;');
text = text.replace(/&(?!#?[xX]?(?:[\da-fA-F]+|\w+);)/g, '&amp;');
// Encode naked <'s
text = text.replace(/<(?![a-z\/?$!])/gi, '&lt;');
@ -18,6 +33,11 @@ showdown.subParser('makehtml.encodeAmpsAndAngles', function (text, options, glob
// Encode >
text = text.replace(/>/g, '&gt;');
text = globals.converter._dispatch('makehtml.encodeAmpsAndAngles.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.encodeAmpsAndAngles.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,21 +1,42 @@
/**
* 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.
*/
////
// makehtml/encodeBackslashEscapes.js
// Copyright (c) 2018 ShowdownJS
//
// Returns the string, with after processing the following backslash escape sequences.
//
// 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.
//
// ***Author:***
// - attacklab
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.encodeBackslashEscapes', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.encodeBackslashEscapes.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.encodeBackslashEscapes.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback);
text = text.replace(/\\([`*_{}\[\]()>#+.!~=|:-])/g, showdown.helper.escapeCharactersCallback);
text = globals.converter._dispatch('makehtml.encodeBackslashEscapes.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.encodeBackslashEscapes.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,12 +1,26 @@
/**
* Encode/escape certain characters inside Markdown code runs.
* The point is that in code, these characters are literals,
* and lose their special Markdown meanings.
*/
////
// makehtml/emoji.js
// Copyright (c) 2018 ShowdownJS
//
// Encode/escape certain characters inside Markdown code runs.
// The point is that in code, these characters are literals,
// and lose their special Markdown meanings.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.encodeCode', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.encodeCode.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.encodeCode.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// Encode all ampersands; HTML entities are not
// entities within a Markdown code span.
@ -18,6 +32,11 @@ showdown.subParser('makehtml.encodeCode', function (text, options, globals) {
// Now, escape characters that are magic in Markdown:
.replace(/([*_{}\[\]\\=~-])/g, showdown.helper.escapeCharactersCallback);
text = globals.converter._dispatch('makehtml.encodeCode.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.encodeCode.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,14 +1,29 @@
/**
* Within tags -- meaning between < and > -- encode [\ ` * _ ~ =] so they
* don't conflict with their use in Markdown for code, italics and strong.
*/
////
// makehtml/escapeSpecialCharsWithinTagAttributes.js
// Copyright (c) 2018 ShowdownJS
//
// Within tags -- meaning between < and > -- encode [\ ` * _ ~ =] so they
// don't conflict with their use in Markdown for code, italics and strong.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.escapeSpecialCharsWithinTagAttributes', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.escapeSpecialCharsWithinTagAttributes.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.escapeSpecialCharsWithinTagAttributes.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// Build a regex to find HTML tags.
var tags = /<\/?[a-z\d_:-]+(?:[\s]+[\s\S]+?)?>/gi,
comments = /<!(--(?:(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--)>/gi;
let tags = /<\/?[a-z\d_:-]+(?:[\s]+[\s\S]+?)?>/gi,
comments = /<!(--(([^>-]|-[^>])([^-]|-[^-])*)--)>/gi;
text = text.replace(tags, function (wholeMatch) {
return wholeMatch
@ -21,6 +36,11 @@ showdown.subParser('makehtml.escapeSpecialCharsWithinTagAttributes', function (t
.replace(/([\\`*_~=|])/g, showdown.helper.escapeCharactersCallback);
});
text = globals.converter._dispatch('makehtml.escapeSpecialCharsWithinTagAttributes.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.escapeSpecialCharsWithinTagAttributes.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -0,0 +1,134 @@
////
// makehtml/githubCodeBlock.js
// Copyright (c) 2018 ShowdownJS
//
// 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
// ```
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.githubCodeBlock', function (text, options, globals) {
'use strict';
// early exit if option is not enabled
if (!options.ghCodeBlocks) {
return text;
}
let startEvent = new showdown.Event('makehtml.githubCodeBlock.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output + '¨0';
let pattern = /(?:^|\n) {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)\n {0,3}\1/g;
text = text.replace(pattern, function (wholeMatch, delim, language, codeblock) {
let end = (options.omitExtraWLInCodeBlocks) ? '' : '\n',
otp,
attributes = {
pre: {},
code: {},
};
let captureStartEvent = new showdown.Event('makehtml.githubCodeBlock.onCapture', codeblock);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_whoteMatch: wholeMatch,
codeblock: codeblock,
infostring: language
})
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
// First parse the github code block
let infostring = captureStartEvent.matches.infostring;
let lang = infostring.trim().split(' ')[0];
codeblock = captureStartEvent.matches.codeblock;
codeblock = showdown.subParser('makehtml.encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('makehtml.detab')(codeblock, options, globals);
codeblock = codeblock
.replace(/^\n+/g, '') // trim leading newlines
.replace(/\n+$/g, ''); // trim trailing whitespace
attributes = captureStartEvent.attributes;
otp = '<pre><code>';
if (!showdown.helper.isUndefined(attributes)) {
otp = '<pre' + showdown.helper._populateAttributes(attributes.pre) + '>';
// if the language has spaces followed by some other chars, according to the spec we should just ignore everything
// after the first space
if (infostring) {
if (!attributes.code) {
attributes.code = {};
}
if (!attributes.code.classes) {
attributes.code.classes = [];
}
if (attributes.code.classes) {
if (showdown.helper.isString(attributes.code.classes)) {
attributes.code.classes += ' ' + lang + ' language-' + lang;
} else if (showdown.helper.isArray(attributes.code.classes)) {
attributes.code.classes.push(lang);
attributes.code.classes.push('language-' + lang);
}
}
}
otp += '<code' + showdown.helper._populateAttributes(attributes.code) + '>';
}
if (options.omitExtraWLInCodeBlocks) {
end = '';
}
otp += codeblock + end + '</code></pre>';
}
let beforeHashEvent = new showdown.Event('makehtml.githubCodeBlock.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
otp = showdown.subParser('makehtml.hashBlock')(otp, 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: otp}) - 1) + 'G\n\n';
});
// attacklab: strip sentinel
text = text.replace(/¨0/, '');
let afterEvent = new showdown.Event('makehtml.githubCodeBlock.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,50 +0,0 @@
/**
* 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('makehtml.githubCodeBlocks', function (text, options, globals) {
'use strict';
// early exit if option is not enabled
if (!options.ghCodeBlocks) {
return text;
}
text = globals.converter._dispatch('makehtml.githubCodeBlocks.before', text, options, globals).getText();
text += '¨0';
text = text.replace(/(?:^|\n) {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)\n {0,3}\1/g, function (wholeMatch, delim, language, codeblock) {
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
// if the language has spaces followed by some other chars, according to the spec we should just ignore everything
// after the first space
language = language.trim().split(' ')[0];
// First parse the github code block
codeblock = showdown.subParser('makehtml.encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('makehtml.detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
codeblock = '<pre><code' + (language ? ' class="' + language + ' language-' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
codeblock = showdown.subParser('makehtml.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';
});
// attacklab: strip sentinel
text = text.replace(/¨0/, '');
return globals.converter._dispatch('makehtml.githubCodeBlocks.after', text, options, globals).getText();
});

View File

@ -1,8 +1,31 @@
////
// makehtml/hashBlock.js
// Copyright (c) 2018 ShowdownJS
//
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.hashBlock', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.hashBlock.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.hashBlock.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
text = text.replace(/(^\n+|\n+$)/g, '');
text = '\n\n¨K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
text = globals.converter._dispatch('makehtml.hashBlock.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.hashBlock.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,18 +1,37 @@
/**
* Hash and escape <code> elements that should not be parsed as markdown
*/
////
// makehtml/hashCodeTags.js
// Copyright (c) 2018 ShowdownJS
//
// Hash and escape <code> elements that should not be parsed as markdown
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.hashCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.hashCodeTags.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.hashCodeTags.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var repFunc = function (wholeMatch, match, left, right) {
var codeblock = left + showdown.subParser('makehtml.encodeCode')(match, options, globals) + right;
let repFunc = function (wholeMatch, match, left, right) {
let codeblock = left + showdown.subParser('makehtml.encodeCode')(match, options, globals) + right;
return '¨C' + (globals.gHtmlSpans.push(codeblock) - 1) + 'C';
};
// Hash naked <code>
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '<code\\b[^>]*>', '</code>', 'gim');
text = globals.converter._dispatch('makehtml.hashCodeTags.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.hashCodeTags.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -2,7 +2,7 @@ showdown.subParser('makehtml.hashElement', function (text, options, globals) {
'use strict';
return function (wholeMatch, m1) {
var blockText = m1;
let blockText = m1;
// Undo double lines
blockText = blockText.replace(/\n\n/g, '\n');

View File

@ -1,8 +1,25 @@
////
// makehtml/hashHTMLBlocks.js
// Copyright (c) 2018 ShowdownJS
//
// Hash HTML blocks
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.hashHTMLBlocks', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.hashHTMLBlocks.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.hashHTMLBlocks.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var blockTags = [
let blockTags = [
'pre',
'div',
'h1',
@ -40,7 +57,7 @@ showdown.subParser('makehtml.hashHTMLBlocks', function (text, options, globals)
'p'
],
repFunc = function (wholeMatch, match, left, right) {
var txt = wholeMatch;
let 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) {
@ -57,9 +74,9 @@ showdown.subParser('makehtml.hashHTMLBlocks', function (text, options, globals)
}
// hash HTML Blocks
for (var i = 0; i < blockTags.length; ++i) {
for (let i = 0; i < blockTags.length; ++i) {
var opTagPos,
let opTagPos,
rgx1 = new RegExp('^ {0,3}(<' + blockTags[i] + '\\b[^>]*>)', 'im'),
patLeft = '<' + blockTags[i] + '\\b[^>]*>',
patRight = '</' + blockTags[i] + '>';
@ -70,7 +87,7 @@ showdown.subParser('makehtml.hashHTMLBlocks', function (text, options, globals)
//2. Split the text in that position
var subTexts = showdown.helper.splitAtIndex(text, opTagPos),
let subTexts = showdown.helper.splitAtIndex(text, opTagPos),
//3. Match recursively
newSubText1 = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');
@ -94,6 +111,11 @@ showdown.subParser('makehtml.hashHTMLBlocks', function (text, options, globals)
text = text.replace(/\n\n( {0,3}<([?%])[^\r]*?\2>[ \t]*(?=\n{2,}))/g,
showdown.subParser('makehtml.hashElement')(text, options, globals));
text = globals.converter._dispatch('makehtml.hashHTMLBlocks.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.hashHTMLBlocks.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,9 +1,23 @@
/**
* Hash span elements that should not be parsed as markdown
*/
////
// makehtml/hashHTMLSpans.js
// Copyright (c) 2018 ShowdownJS
//
// Hash span elements that should not be parsed as markdown
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.hashHTMLSpans', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.hashHTMLSpans.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.hashHTMLSpans.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// Hash Self Closing tags
text = text.replace(/<[^>]+?\/>/gi, function (wm) {
@ -25,34 +39,11 @@ showdown.subParser('makehtml.hashHTMLSpans', function (text, options, globals) {
return showdown.helper._hashHTMLSpan(wm, globals);
});
text = globals.converter._dispatch('makehtml.hashHTMLSpans.after', text, options, globals).getText();
return text;
});
/**
* Unhash HTML spans
*/
showdown.subParser('makehtml.unhashHTMLSpans', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.unhashHTMLSpans.before', text, options, globals).getText();
for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
var repText = globals.gHtmlSpans[i],
// limiter to prevent infinite loop (assume 10 as limit for recurse)
limit = 0;
while (/¨C(\d+)C/.test(repText)) {
var num = RegExp.$1;
repText = repText.replace('¨C' + num + 'C', globals.gHtmlSpans[num]);
if (limit === 10) {
console.error('maximum nesting of 10 spans reached!!!');
break;
}
++limit;
}
text = text.replace('¨C' + i + 'C', repText);
}
text = globals.converter._dispatch('makehtml.unhashHTMLSpans.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.hashHTMLSpans.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,19 +1,38 @@
/**
* Hash and escape <pre><code> elements that should not be parsed as markdown
*/
////
// makehtml/githubCodeBlock.js
// Copyright (c) 2018 ShowdownJS
//
// Hash and escape <pre><code> elements that should not be parsed as markdown
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.hashPreCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.hashPreCodeTags.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.hashPreCodeTags.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var repFunc = function (wholeMatch, match, left, right) {
let repFunc = function (wholeMatch, match, left, right) {
// encode html entities
var codeblock = left + showdown.subParser('makehtml.encodeCode')(match, options, globals) + right;
let codeblock = left + showdown.subParser('makehtml.encodeCode')(match, options, globals) + right;
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
};
// Hash <pre><code>
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^ {0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^ {0,3}</code>\\s*</pre>', 'gim');
text = globals.converter._dispatch('makehtml.hashPreCodeTags.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.hashPreCodeTags.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,126 +0,0 @@
showdown.subParser('makehtml.headers', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.headers.before', text, options, globals).getText();
var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
// Set text-style headers:
// Header 1
// ========
//
// Header 2
// --------
//
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) {
var spanGamut = showdown.subParser('makehtml.spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
hLevel = headerLevelStart,
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
return showdown.subParser('makehtml.hashBlock')(hashBlock, options, globals);
});
text = text.replace(setextRegexH2, function (matchFound, m1) {
var spanGamut = showdown.subParser('makehtml.spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
hLevel = headerLevelStart + 1,
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
return showdown.subParser('makehtml.hashBlock')(hashBlock, options, globals);
});
// atx-style headers:
// # Header 1
// ## Header 2
// ## Header 2 with closing hashes ##
// ...
// ###### Header 6
//
var atxStyle = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
text = text.replace(atxStyle, function (wholeMatch, m1, m2) {
var hText = m2;
if (options.customizedHeaderId) {
hText = m2.replace(/\s?{([^{]+?)}\s*$/, '');
}
var span = showdown.subParser('makehtml.spanGamut')(hText, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
hLevel = headerLevelStart - 1 + m1.length,
header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';
return showdown.subParser('makehtml.hashBlock')(header, options, globals);
});
function headerId (m) {
var title,
prefix;
// It is separate from other options to allow combining prefix and customized
if (options.customizedHeaderId) {
var match = m.match(/{([^{]+?)}\s*$/);
if (match && match[1]) {
m = match[1];
}
}
title = m;
// Prefix id to prevent causing inadvertent pre-existing style matches.
if (showdown.helper.isString(options.prefixHeaderId)) {
prefix = options.prefixHeaderId;
} else if (options.prefixHeaderId === true) {
prefix = 'section-';
} else {
prefix = '';
}
if (!options.rawPrefixHeaderId) {
title = prefix + title;
}
if (options.ghCompatibleHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&amp;/g, '')
.replace(/¨T/g, '')
.replace(/¨D/g, '')
// replace rest of the chars (&~$ are repeated as they might have been escaped)
// borrowed from github's redcarpet (some they should produce similar results)
.replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
.toLowerCase();
} else if (options.rawHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&amp;/g, '&')
.replace(/¨T/g, '¨')
.replace(/¨D/g, '$')
// replace " and '
.replace(/["']/g, '-')
.toLowerCase();
} else {
title = title
.replace(/[^\w]/g, '')
.toLowerCase();
}
if (options.rawPrefixHeaderId) {
title = prefix + title;
}
if (globals.hashLinkCounts[title]) {
title = title + '-' + (globals.hashLinkCounts[title]++);
} else {
globals.hashLinkCounts[title] = 1;
}
return title;
}
text = globals.converter._dispatch('makehtml.headers.after', text, options, globals).getText();
return text;
});

View File

@ -0,0 +1,171 @@
////
// makehtml/blockquote.js
// Copyright (c) 2018 ShowdownJS
//
// Transforms MD headings into `<h#>` html entities
//
// Setext-style headers:
// Header 1
// ========
//
// Header 2
// --------
//
// atx-style headers:
// # Header 1
// ## Header 2
// ## Header 2 with closing hashes ##
// ...
// ###### Header 6
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.heading', function (text, options, globals) {
'use strict';
function parseHeader (pattern, wholeMatch, headingText, headingLevel, headingId) {
let captureStartEvent = new showdown.Event('makehtml.heading.onCapture', headingText),
otp;
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
heading: headingText
})
.setAttributes({
id: headingId
});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
headingText = captureStartEvent.matches.heading;
let spanGamut = showdown.subParser('makehtml.spanGamut')(headingText, options, globals),
attributes = captureStartEvent.attributes;
otp = '<h' + headingLevel + showdown.helper._populateAttributes(attributes) + '>' + spanGamut + '</h' + headingLevel + '>';
}
let beforeHashEvent = new showdown.Event('makehtml.heading.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return showdown.subParser('makehtml.hashBlock')(otp, options, globals);
}
let startEvent = new showdown.Event('makehtml.heading.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
let 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,
atxRegex = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
text = text.replace(setextRegexH1, function (wholeMatch, headingText) {
let id = (options.noHeaderId) ? null : showdown.subParser('makehtml.heading.id')(headingText, options, globals);
return parseHeader(setextRegexH1, wholeMatch, headingText, options.headerLevelStart, id);
});
text = text.replace(setextRegexH2, function (wholeMatch, headingText) {
let id = (options.noHeaderId) ? null : showdown.subParser('makehtml.heading.id')(headingText, options, globals);
return parseHeader(setextRegexH2, wholeMatch, headingText, options.headerLevelStart + 1, id);
});
text = text.replace(atxRegex, function (wholeMatch, m1, m2) {
let headingLevel = options.headerLevelStart - 1 + m1.length,
headingText = (options.customizedHeaderId) ? m2.replace(/\s?{([^{]+?)}\s*$/, '') : m2,
id = (options.noHeaderId) ? null : showdown.subParser('makehtml.heading.id')(m2, options, globals);
return parseHeader(setextRegexH2, wholeMatch, headingText, headingLevel, id);
});
let afterEvent = new showdown.Event('makehtml.heading.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});
showdown.subParser('makehtml.heading.id', function (m, options, globals) {
let title,
prefix;
// It is separate from other options to allow combining prefix and customized
if (options.customizedHeaderId) {
let match = m.match(/{([^{]+?)}\s*$/);
if (match && match[1]) {
m = match[1];
}
}
title = m;
// Prefix id to prevent causing inadvertent pre-existing style matches.
if (showdown.helper.isString(options.prefixHeaderId)) {
prefix = options.prefixHeaderId;
} else if (options.prefixHeaderId === true) {
prefix = 'section-';
} else {
prefix = '';
}
if (!options.rawPrefixHeaderId) {
title = prefix + title;
}
if (options.ghCompatibleHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&amp;/g, '')
.replace(/¨T/g, '')
.replace(/¨D/g, '')
// replace rest of the chars (&~$ are repeated as they might have been escaped)
// borrowed from github's redcarpet (so they should produce similar results)
.replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
.toLowerCase();
} else if (options.rawHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&amp;/g, '&')
.replace(/¨T/g, '¨')
.replace(/¨D/g, '$')
// replace " and '
.replace(/["']/g, '-')
.toLowerCase();
} else {
title = title
.replace(/[^\w]/g, '')
.toLowerCase();
}
if (options.rawPrefixHeaderId) {
title = prefix + title;
}
if (globals.hashLinkCounts[title]) {
title = title + '-' + (globals.hashLinkCounts[title]++);
} else {
globals.hashLinkCounts[title] = 1;
}
return title;
});

View File

@ -1,18 +1,86 @@
/**
* Turn Markdown horizontal rule shortcuts into <hr /> tags.
*
* Any 3 or more unindented consecutive hyphens, asterisks or underscores with or without a space beetween them
* in a single line is considered a horizontal rule
*/
////
// makehtml/blockquote.js
// Copyright (c) 2018 ShowdownJS
//
// Turn Markdown horizontal rule shortcuts into <hr /> tags.
//
// Any 3 or more unindented consecutive hyphens, asterisks or underscores with or without a space beetween them
// in a single line is considered a horizontal rule
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.horizontalRule', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.horizontalRule.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.horizontalRule.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
var key = showdown.subParser('makehtml.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('makehtml.horizontalRule.after', text, options, globals).getText();
return text;
const rgx1 = /^ {0,2}( ?-){3,}[ \t]*$/gm;
text = text.replace(/^ {0,2}( ?-){3,}[ \t]*$/gm, function (wholeMatch) {
return parse(rgx1, wholeMatch);
});
const rgx2 = /^ {0,2}( ?\*){3,}[ \t]*$/gm;
text = text.replace(/^ {0,2}( ?\*){3,}[ \t]*$/gm, function (wholeMatch) {
return parse(rgx2, wholeMatch);
});
const rgx3 = /^ {0,2}( ?\*){3,}[ \t]*$/gm;
text = text.replace(/^ {0,2}( ?_){3,}[ \t]*$/gm, function (wholeMatch) {
return parse(rgx3, wholeMatch);
});
let afterEvent = new showdown.Event('makehtml.horizontalRule.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
/**
*
* @param {RegExp} pattern
* @param {string} wholeMatch
* @returns {string}
*/
function parse (pattern, wholeMatch) {
let otp;
let captureStartEvent = new showdown.Event('makehtml.horizontalRule.onCapture', wholeMatch);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_whoteMatch: wholeMatch
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
otp = '<hr' + showdown.helper._populateAttributes(captureStartEvent.attributes) + ' />';
}
let beforeHashEvent = new showdown.Event('makehtml.horizontalRule.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
otp = showdown.subParser('makehtml.hashBlock')(otp, options, globals);
return otp;
}
});

View File

@ -0,0 +1,188 @@
////
// makehtml/blockquote.js
// Copyright (c) 2018 ShowdownJS
//
// Turn Markdown image into <img> tags.
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.image', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.image.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
let inlineRegExp = /!\[([^\]]*?)][ \t]*\([ \t]?<?(\S+?(?:\(\S*?\)\S*?)?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\5)?[ \t]?\)/g,
crazyRegExp = /!\[([^\]]*?)][ \t]*\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\5)?[ \t]?\)/g,
base64RegExp = /!\[([^\]]*?)][ \t]*\([ \t]?<?(data:.+?\/.+?;base64,[A-Za-z\d+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]/g,
refShortcutRegExp = /!\[([^\[\]]+)]/g;
// First, handle reference-style labeled images: ![alt text][id]
text = text.replace(referenceRegExp, function (wholeMatch, altText, linkId) {
return writeImageTag ('reference', referenceRegExp, wholeMatch, altText, null, linkId);
});
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
// base64 encoded images
text = text.replace(base64RegExp, function (wholeMatch, altText, url, width, height, m5, title) {
url = url.replace(/\s/g, '');
return writeImageTag ('inline', base64RegExp, wholeMatch, altText, url, null, width, height, title);
});
// cases with crazy urls like ./image/cat1).png
text = text.replace(crazyRegExp, function (wholeMatch, altText, url, width, height, m5, title) {
url = showdown.helper.applyBaseUrl(options.relativePathBaseUrl, url);
return writeImageTag ('inline', crazyRegExp, wholeMatch, altText, url, null, width, height, title);
});
// normal cases
text = text.replace(inlineRegExp, function (wholeMatch, altText, url, width, height, m5, title) {
url = showdown.helper.applyBaseUrl(options.relativePathBaseUrl, url);
return writeImageTag ('inline', inlineRegExp, wholeMatch, altText, url, null, width, height, title);
});
// handle reference-style shortcuts: ![img text]
text = text.replace(refShortcutRegExp, function (wholeMatch, altText) {
return writeImageTag ('reference', refShortcutRegExp, wholeMatch, altText);
});
let afterEvent = new showdown.Event('makehtml.image.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
/**
* @param {string} subEvtName
* @param {RegExp} pattern
* @param {string} wholeMatch
* @param {string} altText
* @param {string|null} [url]
* @param {string|null} [linkId]
* @param {string|null} [width]
* @param {string|null} [height]
* @param {string|null} [title]
* @returns {string}
*/
function writeImageTag (subEvtName, pattern, wholeMatch, altText, url, linkId, width, height, title) {
let gUrls = globals.gUrls,
gTitles = globals.gTitles,
gDims = globals.gDimensions,
matches = {
_wholeMatch: wholeMatch,
_altText: altText,
_linkId: linkId,
_url: url,
_width: width,
_height: height,
_title: title
},
otp,
attributes = {};
linkId = (linkId) ? linkId.toLowerCase() : null;
if (!title) {
title = null;
}
// Special case for explicit empty url
if (wholeMatch.search(/\(<?\s*>? ?(['"].*['"])?\)$/m) > -1) {
url = '';
} else if (showdown.helper.isUndefined(url) || url === '' || url === null) {
if (linkId === '' || linkId === null) {
// lower-case and turn embedded newlines into spaces
linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
}
url = '#' + linkId;
if (!showdown.helper.isUndefined(gUrls[linkId])) {
url = gUrls[linkId];
if (!showdown.helper.isUndefined(gTitles[linkId])) {
title = gTitles[linkId];
}
if (!showdown.helper.isUndefined(gDims[linkId])) {
width = gDims[linkId].width;
height = gDims[linkId].height;
}
} else {
return wholeMatch;
}
}
altText = altText
.replace(/"/g, '&quot;')
//altText = showdown.helper.escapeCharacters(altText, '*_', false);
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
//url = showdown.helper.escapeCharacters(url, '*_', false);
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
if (title && showdown.helper.isString(title)) {
title = title
.replace(/"/g, '&quot;')
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
}
if (width) {
width = (width === '*') ? 'auto' : width;
} else {
width = null;
}
if (height) {
height = (height === '*') ? 'auto' : height;
} else {
height = null;
}
let captureStartEvent = new showdown.Event('makehtml.image.' + subEvtName + '.onCapture', wholeMatch);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches(matches)
.setAttributes({
src: url,
alt: altText,
title: title,
width: width,
height: height
});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
otp = '<img' + showdown.helper._populateAttributes(attributes) + ' />';
}
let beforeHashEvent = new showdown.Event('makehtml.image.' + subEvtName + '.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return otp;
}
});

View File

@ -1,110 +0,0 @@
/**
* Turn Markdown image shortcuts into <img> tags.
*/
showdown.subParser('makehtml.images', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.images.before', text, options, globals).getText();
var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g,
base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g,
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;
function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) {
url = url.replace(/\s/g, '');
return writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title);
}
function writeImageTagBaseUrl (wholeMatch, altText, linkId, url, width, height, m5, title) {
url = showdown.helper.applyBaseUrl(options.relativePathBaseUrl, url);
return writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title);
}
function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
var gUrls = globals.gUrls,
gTitles = globals.gTitles,
gDims = globals.gDimensions;
linkId = linkId.toLowerCase();
if (!title) {
title = '';
}
// Special case for explicit empty url
if (wholeMatch.search(/\(<?\s*>? ?(['"].*['"])?\)$/m) > -1) {
url = '';
} else if (url === '' || url === null) {
if (linkId === '' || linkId === null) {
// lower-case and turn embedded newlines into spaces
linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
}
url = '#' + linkId;
if (!showdown.helper.isUndefined(gUrls[linkId])) {
url = gUrls[linkId];
if (!showdown.helper.isUndefined(gTitles[linkId])) {
title = gTitles[linkId];
}
if (!showdown.helper.isUndefined(gDims[linkId])) {
width = gDims[linkId].width;
height = gDims[linkId].height;
}
} else {
return wholeMatch;
}
}
altText = altText
.replace(/"/g, '&quot;')
//altText = showdown.helper.escapeCharacters(altText, '*_', false);
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
//url = showdown.helper.escapeCharacters(url, '*_', false);
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
var result = '<img src="' + url + '" alt="' + altText + '"';
if (title && showdown.helper.isString(title)) {
title = title
.replace(/"/g, '&quot;')
//title = showdown.helper.escapeCharacters(title, '*_', false);
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
result += ' title="' + title + '"';
}
if (width && height) {
width = (width === '*') ? 'auto' : width;
height = (height === '*') ? 'auto' : height;
result += ' width="' + width + '"';
result += ' height="' + height + '"';
}
result += ' />';
return result;
}
// First, handle reference-style labeled images: ![alt text][id]
text = text.replace(referenceRegExp, writeImageTag);
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
// base64 encoded images
text = text.replace(base64RegExp, writeImageTagBase64);
// cases with crazy urls like ./image/cat1).png
text = text.replace(crazyRegExp, writeImageTagBaseUrl);
// normal cases
text = text.replace(inlineRegExp, writeImageTagBaseUrl);
// handle reference-style shortcuts: ![img text]
text = text.replace(refShortcutRegExp, writeImageTag);
text = globals.converter._dispatch('makehtml.images.after', text, options, globals).getText();
return text;
});

View File

@ -1,66 +0,0 @@
showdown.subParser('makehtml.italicsAndBold', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.italicsAndBold.before', text, options, globals).getText();
// it's faster to have 3 separate regexes for each case than have just one
// because of backtracing, in some cases, it could lead to an exponential effect
// called "catastrophic backtrace". Ominous!
function parseInside (txt, left, right) {
return left + txt + right;
}
// Parse underscores
if (options.literalMidWordUnderscores) {
text = text.replace(/\b___(\S[\s\S]*?)___\b/g, function (wm, txt) {
return parseInside (txt, '<strong><em>', '</em></strong>');
});
text = text.replace(/\b__(\S[\s\S]*?)__\b/g, function (wm, txt) {
return parseInside (txt, '<strong>', '</strong>');
});
text = text.replace(/\b_(\S[\s\S]*?)_\b/g, function (wm, txt) {
return parseInside (txt, '<em>', '</em>');
});
} else {
text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', '</em></strong>') : wm;
});
text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong>', '</strong>') : wm;
});
text = text.replace(/_([^\s_][\s\S]*?)_/g, function (wm, m) {
// !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? parseInside (m, '<em>', '</em>') : wm;
});
}
// Now parse asterisks
/*
if (options.literalMidWordAsterisks) {
text = text.replace(/([^*]|^)\B\*\*\*(\S[\s\S]+?)\*\*\*\B(?!\*)/g, function (wm, lead, txt) {
return parseInside (txt, lead + '<strong><em>', '</em></strong>');
});
text = text.replace(/([^*]|^)\B\*\*(\S[\s\S]+?)\*\*\B(?!\*)/g, function (wm, lead, txt) {
return parseInside (txt, lead + '<strong>', '</strong>');
});
text = text.replace(/([^*]|^)\B\*(\S[\s\S]+?)\*\B(?!\*)/g, function (wm, lead, txt) {
return parseInside (txt, lead + '<em>', '</em>');
});
} else {
*/
text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', '</em></strong>') : wm;
});
text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
return (/\S$/.test(m)) ? parseInside (m, '<strong>', '</strong>') : wm;
});
text = text.replace(/\*([^\s*][\s\S]*?)\*/g, function (wm, m) {
// !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? parseInside (m, '<em>', '</em>') : wm;
});
//}
text = globals.converter._dispatch('makehtml.italicsAndBold.after', text, options, globals).getText();
return text;
});

View File

@ -0,0 +1,323 @@
////
// makehtml/links.js
// Copyright (c) 2018 ShowdownJS
//
// Transforms MD links into `<a>` html anchors
//
// A link contains link text (the visible text), a link destination (the URI that is the link destination), and
// optionally a link title. There are two basic kinds of links in Markdown.
// In inline links the destination and title are given immediately after the link text.
// In reference links the destination and title are defined elsewhere in the document.
//
// ***Author:***
// - Estevão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.link', function (text, options, globals) {
//
// Parser starts here
//
let startEvent = new showdown.Event('makehtml.link.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// 1. Handle reference-style links: [link text] [id]
let referenceRegex = /\[((?:\[[^\]]*]|[^\[\]])*)] ?(?:\n *)?\[(.*?)]/g;
text = text.replace(referenceRegex, function (wholeMatch, text, linkId) {
// bail if we find 2 newlines somewhere
if (/\n\n/.test(wholeMatch)) {
return wholeMatch;
}
return writeAnchorTag ('reference', referenceRegex, wholeMatch, text, linkId);
});
// 2. Handle inline-style links: [link text](url "optional title")
// 2.1. Look for empty cases: []() and [empty]() and []("title")
let inlineEmptyRegex = /\[(.*?)]\(<? ?>? ?(["'](.*)["'])?\)/g;
text = text.replace(inlineEmptyRegex, function (wholeMatch, text, m1, title) {
return writeAnchorTag ('inline', inlineEmptyRegex, wholeMatch, text, null, null, title, true);
});
// 2.2. Look for cases with crazy urls like ./image/cat1).png
// the url mus be enclosed in <>
let inlineCrazyRegex = /\[((?:\[[^\]]*]|[^\[\]])*)]\s?\([ \t]?<([^>]*)>(?:[ \t]*((["'])([^"]*?)\4))?[ \t]?\)/g;
text = text.replace(inlineCrazyRegex, function (wholeMatch, text, url, m1, m2, title) {
return writeAnchorTag ('inline', inlineCrazyRegex, wholeMatch, text, null, url, title);
});
// 2.3. inline links with no title or titles wrapped in ' or ":
// [text](url.com) || [text](<url.com>) || [text](url.com "title") || [text](<url.com> "title")
let inlineNormalRegex1 = /\[([\S ]*?)]\s?\( *<?([^\s'"]*?(?:\(\S*?\)\S*?)?)>?\s*(?:(['"])(.*?)\3)? *\)/g;
text = text.replace(inlineNormalRegex1, function (wholeMatch, text, url, m1, title) {
return writeAnchorTag ('inline', inlineNormalRegex1, wholeMatch, text, null, url, title);
});
// 2.4. inline links with titles wrapped in (): [foo](bar.com (title))
let inlineNormalRegex2 = /\[([\S ]*?)]\s?\( *<?([^\s'"]*?(?:\(\S*?\)\S*?)?)>?\s+\((.*?)\) *\)/g;
text = text.replace(inlineNormalRegex2, function (wholeMatch, text, url, title) {
return writeAnchorTag ('inline', inlineNormalRegex2, wholeMatch, text, null, url, title);
});
// 3. Handle reference-style shortcuts: [link text]
// These must come last in case there's a [link text][1] or [link text](/foo)
let referenceShortcutRegex = /\[([^\[\]]+)]/g;
text = text.replace(referenceShortcutRegex, function (wholeMatch, text) {
return writeAnchorTag ('reference', referenceShortcutRegex, wholeMatch, text);
});
// 4. Handle angle brackets links -> `<http://example.com/>`
// Must come after links, because you can use < and > delimiters in inline links like [this](<url>).
// 4.1. Handle links first
let angleBracketsLinksRegex = /<(((?:https?|ftp):\/\/|www\.)[^'">\s]+)>/gi;
text = text.replace(angleBracketsLinksRegex, function (wholeMatch, url, urlStart) {
let text = url;
url = (urlStart === 'www.') ? 'http://' + url : url;
return writeAnchorTag ('angleBrackets', angleBracketsLinksRegex, wholeMatch, text, null, url);
});
// 4.2. Then mail adresses
let angleBracketsMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z\d]+(\.[-a-z\d]+)*\.[a-z]+)>/gi;
text = text.replace(angleBracketsMailRegex, function (wholeMatch, mail) {
const m = parseMail(mail);
return writeAnchorTag ('angleBrackets', angleBracketsMailRegex, wholeMatch, m.mail, null, m.url);
});
// 5. Handle GithubMentions (if option is enabled)
if (options.ghMentions) {
let ghMentionsRegex = /(^|\s)(\\)?(@([a-z\d]+(?:[a-z\d._-]+?[a-z\d]+)*))/gi;
text = text.replace(ghMentionsRegex, function (wholeMatch, st, escape, mentions, username) {
// bail if the mentions was escaped
if (escape === '\\') {
return st + mentions;
}
// check if options.ghMentionsLink is a string
// TODO Validation should be done at initialization not at runtime
if (!showdown.helper.isString(options.ghMentionsLink)) {
throw new Error('ghMentionsLink option must be a string');
}
let url = options.ghMentionsLink.replace(/\{u}/g, username);
return st + writeAnchorTag ('reference', ghMentionsRegex, wholeMatch, mentions, null, url);
});
}
// 6 and 7 have to come here to prevent naked links to catch html
// 6. Handle <a> tags
text = text.replace(/<a\s[^>]*>[\s\S]*<\/a>/g, function (wholeMatch) {
return showdown.helper._hashHTMLSpan(wholeMatch, globals);
});
// 7. Handle <img> tags
text = text.replace(/<img\s[^>]*\/?>/g, function (wholeMatch) {
return showdown.helper._hashHTMLSpan(wholeMatch, globals);
});
// 8. Handle naked links (if option is enabled)
if (options.simplifiedAutoLink) {
// 8.1. Check for naked URLs
// we also include leading markdown magic chars [_*~] for cases like __https://www.google.com/foobar__
let nakedUrlRegex = /([_*~]*?)(((?:https?|ftp):\/\/|www\.)[^\s<>"'`´.-][^\s<>"'`´]*?\.[a-z\d.]+[^\s<>"']*)\1/gi;
text = text.replace(nakedUrlRegex, function (wholeMatch, leadingMDChars, url, urlPrefix) {
// we now will start traversing the url from the front to back, looking for punctuation chars [_*~,;:.!?\)\]]
const len = url.length;
let suffix = '';
for (let i = len - 1; i >= 0; --i) {
let char = url.charAt(i);
if (/[_*~,;:.!?]/.test(char)) {
// it's a punctuation char so we remove it from the url
url = url.slice(0, -1);
// and prepend it to the suffix
suffix = char + suffix;
} else if (/[)\]]/.test(char)) {
// it's a parenthesis so we need to check for "balance" (kinda)
let opPar, clPar;
if (/\)/.test(char)) {
// it's a curved parenthesis
opPar = url.match(/\(/g) || [];
clPar = url.match(/\)/g);
} else {
// it's a squared parenthesis
opPar = url.match(/\[/g) || [];
clPar = url.match(/]/g);
}
if (opPar.length < clPar.length) {
// there are more closing Parenthesis than opening so chop it!!!!!
url = url.slice(0, -1);
// and prepend it to the suffix
suffix = char + suffix;
} else {
// it's (kinda) balanced so our work is done
break;
}
} else {
// it's not a punctuation or a parenthesis so our work is done
break;
}
}
// we copy the treated url to the text variable
let txt = url;
// finally, if it's a www shortcut, we prepend http
url = (urlPrefix === 'www.') ? 'http://' + url : url;
// url part is done so let's take care of text now
// we need to escape the text (because of links such as www.example.com/foo__bar__baz)
txt = txt.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
// and return the link tag, with the leadingMDChars and suffix. The leadingMDChars are added at the end too because
// we consumed those characters in the regexp
return leadingMDChars +
writeAnchorTag ('autoLink', nakedUrlRegex, wholeMatch, txt, null, url) +
suffix +
leadingMDChars;
});
// 8.2. Now check for naked mail
let nakedMailRegex = /(^|\s)(?:mailto:)?([A-Za-z\d!#$%&'*+-/=?^_`{|}~.]+@[-a-z\d]+(\.[-a-z\d]+)*\.[a-z]+)(?=$|\s)/gmi;
text = text.replace(nakedMailRegex, function (wholeMatch, leadingChar, mail) {
const m = parseMail(mail);
return leadingChar + writeAnchorTag ('autoLink', nakedMailRegex, wholeMatch, m.mail, null, m.url);
});
}
let afterEvent = new showdown.Event('makehtml.link.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
/**
*
* @param {string} subEvtName
* @param {RegExp} pattern
* @param {string} wholeMatch
* @param {string} text
* @param {string|null} [linkId]
* @param {string|null} [url]
* @param {string|null} [title]
* @param {boolean} [emptyCase]
* @returns {string}
*/
function writeAnchorTag (subEvtName, pattern, wholeMatch, text, linkId, url, title, emptyCase) {
let matches = {
_wholeMatch: wholeMatch,
_linkId: linkId,
_url: url,
_title: title,
text: text
},
otp,
attributes = {};
title = title || null;
url = url || null;
linkId = (linkId) ? linkId.toLowerCase() : null;
emptyCase = !!emptyCase;
if (emptyCase) {
url = '';
} else if (!url) {
if (!linkId) {
// lower-case and turn embedded newlines into spaces
linkId = text.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];
}
} else {
return wholeMatch;
}
}
url = showdown.helper.applyBaseUrl(options.relativePathBaseUrl, url);
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
attributes.href = url;
if (title && showdown.helper.isString(title)) {
title = title
.replace(/"/g, '&quot;')
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
attributes.title = title;
}
// optionLinksInNewWindow only applies
// to external links. Hash links (#) open in same page
if (options.openLinksInNewWindow && !/^#/.test(url)) {
attributes.rel = 'noopener noreferrer';
attributes.target = '¨E95Eblank'; // escaped _
}
let captureStartEvent = new showdown.Event('makehtml.link.' + subEvtName + '.onCapture', wholeMatch);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches(matches)
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
text = captureStartEvent.matches.text || '';
// Text can be a markdown element, so we run through the appropriate parsers
text = showdown.subParser('makehtml.codeSpan')(text, options, globals);
text = showdown.subParser('makehtml.emoji')(text, options, globals);
text = showdown.subParser('makehtml.underline')(text, options, globals);
text = showdown.subParser('makehtml.emphasisAndStrong')(text, options, globals);
text = showdown.subParser('makehtml.strikethrough')(text, options, globals);
text = showdown.subParser('makehtml.ellipsis')(text, options, globals);
text = showdown.subParser('makehtml.hashHTMLSpans')(text, options, globals);
otp = '<a' + showdown.helper._populateAttributes(attributes) + '>' + text + '</a>';
}
let beforeHashEvent = new showdown.Event('makehtml.link.' + subEvtName + '.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return showdown.subParser('makehtml.hashHTMLSpans')(otp, options, globals);
}
/**
* @param {string} mail
* @returns {{mail: string, url: string}}
*/
function parseMail (mail) {
let url = 'mailto:';
mail = showdown.subParser('makehtml.unescapeSpecialChars')(mail, options, globals);
if (options.encodeEmails) {
url = showdown.helper.encodeEmailAddress(url + mail);
mail = showdown.helper.encodeEmailAddress(mail);
} else {
url = url + mail;
}
return {
mail: mail,
url: url
};
}
});

View File

@ -1,428 +0,0 @@
////
// makehtml/links.js
// Copyright (c) 2018 ShowdownJS
//
// Transforms MD links into `<a>` html anchors
//
// A link contains link text (the visible text), a link destination (the URI that is the link destination), and
// optionally a link title. There are two basic kinds of links in Markdown.
// In inline links the destination and title are given immediately after the link text.
// In reference links the destination and title are defined elsewhere in the document.
//
// ***Author:***
// - Estevão Soares dos Santos (Tivie) <https://github.com/tivie>
////
(function () {
/**
* Helper function: Wrapper function to pass as second replace parameter
*
* @param {RegExp} rgx
* @param {string} evtRootName
* @param {{}} options
* @param {{}} globals
* @returns {Function}
*/
function replaceAnchorTagReference (rgx, evtRootName, options, globals, emptyCase) {
emptyCase = !!emptyCase;
return function (wholeMatch, text, id, url, m5, m6, title) {
// bail we we find 2 newlines somewhere
if (/\n\n/.test(wholeMatch)) {
return wholeMatch;
}
var evt = createEvent(rgx, evtRootName + '.captureStart', wholeMatch, text, id, url, title, options, globals);
return writeAnchorTag(evt, options, globals, emptyCase);
};
}
function replaceAnchorTagBaseUrl (rgx, evtRootName, options, globals, emptyCase) {
return function (wholeMatch, text, id, url, m5, m6, title) {
url = showdown.helper.applyBaseUrl(options.relativePathBaseUrl, url);
var evt = createEvent(rgx, evtRootName + '.captureStart', wholeMatch, text, id, url, title, options, globals);
return writeAnchorTag(evt, options, globals, emptyCase);
};
}
/**
* TODO Normalize this
* Helper function: Create a capture event
* @param {RegExp} rgx
* @param {String} evtName Event name
* @param {String} wholeMatch
* @param {String} text
* @param {String} id
* @param {String} url
* @param {String} title
* @param {{}} options
* @param {{}} globals
* @returns {showdown.helper.Event|*}
*/
function createEvent (rgx, evtName, wholeMatch, text, id, url, title, options, globals) {
return globals.converter._dispatch(evtName, wholeMatch, options, globals, {
regexp: rgx,
matches: {
wholeMatch: wholeMatch,
text: text,
id: id,
url: url,
title: title
}
});
}
/**
* Helper Function: Normalize and write an anchor tag based on passed parameters
* @param evt
* @param options
* @param globals
* @param {boolean} emptyCase
* @returns {string}
*/
function writeAnchorTag (evt, options, globals, emptyCase) {
var wholeMatch = evt.getMatches().wholeMatch;
var text = evt.getMatches().text;
var id = evt.getMatches().id;
var url = evt.getMatches().url;
var title = evt.getMatches().title;
var target = '';
if (!title) {
title = '';
}
id = (id) ? id.toLowerCase() : '';
if (emptyCase) {
url = '';
} else if (!url) {
if (!id) {
// lower-case and turn embedded newlines into spaces
id = text.toLowerCase().replace(/ ?\n/g, ' ');
}
url = '#' + id;
if (!showdown.helper.isUndefined(globals.gUrls[id])) {
url = globals.gUrls[id];
if (!showdown.helper.isUndefined(globals.gTitles[id])) {
title = globals.gTitles[id];
}
} else {
return wholeMatch;
}
}
//url = showdown.helper.escapeCharacters(url, '*_:~', false); // replaced line to improve performance
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
if (title !== '' && title !== null) {
title = title.replace(/"/g, '&quot;');
//title = showdown.helper.escapeCharacters(title, '*_', false); // replaced line to improve performance
title = title.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
title = ' title="' + title + '"';
}
// optionLinksInNewWindow only applies
// to external links. Hash links (#) open in same page
if (options.openLinksInNewWindow && !/^#/.test(url)) {
// escaped _
target = ' rel="noopener noreferrer" target="¨E95Eblank"';
}
// Text can be a markdown element, so we run through the appropriate parsers
text = showdown.subParser('makehtml.codeSpans')(text, options, globals);
text = showdown.subParser('makehtml.emoji')(text, options, globals);
text = showdown.subParser('makehtml.underline')(text, options, globals);
text = showdown.subParser('makehtml.italicsAndBold')(text, options, globals);
text = showdown.subParser('makehtml.strikethrough')(text, options, globals);
text = showdown.subParser('makehtml.ellipsis')(text, options, globals);
text = showdown.subParser('makehtml.hashHTMLSpans')(text, options, globals);
//evt = createEvent(rgx, evtRootName + '.captureEnd', wholeMatch, text, id, url, title, options, globals);
var result = '<a href="' + url + '"' + title + target + '>' + text + '</a>';
//evt = createEvent(rgx, evtRootName + '.beforeHash', wholeMatch, text, id, url, title, options, globals);
result = showdown.subParser('makehtml.hashHTMLSpans')(result, options, globals);
return result;
}
var evtRootName = 'makehtml.links';
/**
* Turn Markdown link shortcuts into XHTML <a> tags.
*/
showdown.subParser('makehtml.links', function (text, options, globals) {
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
// 1. Handle reference-style links: [link text] [id]
text = showdown.subParser('makehtml.links.reference')(text, options, globals);
// 2. Handle inline-style links: [link text](url "optional title")
text = showdown.subParser('makehtml.links.inline')(text, options, globals);
// 3. Handle reference-style shortcuts: [link text]
// These must come last in case there's a [link text][1] or [link text](/foo)
text = showdown.subParser('makehtml.links.referenceShortcut')(text, options, globals);
// 4. Handle angle brackets links -> `<http://example.com/>`
// Must come after links, because you can use < and > delimiters in inline links like [this](<url>).
text = showdown.subParser('makehtml.links.angleBrackets')(text, options, globals);
// 5. Handle GithubMentions (if option is enabled)
text = showdown.subParser('makehtml.links.ghMentions')(text, options, globals);
// 6. Handle <a> tags and img tags
text = text.replace(/<a\s[^>]*>[\s\S]*<\/a>/g, function (wholeMatch) {
return showdown.helper._hashHTMLSpan(wholeMatch, globals);
});
text = text.replace(/<img\s[^>]*\/?>/g, function (wholeMatch) {
return showdown.helper._hashHTMLSpan(wholeMatch, globals);
});
// 7. Handle naked links (if option is enabled)
text = showdown.subParser('makehtml.links.naked')(text, options, globals);
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.inline', function (text, options, globals) {
var evtRootName = evtRootName + '.inline';
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
// 1. Look for empty cases: []() and [empty]() and []("title")
var rgxEmpty = /\[(.*?)]()()()()\(<? ?>? ?(?:["'](.*)["'])?\)/g;
text = text.replace(rgxEmpty, replaceAnchorTagBaseUrl(rgxEmpty, evtRootName, options, globals, true));
// 2. Look for cases with crazy urls like ./image/cat1).png
var rgxCrazy = /\[((?:\[[^\]]*]|[^\[\]])*)]()\s?\([ \t]?<([^>]*)>(?:[ \t]*((["'])([^"]*?)\5))?[ \t]?\)/g;
text = text.replace(rgxCrazy, replaceAnchorTagBaseUrl(rgxCrazy, evtRootName, options, globals));
// 3. inline links with no title or titles wrapped in ' or ":
// [text](url.com) || [text](<url.com>) || [text](url.com "title") || [text](<url.com> "title")
//var rgx2 = /\[[ ]*[\s]?[ ]*([^\n\[\]]*?)[ ]*[\s]?[ ]*] ?()\(<?[ ]*[\s]?[ ]*([^\s'"]*)>?(?:[ ]*[\n]?[ ]*()(['"])(.*?)\5)?[ ]*[\s]?[ ]*\)/; // this regex is too slow!!!
var rgx2 = /\[([\S ]*?)]\s?()\( *<?([^\s'"]*?(?:\([\S]*?\)[\S]*?)?)>?\s*(?:()(['"])(.*?)\5)? *\)/g;
text = text.replace(rgx2, replaceAnchorTagBaseUrl(rgx2, evtRootName, options, globals));
// 4. inline links with titles wrapped in (): [foo](bar.com (title))
var rgx3 = /\[([\S ]*?)]\s?()\( *<?([^\s'"]*?(?:\([\S]*?\)[\S]*?)?)>?\s+()()\((.*?)\) *\)/g;
text = text.replace(rgx3, replaceAnchorTagBaseUrl(rgx3, evtRootName, options, globals));
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.reference', function (text, options, globals) {
var evtRootName = evtRootName + '.reference';
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
var rgx = /\[((?:\[[^\]]*]|[^\[\]])*)] ?(?:\n *)?\[(.*?)]()()()()/g;
text = text.replace(rgx, replaceAnchorTagReference(rgx, evtRootName, options, globals));
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.referenceShortcut', function (text, options, globals) {
var evtRootName = evtRootName + '.referenceShortcut';
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
var rgx = /\[([^\[\]]+)]()()()()()/g;
text = text.replace(rgx, replaceAnchorTagReference(rgx, evtRootName, options, globals));
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.ghMentions', function (text, options, globals) {
var evtRootName = evtRootName + 'ghMentions';
if (!options.ghMentions) {
return text;
}
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
var rgx = /(^|\s)(\\)?(@([a-z\d]+(?:[a-z\d._-]+?[a-z\d]+)*))/gi;
text = text.replace(rgx, function (wholeMatch, st, escape, mentions, username) {
// bail if the mentions was escaped
if (escape === '\\') {
return st + mentions;
}
// check if options.ghMentionsLink is a string
// TODO Validation should be done at initialization not at runtime
if (!showdown.helper.isString(options.ghMentionsLink)) {
throw new Error('ghMentionsLink option must be a string');
}
var url = options.ghMentionsLink.replace(/{u}/g, username);
var evt = createEvent(rgx, evtRootName + '.captureStart', wholeMatch, mentions, null, url, null, options, globals);
// captureEnd Event is triggered inside writeAnchorTag function
return st + writeAnchorTag(evt, options, globals);
});
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.angleBrackets', function (text, options, globals) {
var evtRootName = 'makehtml.links.angleBrackets';
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
// 1. Parse links first
var urlRgx = /<(((?:https?|ftp):\/\/|www\.)[^'">\s]+)>/gi;
text = text.replace(urlRgx, function (wholeMatch, url, urlStart) {
var text = url;
url = (urlStart === 'www.') ? 'http://' + url : url;
var evt = createEvent(urlRgx, evtRootName + '.captureStart', wholeMatch, text, null, url, null, options, globals);
return writeAnchorTag(evt, options, globals);
});
// 2. Then Mail Addresses
var mailRgx = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
text = text.replace(mailRgx, function (wholeMatch, mail) {
var url = 'mailto:';
mail = showdown.subParser('makehtml.unescapeSpecialChars')(mail, options, globals);
if (options.encodeEmails) {
url = showdown.helper.encodeEmailAddress(url + mail);
mail = showdown.helper.encodeEmailAddress(mail);
} else {
url = url + mail;
}
var evt = createEvent(mailRgx, evtRootName + '.captureStart', wholeMatch, mail, null, url, null, options, globals);
return writeAnchorTag(evt, options, globals);
});
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
/**
* TODO MAKE THIS WORK (IT'S NOT ACTIVATED)
* TODO WRITE THIS DOCUMENTATION
*/
showdown.subParser('makehtml.links.naked', function (text, options, globals) {
if (!options.simplifiedAutoLink) {
return text;
}
var evtRootName = 'makehtml.links.naked';
text = globals.converter._dispatch(evtRootName + '.start', text, options, globals).getText();
// 2. Now we check for
// we also include leading markdown magic chars [_*~] for cases like __https://www.google.com/foobar__
var urlRgx = /([_*~]*?)(((?:https?|ftp):\/\/|www\.)[^\s<>"'`´.-][^\s<>"'`´]*?\.[a-z\d.]+[^\s<>"']*)\1/gi;
text = text.replace(urlRgx, function (wholeMatch, leadingMDChars, url, urlPrefix) {
// we now will start traversing the url from the front to back, looking for punctuation chars [_*~,;:.!?\)\]]
var len = url.length;
var suffix = '';
for (var i = len - 1; i >= 0; --i) {
var char = url.charAt(i);
if (/[_*~,;:.!?]/.test(char)) {
// it's a punctuation char
// we remove it from the url
url = url.slice(0, -1);
// and prepend it to the suffix
suffix = char + suffix;
} else if (/\)/.test(char)) {
var opPar = url.match(/\(/g) || [];
var clPar = url.match(/\)/g);
// it's a curved parenthesis so we need to check for "balance" (kinda)
if (opPar.length < clPar.length) {
// there are more closing Parenthesis than opening so chop it!!!!!
url = url.slice(0, -1);
// and prepend it to the suffix
suffix = char + suffix;
} else {
// it's (kinda) balanced so our work is done
break;
}
} else if (/]/.test(char)) {
var opPar2 = url.match(/\[/g) || [];
var clPar2 = url.match(/\]/g);
// it's a squared parenthesis so we need to check for "balance" (kinda)
if (opPar2.length < clPar2.length) {
// there are more closing Parenthesis than opening so chop it!!!!!
url = url.slice(0, -1);
// and prepend it to the suffix
suffix = char + suffix;
} else {
// it's (kinda) balanced so our work is done
break;
}
} else {
// it's not a punctuation or a parenthesis so our work is done
break;
}
}
// we copy the treated url to the text variable
var text = url;
// finally, if it's a www shortcut, we prepend http
url = (urlPrefix === 'www.') ? 'http://' + url : url;
// url part is done so let's take care of text now
// we need to escape the text (because of links such as www.example.com/foo__bar__baz)
text = text.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
// finally we dispatch the event
var evt = createEvent(urlRgx, evtRootName + '.captureStart', wholeMatch, text, null, url, null, options, globals);
// and return the link tag, with the leadingMDChars and suffix. The leadingMDChars are added at the end too because
// we consumed those characters in the regexp
return leadingMDChars + writeAnchorTag(evt, options, globals) + suffix + leadingMDChars;
});
// 2. Then mails
var mailRgx = /(^|\s)(?:mailto:)?([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?=$|\s)/gmi;
text = text.replace(mailRgx, function (wholeMatch, leadingChar, mail) {
var url = 'mailto:';
mail = showdown.subParser('makehtml.unescapeSpecialChars')(mail, options, globals);
if (options.encodeEmails) {
url = showdown.helper.encodeEmailAddress(url + mail);
mail = showdown.helper.encodeEmailAddress(mail);
} else {
url = url + mail;
}
var evt = createEvent(mailRgx, evtRootName + '.captureStart', wholeMatch, mail, null, url, null, options, globals);
return leadingChar + writeAnchorTag(evt, options, globals);
});
text = globals.converter._dispatch(evtRootName + '.end', text, options, globals).getText();
return text;
});
})();

View File

@ -0,0 +1,406 @@
////
// makehtml/list.js
// Copyright (c) 2022 ShowdownJS
//
// Transforms MD lists into `<ul>` or `<ol>` html list
//
// Markdown supports ordered (numbered) and unordered (bulleted) lists.
// Unordered lists use asterisks, pluses, and hyphens - interchangably - as list markers
// Ordered lists use numbers followed by periods.
//
// ***Author:***
// - Estevão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.list', function (text, options, globals) {
'use strict';
// Start of list parsing
const subListRgx = /^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
const mainListRgx = /(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
const listTypeRgx = /[*+-]/g;
let startEvent = new showdown.Event('makehtml.list.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// add sentinel to hack around khtml/safari bug:
// http://bugs.webkit.org/show_bug.cgi?id=11231
text += '¨0';
if (globals.gListLevel) {
text = text.replace(subListRgx, function (wholeMatch, list, m2) {
return parseConsecutiveLists(subListRgx, list, (m2.search(listTypeRgx) > -1) ? 'ul' : 'ol', true);
});
} else {
text = text.replace(mainListRgx, function (wholeMatch, m1, list, m3) {
return parseConsecutiveLists(mainListRgx, list, (m3.search(listTypeRgx) > -1) ? 'ul' : 'ol', false);
});
}
// strip sentinel
text = text.replace(/¨0/, '');
let afterEvent = new showdown.Event('makehtml.list.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
/**
*
* @param {RegExp} pattern
* @param {string} item
* @param {boolean} checked
* @returns {string}
*/
function processTaskListItem (pattern, item, checked) {
const checkboxRgx = /^[ \t]*\[([xX ])]/m;
item = item.replace(checkboxRgx, function (wm, checkedRaw) {
let attributes = {
type: 'checkbox',
disabled: true,
style: 'margin: 0px 0.35em 0.25em -1.6em; vertical-align: middle;',
checked: !!checked
};
let captureStartEvent = new showdown.Event('makehtml.list.taskListItem.checkbox.onCapture', item);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: item,
_tasklistButton: wm,
_taksListButtonChecked: checkedRaw
})
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
let otp;
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
otp = '<input' + showdown.helper._populateAttributes(attributes) + '>';
}
let beforeHashEvent = new showdown.Event('makehtml.list.taskListItem.checkbox.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return otp;
});
return item;
}
/**
* Process the contents of a single ordered or unordered list, splitting it
* into individual list items.
* @param {string} listStr
* @param {boolean} trimTrailing
* @returns {string}
*/
function processListItems (listStr, trimTrailing) {
// 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++;
// trim trailing blank lines:
listStr = listStr.replace(/\n{2,}$/, '\n');
// attacklab: add sentinel to emulate \z
listStr += '¨0';
let rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[([xX ])])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0| {0,3}([*+-]|\d+[.])[ \t]+))/gm,
isParagraphed = (/\n[ \t]*\n(?!¨0)/.test(listStr));
// 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
// This will be removed in version 2.0
if (options.disableForced4SpacesIndentedSublists) {
rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[([xX ])])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0|\2([*+-]|\d+[.])[ \t]+))/gm;
}
listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checkedRaw) {
let item = showdown.helper.outdent(m4),
attributes = {},
checked = (checkedRaw && checkedRaw.trim() !== ''),
eventName = 'makehtml.list.listItem',
captureStartEvent,
matches = {
_wholeMatch: wholeMatch,
listItem: item,
};
// Support for github tasklists
if (taskbtn && options.tasklists) {
// it's a github tasklist and tasklists are enabled
// Style used for tasklist bullets
attributes.classes = ['task-list-item'];
attributes.style = 'list-style-type: none;';
if (options.moreStyling && checked) {
attributes.classes.push('task-list-item-complete');
}
eventName = 'makehtml.list.taskListItem';
matches._taskListButton = taskbtn;
matches._taskListButtonChecked = checkedRaw;
}
captureStartEvent = new showdown.Event(eventName + '.onCapture', item);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(rgx)
.setMatches(matches)
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
item = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
item = captureStartEvent.matches.listItem;
// even if user there's no tasklist, it's fine because the tasklist handler will bail without raising the event
if (options.tasklists) {
item = processTaskListItem(rgx, item, checked);
}
// 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
item = item.replace(/^([-*+]|\d\.)[ \t]+[\S\n ]*/g, function (wm2) {
return '¨A' + wm2;
});
// SPECIAL CASE: a heading followed by a paragraph of text that is not separated by a double newline
// or/nor indented. ex:
//
// - # foo
// bar is great
//
// While this does now follow the spec per se, not allowing for this might cause confusion since
// header blocks don't need double-newlines after
if (/^#+.+\n.+/.test(item)) {
item = item.replace(/^(#+.+)$/m, '$1\n');
}
// m1 - Leading line or
// Has a double return (multi paragraph)
if (m1 || (item.search(/\n{2,}/) > -1)) {
item = showdown.subParser('makehtml.githubCodeBlock')(item, options, globals);
item = showdown.subParser('makehtml.blockquote')(item, options, globals);
item = showdown.subParser('makehtml.heading')(item, options, globals);
item = showdown.subParser('makehtml.list')(item, options, globals);
item = showdown.subParser('makehtml.codeBlock')(item, options, globals);
item = showdown.subParser('makehtml.table')(item, options, globals);
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
//item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
// TODO: This is a copy of the paragraph parser
// This is a provisory fix for issue #494
// For a permanent fix we need to rewrite the paragraph parser, passing the unhashify logic outside
// so that we can call the paragraph parser without accidently unashifying previously parsed blocks
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
let grafs = item.split(/\n{2,}/g),
grafsOut = [],
end = grafs.length; // Wrap <p> tags
for (let i = 0; i < end; i++) {
let str = grafs[i];
// if this is an HTML marker, copy it
if (str.search(/¨([KG])(\d+)\1/g) >= 0) {
grafsOut.push(str);
// test for presence of characters to prevent empty lines being parsed
// as paragraphs (resulting in undesired extra empty paragraphs)
} else if (str.search(/\S/) >= 0) {
str = showdown.subParser('makehtml.spanGamut')(str, options, globals);
str = str.replace(/^([ \t]*)/g, '<p>');
str += '</p>';
grafsOut.push(str);
}
}
item = grafsOut.join('\n');
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
} else {
// Recursion for sub-lists:
item = showdown.subParser('makehtml.list')(item, options, globals);
item = item.replace(/\n$/, ''); // chomp(item)
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
// Colapse double linebreaks
item = item.replace(/\n\n+/g, '\n\n');
if (isParagraphed) {
item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
} else {
item = showdown.subParser('makehtml.spanGamut')(item, options, globals);
}
}
// now we need to remove the marker (¨A)
item = item.replace('¨A', '');
// we can finally wrap the line in list item tags
item = '<li' + showdown.helper._populateAttributes(attributes) + '>' + item + '</li>\n';
}
let beforeHashEvent = new showdown.Event(eventName + '.onHash', item);
beforeHashEvent
.setOutput(item)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
return beforeHashEvent.output;
});
// attacklab: strip sentinel
listStr = listStr.replace(/¨0/g, '');
globals.gListLevel--;
if (trimTrailing) {
listStr = listStr.replace(/\s+$/, '');
}
return listStr;
}
/**
*
* @param {string} list
* @param {string} listType
* @returns {string|null}
*/
function styleStartNumber (list, listType) {
// check if ol and starts by a number different than 1
if (listType === 'ol') {
let res = list.match(/^ *(\d+)\./);
if (res && res[1] !== '1') {
return res[1];
}
}
return null;
}
/**
* Check and parse consecutive lists (better fix for issue #142)
* @param {RegExp} pattern
* @param {string} list
* @param {string} listType
* @param {boolean} trimTrailing
* @returns {string}
*/
function parseConsecutiveLists (pattern, list, listType, trimTrailing) {
let otp = '';
let captureStartEvent = new showdown.Event('makehtml.list.onCapture', list);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: list,
list: list
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
let attributes = captureStartEvent.attributes;
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
// check if we caught 2 or more consecutive lists by mistake
// we use the counterRgx, meaning if listType is UL we look for OL and vice versa
const olRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?\d+\.[ \t]/gm : /^ {0,3}\d+\.[ \t]/gm;
const ulRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?[*+-][ \t]/gm : /^ {0,3}[*+-][ \t]/gm;
let counterRxg = (listType === 'ul') ? olRgx : ulRgx;
let attrs = showdown.helper.cloneObject(attributes);
if (list.search(counterRxg) !== -1) {
(function parseCL (txt) {
let pos = txt.search(counterRxg);
attrs.start = styleStartNumber(list, listType);
if (pos !== -1) {
// slice
otp += '\n\n<' + listType + showdown.helper._populateAttributes(attrs) + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
// invert counterType and listType
listType = (listType === 'ul') ? 'ol' : 'ul';
counterRxg = (listType === 'ul') ? olRgx : ulRgx;
//recurse
parseCL(txt.slice(pos), attrs);
} else {
otp += '\n\n<' + listType + showdown.helper._populateAttributes(attrs) + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
}
})(list, attributes);
} else {
attrs.start = styleStartNumber(list, listType);
otp = '\n\n<' + listType + showdown.helper._populateAttributes(attrs) + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
}
}
return otp;
}
});

View File

@ -1,261 +0,0 @@
/**
* Form HTML ordered (numbered) and unordered (bulleted) lists.
*/
showdown.subParser('makehtml.lists', function (text, options, globals) {
'use strict';
/**
* Process the contents of a single ordered or unordered list, splitting it
* into individual list items.
* @param {string} listStr
* @param {boolean} trimTrailing
* @returns {string}
*/
function processListItems (listStr, trimTrailing) {
// 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++;
// trim trailing blank lines:
listStr = listStr.replace(/\n{2,}$/, '\n');
// attacklab: add sentinel to emulate \z
listStr += '¨0';
var rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[([xX ])])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0| {0,3}([*+-]|\d+[.])[ \t]+))/gm,
isParagraphed = (/\n[ \t]*\n(?!¨0)/.test(listStr));
// 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
// This will be removed in version 2.0
if (options.disableForced4SpacesIndentedSublists) {
rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[([xX ])])?[ \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() !== '');
var item = showdown.subParser('makehtml.outdent')(m4, options, globals),
bulletStyle = '';
// Support for github tasklists
if (taskbtn && options.tasklists) {
// Style used for tasklist bullets
bulletStyle = ' class="task-list-item';
if (options.moreStyling) {bulletStyle += checked ? ' task-list-item-complete' : '';}
bulletStyle += '" style="list-style-type: none;"';
item = item.replace(/^[ \t]*\[([xX ])?]/m, 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;
});
}
// 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
item = item.replace(/^([-*+]|\d\.)[ \t]+[\S\n ]*/g, function (wm2) {
return '¨A' + wm2;
});
// SPECIAL CASE: a heading followed by a paragraph of text that is not separated by a double newline
// or/nor indented. ex:
//
// - # foo
// bar is great
//
// While this does now follow the spec per se, not allowing for this might cause confusion since
// header blocks don't need double-newlines after
if (/^#+.+\n.+/.test(item)) {
item = item.replace(/^(#+.+)$/m, '$1\n');
}
// m1 - Leading line or
// Has a double return (multi paragraph)
if (m1 || (item.search(/\n{2,}/) > -1)) {
item = showdown.subParser('makehtml.githubCodeBlocks')(item, options, globals);
item = showdown.subParser('makehtml.blockQuotes')(item, options, globals);
item = showdown.subParser('makehtml.headers')(item, options, globals);
item = showdown.subParser('makehtml.lists')(item, options, globals);
item = showdown.subParser('makehtml.codeBlocks')(item, options, globals);
item = showdown.subParser('makehtml.tables')(item, options, globals);
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
//item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
// TODO: This is a copy of the paragraph parser
// This is a provisory fix for issue #494
// For a permanente fix we need to rewrite the paragraph parser, passing the unhashify logic outside
// so that we can call the paragraph parser without accidently unashifying previously parsed blocks
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
var grafs = item.split(/\n{2,}/g),
grafsOut = [],
end = grafs.length; // Wrap <p> tags
for (var i = 0; i < end; i++) {
var str = grafs[i];
// if this is an HTML marker, copy it
if (str.search(/¨([KG])(\d+)\1/g) >= 0) {
grafsOut.push(str);
// test for presence of characters to prevent empty lines being parsed
// as paragraphs (resulting in undesired extra empty paragraphs)
} else if (str.search(/\S/) >= 0) {
str = showdown.subParser('makehtml.spanGamut')(str, options, globals);
str = str.replace(/^([ \t]*)/g, '<p>');
str += '</p>';
grafsOut.push(str);
}
}
item = grafsOut.join('\n');
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
} else {
// Recursion for sub-lists:
item = showdown.subParser('makehtml.lists')(item, options, globals);
item = item.replace(/\n$/, ''); // chomp(item)
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
// Colapse double linebreaks
item = item.replace(/\n\n+/g, '\n\n');
if (isParagraphed) {
item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
} else {
item = showdown.subParser('makehtml.spanGamut')(item, options, globals);
}
}
// now we need to remove the marker (¨A)
item = item.replace('¨A', '');
// we can finally wrap the line in list item tags
item = '<li' + bulletStyle + '>' + item + '</li>\n';
return item;
});
// attacklab: strip sentinel
listStr = listStr.replace(/¨0/g, '');
globals.gListLevel--;
if (trimTrailing) {
listStr = listStr.replace(/\s+$/, '');
}
return listStr;
}
function styleStartNumber (list, listType) {
// check if ol and starts by a number different than 1
if (listType === 'ol') {
var res = list.match(/^ *(\d+)\./);
if (res && res[1] !== '1') {
return ' start="' + res[1] + '"';
}
}
return '';
}
/**
* Check and parse consecutive lists (better fix for issue #142)
* @param {string} list
* @param {string} listType
* @param {boolean} trimTrailing
* @returns {string}
*/
function parseConsecutiveLists (list, listType, trimTrailing) {
// check if we caught 2 or more consecutive lists by mistake
// we use the counterRgx, meaning if listType is UL we look for OL and vice versa
var olRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?\d+\.[ \t]/gm : /^ {0,3}\d+\.[ \t]/gm,
ulRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?[*+-][ \t]/gm : /^ {0,3}[*+-][ \t]/gm,
counterRxg = (listType === 'ul') ? olRgx : ulRgx,
result = '';
if (list.search(counterRxg) !== -1) {
(function parseCL (txt) {
var pos = txt.search(counterRxg),
style = styleStartNumber(list, listType);
if (pos !== -1) {
// slice
result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
// invert counterType and listType
listType = (listType === 'ul') ? 'ol' : 'ul';
counterRxg = (listType === 'ul') ? olRgx : ulRgx;
//recurse
parseCL(txt.slice(pos));
} else {
result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
}
})(list);
} else {
var style = styleStartNumber(list, listType);
result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
}
return result;
}
// Start of list parsing
var subListRgx = /^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
var mainListRgx = /(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
text = globals.converter._dispatch('lists.before', text, options, globals).getText();
// add sentinel to hack around khtml/safari bug:
// http://bugs.webkit.org/show_bug.cgi?id=11231
text += '¨0';
if (globals.gListLevel) {
text = text.replace(subListRgx, function (wholeMatch, list, m2) {
var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, true);
});
} else {
text = text.replace(mainListRgx, function (wholeMatch, m1, list, m3) {
var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, false);
});
}
// strip sentinel
text = text.replace(/¨0/, '');
text = globals.converter._dispatch('makehtml.lists.after', text, options, globals).getText();
return text;
});

View File

@ -8,11 +8,50 @@ showdown.subParser('makehtml.metadata', function (text, options, globals) {
return text;
}
text = globals.converter._dispatch('makehtml.metadata.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.metadata.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
/**
* @param {RegExp} pattern
* @param {string} wholeMatch
* @param {string} format
* @param {string} content
* @returns {string}
*/
function parseMetadataContents (pattern, wholeMatch, format, content) {
let otp;
let captureStartEvent = new showdown.Event('makehtml.metadata.onCapture', content);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
format: format,
content: content
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
format = captureStartEvent.matches.format;
content = captureStartEvent.matches.content;
// if something was passed as output, it will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
otp = '¨M';
}
function parseMetadataContents (content) {
// raw is raw so it's not changed in any way
globals.metadata.raw = content;
globals.metadata.format = format;
// escape chars forbidden in html attributes
// double quotes
@ -20,35 +59,47 @@ showdown.subParser('makehtml.metadata', function (text, options, globals) {
// ampersand first
.replace(/&/g, '&amp;')
// double quotes
.replace(/"/g, '&quot;');
.replace(/"/g, '&quot;')
// Restore dollar signs and tremas
content = content
.replace(/¨D/g, '$$')
.replace(/¨T/g, '¨');
.replace(/¨T/g, '¨')
// replace multiple spaces
.replace(/\n {4}/g, ' ');
content = content.replace(/\n {4}/g, ' ');
content.replace(/^([\S ]+): +([\s\S]+?)$/gm, function (wm, key, value) {
globals.metadata.parsed[key] = value;
return '';
});
let beforeHashEvent = new showdown.Event('makehtml.metadata.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
if (!otp) {
otp = '¨M';
}
return otp;
}
text = text.replace(/^\s*«««+\s*(\S*?)\n([\s\S]+?)\n»»»+\s*\n/, function (wholematch, format, content) {
parseMetadataContents(content);
return '¨M';
// 1. Metadata with «««»»» delimiters
const rgx1 = /^\s*«««+\s*(\S*?)\n([\s\S]+?)\n»»»+\s*\n/;
text = text.replace(rgx1, function (wholeMatch, format, content) {
return parseMetadataContents(rgx1, wholeMatch, format, content);
});
text = text.replace(/^\s*---+\s*(\S*?)\n([\s\S]+?)\n---+\s*\n/, function (wholematch, format, content) {
if (format) {
globals.metadata.format = format;
}
parseMetadataContents(content);
return '¨M';
// 2. Metadata with YAML delimiters
const rgx2 = /^\s*---+\s*(\S*?)\n([\s\S]+?)\n---+\s*\n/;
text = text.replace(rgx2, function (wholeMatch, format, content) {
return parseMetadataContents(rgx1, wholeMatch, format, content);
});
text = text.replace(/¨M/g, '');
text = globals.converter._dispatch('makehtml.metadata.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.metadata.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,17 +0,0 @@
/**
* Remove one level of line-leading tabs or spaces
*/
showdown.subParser('makehtml.outdent', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.outdent.before', text, options, globals).getText();
// attacklab: hack around Konqueror 3.5.4 bug:
// "----------bug".replace(/^-/g,"") == "bug"
text = text.replace(/^(\t|[ ]{1,4})/gm, '¨0'); // attacklab: g_tab_width
// attacklab: clean up hack
text = text.replace(/¨0/g, '');
text = globals.converter._dispatch('makehtml.outdent.after', text, options, globals).getText();
return text;
});

View File

@ -4,7 +4,6 @@
showdown.subParser('makehtml.paragraphs', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.paragraphs.before', text, options, globals).getText();
// Strip leading and trailing lines:
text = text.replace(/^\n+/g, '');
text = text.replace(/\n+$/g, '');
@ -37,7 +36,7 @@ showdown.subParser('makehtml.paragraphs', function (text, options, globals) {
codeFlag = false;
// if this is a marker for an html block...
// use RegExp.test instead of string.search because of QML bug
while (/¨(K|G)(\d+)\1/.test(grafsOutIt)) {
while (/¨([KG])(\d+)\1/.test(grafsOutIt)) {
var delim = RegExp.$1,
num = RegExp.$2;
@ -54,7 +53,7 @@ showdown.subParser('makehtml.paragraphs', function (text, options, globals) {
}
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
grafsOutIt = grafsOutIt.replace(/(\n\n)?¨(K|G)\d+\2(\n\n)?/, blockText);
grafsOutIt = grafsOutIt.replace(/(\n\n)?¨([KG])\d+\2(\n\n)?/, blockText);
// Check if grafsOutIt is a pre->code
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
codeFlag = true;
@ -66,5 +65,5 @@ showdown.subParser('makehtml.paragraphs', function (text, options, globals) {
// Strip leading and trailing lines:
text = text.replace(/^\n+/g, '');
text = text.replace(/\n+$/g, '');
return globals.converter._dispatch('makehtml.paragraphs.after', text, options, globals).getText();
return text;
});

View File

@ -9,7 +9,7 @@ showdown.subParser('makehtml.runExtension', function (ext, text, options, global
} else if (ext.regex) {
// TODO remove this when old extension loading mechanism is deprecated
var re = ext.regex;
let re = ext.regex;
if (!(re instanceof RegExp)) {
re = new RegExp(re, 'g');
}

View File

@ -5,25 +5,26 @@
showdown.subParser('makehtml.spanGamut', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.span.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.spanGamut.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
text = showdown.subParser('makehtml.codeSpans')(text, options, globals);
text = showdown.subParser('makehtml.codeSpan')(text, options, globals);
text = showdown.subParser('makehtml.escapeSpecialCharsWithinTagAttributes')(text, options, globals);
text = showdown.subParser('makehtml.encodeBackslashEscapes')(text, options, globals);
// Process link and image tags. Images must come first,
// because ![foo][f] looks like a link.
text = showdown.subParser('makehtml.images')(text, options, globals);
text = showdown.subParser('makehtml.image')(text, options, globals);
text = showdown.subParser('makehtml.link')(text, options, globals);
text = globals.converter._dispatch('smakehtml.links.before', text, options, globals).getText();
text = showdown.subParser('makehtml.links')(text, options, globals);
text = globals.converter._dispatch('smakehtml.links.after', text, options, globals).getText();
//text = showdown.subParser('makehtml.autoLinks')(text, options, globals);
//text = showdown.subParser('makehtml.simplifiedAutoLinks')(text, options, globals);
text = showdown.subParser('makehtml.emoji')(text, options, globals);
text = showdown.subParser('makehtml.underline')(text, options, globals);
text = showdown.subParser('makehtml.italicsAndBold')(text, options, globals);
text = showdown.subParser('makehtml.emphasisAndStrong')(text, options, globals);
text = showdown.subParser('makehtml.strikethrough')(text, options, globals);
text = showdown.subParser('makehtml.ellipsis')(text, options, globals);
@ -45,6 +46,11 @@ showdown.subParser('makehtml.spanGamut', function (text, options, globals) {
text = text.replace(/ +\n/g, '<br />\n');
}
text = globals.converter._dispatch('makehtml.spanGamut.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.spanGamut.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -1,11 +1,55 @@
showdown.subParser('makehtml.strikethrough', function (text, options, globals) {
'use strict';
if (options.strikethrough) {
text = globals.converter._dispatch('makehtml.strikethrough.before', text, options, globals).getText();
text = text.replace(/(?:~){2}([\s\S]+?)(?:~){2}/g, function (wm, txt) { return '<del>' + txt + '</del>'; });
text = globals.converter._dispatch('makehtml.strikethrough.after', text, options, globals).getText();
if (!options.strikethrough) {
return text;
}
return text;
let startEvent = new showdown.Event('makehtml.strikethrough.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
const strikethroughRegex = /~{2}([\s\S]+?)~{2}/g;
text = text.replace(strikethroughRegex, function (wholeMatch, txt) {
let otp;
let captureStartEvent = new showdown.Event('makehtml.strikethrough.onCapture', txt);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(strikethroughRegex)
.setMatches({
_wholeMatch: wholeMatch,
strikethrough: txt
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
otp = '<del' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' + txt + '</del>';
}
let beforeHashEvent = new showdown.Event('makehtml.strikethrough.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
return beforeHashEvent.output;
});
let afterEvent = new showdown.Event('makehtml.strikethrough.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -6,13 +6,13 @@
showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) {
'use strict';
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,
base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm;
const 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,
base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z\d+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm;
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '¨0';
var replaceFunc = function (wholeMatch, linkId, url, width, height, blankLines, title) {
let replaceFunc = function (wholeMatch, linkId, url, width, height, blankLines, title) {
// if there aren't two instances of linkId it must not be a reference link so back out
linkId = linkId.toLowerCase();
@ -35,7 +35,7 @@ showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, glo
} else {
if (title) {
globals.gTitles[linkId] = title.replace(/"|'/g, '&quot;');
globals.gTitles[linkId] = title.replace(/["']/g, '&quot;');
}
if (options.parseImgDimensions && width && height) {
globals.gDimensions[linkId] = {

View File

@ -0,0 +1,326 @@
showdown.subParser('makehtml.table', function (text, options, globals) {
'use strict';
if (!options.tables) {
return text;
}
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
//
// parser starts here
//
let startEvent = new showdown.Event('makehtml.table.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
// parse multi column tables
const tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*[-=]{2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*[-=]{2,}[\s\S]+?(?:\n\n|¨0)/gm;
text = text.replace(tableRgx, function (wholeMatch) {
return parse(tableRgx, wholeMatch);
});
const singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*[-=]{2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;
text = text.replace(singeColTblRgx, function (wholeMatch) {
return parse(singeColTblRgx, wholeMatch);
});
let afterEvent = new showdown.Event('makehtml.table.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
/**
*
* @param {RegExp} pattern
* @param {string} wholeMatch
* @returns {string}
*/
function parse (pattern, wholeMatch) {
let tab = preParse(wholeMatch);
// if parseTable returns null then it's a malformed table so we return what we caught
if (!tab) {
return wholeMatch;
}
// only now we consider it to be a markdown table and start doing stuff
let headers = tab.rawHeaders;
let styles = tab.rawStyles;
let cells = tab.rawCells;
let otp;
let captureStartEvent = new showdown.Event('makehtml.table.onCapture', wholeMatch);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
table: wholeMatch
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
if (captureStartEvent.output && captureStartEvent.output !== '') {
// user provided an otp, so we use it
otp = captureStartEvent.output;
} else {
// user changed matches.table, so we need to generate headers, styles, and cells again
if (captureStartEvent.matches.table !== wholeMatch) {
tab = preParse(captureStartEvent.matches.table);
// user passed a malformed table, so we bail
if (!tab) {
return wholeMatch;
}
headers = tab.rawHeaders;
styles = tab.rawStyles;
cells = tab.rawCells;
}
let parsedTab = parseTable (headers, styles, cells);
let attributes = captureStartEvent.attributes;
otp = buildTableOtp(parsedTab.headers, parsedTab.cells, attributes);
}
let beforeHashEvent = new showdown.Event('makehtml.table.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
otp = beforeHashEvent.output;
return otp;
}
/**
*
* @param {string[]} headers
* @param {string[]} cells
* @param {{}} attributes
* @returns {string}
*/
function buildTableOtp (headers, cells, attributes) {
let otp;
const colCount = headers.length,
rowCount = cells.length;
otp = '<table' + showdown.helper._populateAttributes(attributes) + '>\n<thead>\n<tr>\n';
for (let i = 0; i < colCount; ++i) {
otp += headers[i];
}
otp += '</tr>\n</thead>\n<tbody>\n';
for (let i = 0; i < rowCount; ++i) {
otp += '<tr>\n';
for (let ii = 0; ii < colCount; ++ii) {
otp += cells[i][ii];
}
otp += '</tr>\n';
}
otp += '</tbody>\n</table>\n';
return otp;
}
/**
* @param {string} rawTable
* @returns {{rawHeaders: string[], rawStyles: string[], rawCells: *[]}|null}
*/
function preParse (rawTable) {
let tableLines = rawTable.split('\n');
for (let i = 0; i < tableLines.length; ++i) {
// strip wrong first and last column if wrapped tables are used
if (/^ {0,3}\|/.test(tableLines[i])) {
tableLines[i] = tableLines[i].replace(/^ {0,3}\|/, '');
}
if (/\|[ \t]*$/.test(tableLines[i])) {
tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, '');
}
// parse code spans first, but we only support one line code spans
tableLines[i] = showdown.subParser('makehtml.codeSpan')(tableLines[i], options, globals);
}
let rawHeaders = tableLines[0].split('|').map(function (s) { return s.trim();}),
rawStyles = tableLines[1].split('|').map(function (s) { return s.trim();}),
rawCells = [];
tableLines.shift();
tableLines.shift();
for (let i = 0; i < tableLines.length; ++i) {
if (tableLines[i].trim() === '') {
continue;
}
rawCells.push(
tableLines[i]
.split('|')
.map(function (s) {
return s.trim();
})
);
}
if (rawHeaders.length < rawStyles.length) {
return null;
}
return {
rawHeaders: rawHeaders,
rawStyles: rawStyles,
rawCells: rawCells
};
}
/**
*
* @param {string[]} rawHeaders
* @param {string[]} rawStyles
* @param {string[]} rawCells
* @returns {{headers: *, cells: *}}
*/
function parseTable (rawHeaders, rawStyles, rawCells) {
const headers = [],
cells = [],
styles = [],
colCount = rawHeaders.length,
rowCount = rawCells.length;
for (let i = 0; i < colCount; ++i) {
styles.push(parseStyle(rawStyles[i]));
}
for (let i = 0; i < colCount; ++i) {
let header = rawHeaders[i];
let captureStartEvent = new showdown.Event('makehtml.table.header.onCapture', rawHeaders[i]);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(null)
.setMatches({
_wholeMatch: rawHeaders[i],
header: rawHeaders[i]
})
.setAttributes(styles[i]);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
if (captureStartEvent.output && captureStartEvent.output !== '') {
// user provided an otp, so we use it
header = captureStartEvent.output;
} else {
header = parseHeader(captureStartEvent.matches.header, styles[i]);
}
let beforeHashEvent = new showdown.Event('makehtml.table.header.onHash', header);
beforeHashEvent
.setOutput(header)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
header = beforeHashEvent.output;
headers.push(header);
}
for (let i = 0; i < rowCount; ++i) {
let row = [];
for (let ii = 0; ii < colCount; ++ii) {
let cell = (!showdown.helper.isUndefined(rawCells[i][ii])) ? rawCells[i][ii] : '',
attributes = (!showdown.helper.isUndefined(styles[ii])) ? styles[ii] : {};
// since we're reusing attributes, we might need to remove the id that we previously set for headers
if (attributes.id) {
attributes.classes = [attributes.id] + '_col';
delete attributes.id;
}
let captureStartEvent = new showdown.Event('makehtml.table.cell.onCapture', cell);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(null)
.setMatches({
_wholeMatch: cell,
cell: cell
})
.setAttributes(attributes);
captureStartEvent = globals.converter.dispatch(captureStartEvent);
if (captureStartEvent.output && captureStartEvent.output !== '') {
// user provided an otp, so we use it
cell = captureStartEvent.output;
} else {
attributes = captureStartEvent.attributes;
cell = parseCell(captureStartEvent.matches.cell, attributes);
}
let beforeHashEvent = new showdown.Event('makehtml.table.cell.onHash', cell);
beforeHashEvent
.setOutput(cell)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
cell = beforeHashEvent.output;
row.push(cell);
}
cells.push(row);
}
return {headers: headers, cells: cells};
}
/**
* @param {string} sLine
* @returns {{}|{style: string}}
*/
function parseStyle (sLine) {
if (/^:[ \t]*-+$/.test(sLine)) {
return { style: 'text-align:left;' };
} else if (/^-+[ \t]*:[ \t]*$/.test(sLine)) {
return { style: 'text-align:right;' };
} else if (/^:[ \t]*-+[ \t]*:$/.test(sLine)) {
return { style: 'text-align:center;' };
} else {
return {};
}
}
/**
*
* @param {string} headerText
* @param {{id: string}} attributes
* @returns {string}
*/
function parseHeader (headerText, attributes) {
headerText = headerText.trim();
headerText = showdown.subParser('makehtml.spanGamut')(headerText, options, globals);
// support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
// TODO think about this option!!! and remove backwards compatibility
if (options.tablesHeaderId || options.tableHeaderId) {
attributes.id = headerText.replace(/ /g, '_').toLowerCase();
}
return '<th' + showdown.helper._populateAttributes(attributes) + '>' + headerText + '</th>\n';
}
/**
*
* @param {string} cellText
* @param {{}} attributes
* @returns {string}
*/
function parseCell (cellText, attributes) {
cellText = showdown.subParser('makehtml.spanGamut')(cellText, options, globals);
return '<td' + showdown.helper._populateAttributes(attributes) + '>' + cellText + '</td>\n';
}
});

View File

@ -1,143 +0,0 @@
showdown.subParser('makehtml.tables', function (text, options, globals) {
'use strict';
if (!options.tables) {
return text;
}
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*[-=]{2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*[-=]{2,}[\s\S]+?(?:\n\n|¨0)/gm,
//singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*[-=]{2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;
function parseStyles (sLine) {
if (/^:[ \t]*--*$/.test(sLine)) {
return ' style="text-align:left;"';
} else if (/^--*[ \t]*:[ \t]*$/.test(sLine)) {
return ' style="text-align:right;"';
} else if (/^:[ \t]*--*[ \t]*:$/.test(sLine)) {
return ' style="text-align:center;"';
} else {
return '';
}
}
function parseHeaders (header, style) {
var id = '';
header = header.trim();
// support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
if (options.tablesHeaderId || options.tableHeaderId) {
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
}
header = showdown.subParser('makehtml.spanGamut')(header, options, globals);
return '<th' + id + style + '>' + header + '</th>\n';
}
function parseCells (cell, style) {
var subText = showdown.subParser('makehtml.spanGamut')(cell, options, globals);
return '<td' + style + '>' + subText + '</td>\n';
}
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) {
tb += '<tr>\n';
for (var ii = 0; ii < tblLgn; ++ii) {
tb += cells[i][ii];
}
tb += '</tr>\n';
}
tb += '</tbody>\n</table>\n';
return tb;
}
function parseTable (rawTable) {
var i, tableLines = rawTable.split('\n');
for (i = 0; i < tableLines.length; ++i) {
// strip wrong first and last column if wrapped tables are used
if (/^ {0,3}\|/.test(tableLines[i])) {
tableLines[i] = tableLines[i].replace(/^ {0,3}\|/, '');
}
if (/\|[ \t]*$/.test(tableLines[i])) {
tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, '');
}
// parse code spans first, but we only support one line code spans
tableLines[i] = showdown.subParser('makehtml.codeSpans')(tableLines[i], options, globals);
}
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 = [];
tableLines.shift();
tableLines.shift();
for (i = 0; i < tableLines.length; ++i) {
if (tableLines[i].trim() === '') {
continue;
}
rawCells.push(
tableLines[i]
.split('|')
.map(function (s) {
return s.trim();
})
);
}
if (rawHeaders.length < rawStyles.length) {
return rawTable;
}
for (i = 0; i < rawStyles.length; ++i) {
styles.push(parseStyles(rawStyles[i]));
}
for (i = 0; i < rawHeaders.length; ++i) {
if (showdown.helper.isUndefined(styles[i])) {
styles[i] = '';
}
headers.push(parseHeaders(rawHeaders[i], styles[i]));
}
for (i = 0; i < rawCells.length; ++i) {
var row = [];
for (var ii = 0; ii < headers.length; ++ii) {
if (showdown.helper.isUndefined(rawCells[i][ii])) {
}
row.push(parseCells(rawCells[i][ii], styles[ii]));
}
cells.push(row);
}
return buildTable(headers, cells);
}
text = globals.converter._dispatch('makehtml.tables.before', text, options, globals).getText();
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// parse multi column tables
text = text.replace(tableRgx, parseTable);
// parse one column tables
text = text.replace(singeColTblRgx, parseTable);
text = globals.converter._dispatch('makehtml.tables.after', text, options, globals).getText();
return text;
});

View File

@ -5,28 +5,85 @@ showdown.subParser('makehtml.underline', function (text, options, globals) {
return text;
}
text = globals.converter._dispatch('makehtml.underline.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.underline.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
if (options.literalMidWordUnderscores) {
text = text.replace(/\b___(\S[\s\S]*?)___\b/g, function (wm, txt) {
return '<u>' + txt + '</u>';
const rgx1 = /\b___(\S[\s\S]*?)___\b/g;
text = text.replace(rgx1, function (wm, txt) {
return parse(rgx1, wm, txt);
});
text = text.replace(/\b__(\S[\s\S]*?)__\b/g, function (wm, txt) {
return '<u>' + txt + '</u>';
const rgx2 = /\b__(\S[\s\S]*?)__\b/g;
text = text.replace(rgx2, function (wm, txt) {
return parse(rgx2, wm, txt);
});
} else {
text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
return (/\S$/.test(m)) ? '<u>' + m + '</u>' : wm;
const rgx3 = /___(\S[\s\S]*?)___/g;
text = text.replace(rgx3, function (wm, txt) {
if (!(/\S$/.test(txt))) {
return wm;
}
return parse(rgx3, wm, txt);
});
text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
return (/\S$/.test(m)) ? '<u>' + m + '</u>' : wm;
const rgx4 = /__(\S[\s\S]*?)__/g;
text = text.replace(rgx4, function (wm, txt) {
if (!(/\S$/.test(txt))) {
return wm;
}
return parse(rgx4, wm, txt);
});
}
// escape remaining underscores to prevent them being parsed by italic and bold
text = text.replace(/(_)/g, showdown.helper.escapeCharactersCallback);
text = globals.converter._dispatch('makehtml.underline.after', text, options, globals).getText();
let afterEvent = new showdown.Event('makehtml.underline.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
function parse (pattern, wholeMatch, txt) {
let otp;
let captureStartEvent = new showdown.Event('makehtml.underline.onCapture', txt);
captureStartEvent
.setOutput(null)
._setGlobals(globals)
._setOptions(options)
.setRegexp(pattern)
.setMatches({
_wholeMatch: wholeMatch,
strikethrough: txt
})
.setAttributes({});
captureStartEvent = globals.converter.dispatch(captureStartEvent);
// if something was passed as output, it takes precedence
// and will be used as output
if (captureStartEvent.output && captureStartEvent.output !== '') {
otp = captureStartEvent.output;
} else {
otp = '<u' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' + txt + '</u>';
}
let beforeHashEvent = new showdown.Event('makehtml.underline.onHash', otp);
beforeHashEvent
.setOutput(otp)
._setGlobals(globals)
._setOptions(options);
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
return beforeHashEvent.output;
}
return text;
});

View File

@ -3,13 +3,24 @@
*/
showdown.subParser('makehtml.unescapeSpecialChars', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('makehtml.unescapeSpecialChars.before', text, options, globals).getText();
let startEvent = new showdown.Event('makehtml.unescapeSpecialChars.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
text = text.replace(/¨E(\d+)E/g, function (wholeMatch, m1) {
var charCodeToReplace = parseInt(m1);
let charCodeToReplace = parseInt(m1);
return String.fromCharCode(charCodeToReplace);
});
text = globals.converter._dispatch('makehtml.unescapeSpecialChars.after', text, options, globals).getText();
return text;
let afterEvent = new showdown.Event('makehtml.unescapeSpecialChars.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

View File

@ -0,0 +1,46 @@
////
// makehtml/unhashHTMLSpans.js
// Copyright (c) 2018 ShowdownJS
//
// Unhash HTML spans
//
// ***Author:***
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
////
showdown.subParser('makehtml.unhashHTMLSpans', function (text, options, globals) {
'use strict';
let startEvent = new showdown.Event('makehtml.unhashHTMLSpans.onStart', text);
startEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
startEvent = globals.converter.dispatch(startEvent);
text = startEvent.output;
for (let i = 0; i < globals.gHtmlSpans.length; ++i) {
let repText = globals.gHtmlSpans[i],
// limiter to prevent infinite loop (assume 20 as limit for recurse)
limit = 0;
while (/¨C(\d+)C/.test(repText)) {
let num = repText.match(/¨C(\d+)C/)[1];
repText = repText.replace('¨C' + num + 'C', globals.gHtmlSpans[num]);
if (limit === 10) {
console.error('maximum nesting of 20 spans reached!!!');
break;
}
++limit;
}
text = text.replace('¨C' + i + 'C', repText);
}
let afterEvent = new showdown.Event('makehtml.unhashHTMLSpans.onEnd', text);
afterEvent
.setOutput(text)
._setGlobals(globals)
._setOptions(options);
afterEvent = globals.converter.dispatch(afterEvent);
return afterEvent.output;
});

18
test/bootstrap.js vendored
View File

@ -1,6 +1,22 @@
//.webstorm.bootstrap.js
var chai = require('chai');
const chai = require('chai');
const fs = require('fs');
global.chai = chai;
global.expect = chai.expect;
global.showdown = require('../.build/showdown.js');
global.getDefaultOpts = require('./optionswp.js').getDefaultOpts;
// mock XMLHttpRequest for browser and node test
function XMLHttpRequest () {
this.responseText = null;
this.status = null;
this.open = function (mode, file) {
//mode is ignored, it's always sync
this.responseText = fs.readFileSync(file);
this.status = 200;
return this;
};
}
global.XMLHttpRequest = XMLHttpRequest;

View File

@ -0,0 +1 @@
<h1 id="someid">some header</h1>

View File

@ -0,0 +1 @@
# some header {some-id}

View File

@ -1,2 +1,2 @@
<p>this is showdown's emoji <img width="20" height="20" align="absmiddle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAS1BMVEX///8jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS3b1q3b1q3b1q3b1q3b1q3b1q3b1q3b1q0565CIAAAAGXRSTlMAQHCAYCCw/+DQwPCQUBAwoHCAEP+wwFBgS2fvBgAAAUZJREFUeAHs1cGy7BAUheFFsEDw/k97VTq3T6ge2EmdM+pvrP6Iwd74XV9Kb52xuMU4/uc1YNgZLFOeV8FGdhGrNk5SEgUyPxAEdj4LlMRDyhVAMVEa2M7TBSeVZAFPdqHgzSZJwPKgcLFLAooHDJo4EDCw4gAtBoJA5UFj4Ng5LOGLwVXZuoIlji/jeQHFk7+baHxrCjeUwB9+s88KndvlhcyBN5BSkYNQIVVb4pV+Npm7hhuKDs/uMP5KxT3WzSNNLIuuoDpMmuAVMruMSeDyQBi24DTr43LAY7ILA1QYaWkgfHzFthYYzg67SQsCbB8GhJUEGCtO9n0rSaCLxgJQjS/JSgMTg2eBDEHAJ+H350AsjYNYscrErgI2e/l+mdR967TCX/v6N0EhPECYCP0i+IAoYQOE8BogNhQMEMdrgAQWHaMAAGi5I5euoY9NAAAAAElFTkSuQmCC"></p>
<p>and this is github's emoji <img width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/octocat.png?v8"></p>
<p>this is showdown's emoji <img alt="showdown" width="20" height="20" align="absmiddle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAS1BMVEX///8jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS0jJS3b1q3b1q3b1q3b1q3b1q3b1q3b1q3b1q0565CIAAAAGXRSTlMAQHCAYCCw/+DQwPCQUBAwoHCAEP+wwFBgS2fvBgAAAUZJREFUeAHs1cGy7BAUheFFsEDw/k97VTq3T6ge2EmdM+pvrP6Iwd74XV9Kb52xuMU4/uc1YNgZLFOeV8FGdhGrNk5SEgUyPxAEdj4LlMRDyhVAMVEa2M7TBSeVZAFPdqHgzSZJwPKgcLFLAooHDJo4EDCw4gAtBoJA5UFj4Ng5LOGLwVXZuoIlji/jeQHFk7+baHxrCjeUwB9+s88KndvlhcyBN5BSkYNQIVVb4pV+Npm7hhuKDs/uMP5KxT3WzSNNLIuuoDpMmuAVMruMSeDyQBi24DTr43LAY7ILA1QYaWkgfHzFthYYzg67SQsCbB8GhJUEGCtO9n0rSaCLxgJQjS/JSgMTg2eBDEHAJ+H350AsjYNYscrErgI2e/l+mdR967TCX/v6N0EhPECYCP0i+IAoYQOE8BogNhQMEMdrgAQWHaMAAGi5I5euoY9NAAAAAElFTkSuQmCC"></p>
<p>and this is github's emoji <img alt="octocat" width="20" height="20" align="absmiddle" src="https://github.githubassets.com/images/icons/emoji/octocat.png?v8"></p>

View File

@ -7,12 +7,12 @@
</thead>
<tbody>
<tr>
<td>Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td class="first_header_col">Row 1 Cell 1</td>
<td class="second_header_col">Row 1 Cell 2</td>
</tr>
<tr>
<td>Row 2 Cell 1</td>
<td>Row 2 Cell 2</td>
<td class="first_header_col">Row 2 Cell 1</td>
<td class="second_header_col">Row 2 Cell 2</td>
</tr>
</tbody>
</table>

View File

@ -1,6 +1,7 @@
<p><img src="/path/to/img.jpg" alt="Alt text" /></p>
<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>
<p><img src="url/to/image.jpg" alt="Alt text" title="Optional title attribute" /></p>
<p><img src="url/to/image2.jpg" alt="My Image" title="Optional title attribute" /></p>
<p><img src="url/to/image2.jpg" alt="some alt text" /></p>
<p><img src="url/to/image3.jpg" alt="My Image" title="Optional title attribute" /></p>
<p>![leave me alone]</p>
<p>![leave me alone][]</p>

View File

@ -4,6 +4,8 @@
![Alt text][id]
![some alt text][1]
![My Image]
![leave me alone]
@ -11,4 +13,5 @@
![leave me alone][]
[id]: url/to/image.jpg "Optional title attribute"
[My Image]: url/to/image2.jpg "Optional title attribute"
[1]: url/to/image2.jpg
[My Image]: url/to/image3.jpg "Optional title attribute"

View File

@ -1,7 +1,7 @@
/**
* Created by Estevao on 08-06-2015.
*/
var bootstrap = require('./makehtml.bootstrap.js'),
const bootstrap = require('./makehtml.bootstrap.js'),
showdown = bootstrap.showdown,
assertion = bootstrap.assertion,
testsuite = bootstrap.getTestSuite('test/functional/makehtml/cases/features/'),
@ -20,6 +20,7 @@ var bootstrap = require('./makehtml.bootstrap.js'),
metadataSuite = bootstrap.getTestSuite('test/functional/makehtml/cases/features/metadata/'),
splitAdjacentBlockquotesSuite = bootstrap.getTestSuite('test/functional/makehtml/cases/features/splitAdjacentBlockquotes/'),
moreStyling = bootstrap.getTestSuite('test/functional/makehtml/cases/features/moreStyling/'),
customizedHeaderId = bootstrap.getTestSuite('test/functional/makehtml/cases/features/customizedHeaderId/'),
http = require('http'),
https = require('https'),
expect = require('chai').expect;
@ -28,8 +29,8 @@ describe('makeHtml() features testsuite', function () {
'use strict';
describe('issues', function () {
for (var i = 0; i < testsuite.length; ++i) {
var converter;
for (let i = 0; i < testsuite.length; ++i) {
let converter;
if (testsuite[i].name === '#143.support-image-dimensions') {
converter = new showdown.Converter({parseImgDimensions: true});
} else if (testsuite[i].name === '#69.header-level-start') {
@ -113,9 +114,9 @@ describe('makeHtml() features testsuite', function () {
/** test Table Syntax Support **/
describe('table support', function () {
var converter,
let converter,
suite = tableSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'basic-with-header-ids') {
converter = new showdown.Converter({tables: true, tablesHeaderId: true});
} else if (suite[i].name === '#179.parse-md-in-table-ths') {
@ -129,9 +130,9 @@ describe('makeHtml() features testsuite', function () {
/** test simplifiedAutoLink Support **/
describe('simplifiedAutoLink support in', function () {
var converter,
let converter,
suite = simplifiedAutoLinkSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'emphasis-and-strikethrough') {
converter = new showdown.Converter({simplifiedAutoLink: true, strikethrough: true});
} else if (suite[i].name === 'disallow-underscores') {
@ -147,9 +148,9 @@ describe('makeHtml() features testsuite', function () {
/** test openLinksInNewWindow support **/
describe('openLinksInNewWindow support in', function () {
var converter,
let converter,
suite = openLinksInNewWindowSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'simplifiedAutoLink') {
converter = new showdown.Converter({openLinksInNewWindow: true, simplifiedAutoLink: true});
} else {
@ -161,9 +162,9 @@ describe('makeHtml() features testsuite', function () {
/** test disableForced4SpacesIndentedSublists support **/
describe('disableForced4SpacesIndentedSublists support in', function () {
var converter,
let converter,
suite = disableForced4SpacesIndentedSublistsSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -171,9 +172,9 @@ describe('makeHtml() features testsuite', function () {
/** test rawHeaderId support **/
describe('rawHeaderId support', function () {
var converter,
let converter,
suite = rawHeaderIdSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'with-prefixHeaderId') {
converter = new showdown.Converter({rawHeaderId: true, prefixHeaderId: '/prefix/'});
} else {
@ -185,9 +186,9 @@ describe('makeHtml() features testsuite', function () {
/** test rawPrefixHeaderId support **/
describe('rawPrefixHeaderId support', function () {
var converter,
let converter,
suite = rawPrefixHeaderIdSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({rawPrefixHeaderId: true, prefixHeaderId: '/prefix/'});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -195,7 +196,7 @@ describe('makeHtml() features testsuite', function () {
/** test emojis support **/
describe('emojis support', function () {
var converter,
let converter,
suite = emojisSuite,
imgSrcRegexp = /<img[^>]+src=("https?:\/\/[^"]+"|'https?:\/\/[^']+')/g;
@ -221,7 +222,7 @@ describe('makeHtml() features testsuite', function () {
};
}
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'simplifiedautolinks') {
converter = new showdown.Converter({emoji: true, simplifiedAutoLink: true});
} else {
@ -230,7 +231,7 @@ describe('makeHtml() features testsuite', function () {
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
var imgUrl = imgSrcRegexp.exec(suite[i].expected);
let imgUrl = imgSrcRegexp.exec(suite[i].expected);
if (imgUrl) {
it('should use a working emoji URL', testImageUrlExists(imgUrl[1]));
}
@ -239,9 +240,9 @@ describe('makeHtml() features testsuite', function () {
/** test underline support **/
describe('underline support', function () {
var converter,
let converter,
suite = underlineSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'simplifiedautolinks') {
converter = new showdown.Converter({underline: true, simplifiedAutoLink: true});
} else {
@ -253,9 +254,9 @@ describe('makeHtml() features testsuite', function () {
/** test ellipsis option **/
describe('ellipsis option', function () {
var converter,
let converter,
suite = ellipsisSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({ellipsis: false});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -263,9 +264,9 @@ describe('makeHtml() features testsuite', function () {
/** test literalMidWordUnderscores option **/
describe('literalMidWordUnderscores option', function () {
var converter,
let converter,
suite = literalMidWordUnderscoresSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({literalMidWordUnderscores: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -274,9 +275,9 @@ describe('makeHtml() features testsuite', function () {
/** test literalMidWordAsterisks option **/
/*
describe('literalMidWordAsterisks option', function () {
var converter,
let converter,
suite = literalMidWordAsterisksSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({literalMidWordAsterisks: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -285,9 +286,9 @@ describe('makeHtml() features testsuite', function () {
/** test completeHTMLDocument option **/
describe('completeHTMLDocument option', function () {
var converter,
let converter,
suite = completeHTMLOutputSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({completeHTMLDocument: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
@ -296,10 +297,10 @@ describe('makeHtml() features testsuite', function () {
/** test metadata option **/
describe('metadata option', function () {
var converter,
let converter,
suite = metadataSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
if (suite[i].name === 'embeded-in-output' ||
suite[i].name === 'embeded-two-consecutive-metadata-blocks' ||
suite[i].name === 'embeded-two-consecutive-metadata-blocks-different-symbols') {
@ -315,10 +316,10 @@ describe('makeHtml() features testsuite', function () {
/** test metadata option **/
describe('splitAdjacentBlockquotes option', function () {
var converter,
let converter,
suite = splitAdjacentBlockquotesSuite;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({splitAdjacentBlockquotes: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
@ -326,13 +327,23 @@ describe('makeHtml() features testsuite', function () {
/** test moreStyling option **/
describe('moreStyling option', function () {
var converter,
let converter,
suite = moreStyling;
for (var i = 0; i < suite.length; ++i) {
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({moreStyling: true, tasklists: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
describe('customizedHeaderId option', function () {
let converter,
suite = customizedHeaderId;
for (let i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({customizedHeaderId: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
});

View File

@ -0,0 +1,82 @@
# ATX h1
## ATX h2
### ATX h3
#### ATX h4
##### ATX h5
###### ATX h6
setext h1
----------
setext h2
==========
some paragraph
*italic*
**bold**
***bold and italic***
__underline__
___superunderline___
~~strikethrough~~
[link](foo.com)
|[img](foo.com/img.jpg)
some `code line`
---
some http://autolink.com autolink
> blockquote
other paragraph
code
block
```javascript
gh codeblock
```
@mentions
- ul 1
- ul 2
- ul 3
other paragraph
1. ol 1
2. ol 2
3. ol 3
foo
- [ ] task item 1
- [X] task item 2
| foo | bar |
|-----|-----|
| 1 | 2 |
paragraph
| foo | bar |
|-----|-----|
<div markdown="1">a div</div>

View File

@ -1,5 +1,5 @@
/* jshint ignore:start */
var fs = require('fs'),
let fs = require('fs'),
filedata;
filedata = fs.readFileSync('src/options.js', 'utf8');
eval(filedata);

View File

@ -1,7 +1,6 @@
/**
* Created by Estevao on 31-05-2015.
*/
//let showdown = require('../../.build/showdown.js') || require('showdown');
chai.should();
@ -159,8 +158,8 @@ describe('showdown.Converter', function () {
it('should listen to ' + name, function () {
let converter = new showdown.Converter();
converter.listen(name, function (event) {
let evtName = event.getName();
let text = event.getCapturedText();
let evtName = event.name;
let text = event.input;
evtName.should.equal(name.toLowerCase());
text.should.match(/^[\s\S]*foo[\s\S]*$/);
return text;

View File

@ -0,0 +1,289 @@
/**
* Created by Tivie on 04/03/2022.
*/
// in node, if bootstrap is pre-loaded, there's a mock of XMLHttpRequest which
// internally just calls fs.readFileSync
describe('showdown.Event', function () {
'use strict';
//const subparserList = showdown.getSubParserList();
const testSpec = {
makehtml: {
doesNotExist: [
{ event: 'onStart', text: 'foo', result: false },
{ event: 'onEnd', text: 'foo', result: false },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: 'foo', result: false }
],
blockquote: [
{ event: 'onStart', text: '> foo', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '> foo', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '> foo', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '> foo', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
codeBlock: [
{ event: 'onStart', text: ' foo\n bar', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: ' foo\n bar', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: ' foo\n bar', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: ' foo\n bar', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
codeSpan: [
{ event: 'onStart', text: '`foo`', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '`foo`', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '`foo`', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '`foo`', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
emoji: [
{ event: 'onStart', text: ':smile:', result: true },
{ event: 'onStart', text: 'smile', result: true },
{ event: 'onEnd', text: ':smile:', result: true },
{ event: 'onEnd', text: 'smile', result: true },
{ event: 'onCapture', text: ':smile:', result: true },
{ event: 'onCapture', text: ':blablablablabla:', result: false }, // this emoji does not exist
{ event: 'onCapture', text: 'smile', result: false },
{ event: 'onHash', text: ':smile:', result: true },
{ event: 'onHash', text: ':blablablablabla:', result: false }, // this emoji does not exist
{ event: 'onHash', text: 'smile', result: false }
],
emphasisAndStrong: [
{ event: 'onStart', text: '*foo*', result: true },
{ event: 'onStart', text: '**foo**', result: true },
{ event: 'onStart', text: '***foo***', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '*foo*', result: true },
{ event: 'onEnd', text: '**foo**', result: true },
{ event: 'onEnd', text: '***foo***', result: true },
{ event: 'onEnd', text: 'foo', result: true }
],
'emphasisAndStrong.emphasis': [
{ event: 'onCapture', text: '*foo*', result: true },
{ event: 'onCapture', text: '**foo**', result: false },
{ event: 'onCapture', text: '***foo***', result: false },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '*foo*', result: true },
{ event: 'onHash', text: '**foo**', result: false },
{ event: 'onHash', text: '***foo***', result: false },
{ event: 'onHash', text: 'foo', result: false }
],
'emphasisAndStrong.strong': [
{ event: 'onCapture', text: '*foo*', result: false },
{ event: 'onCapture', text: '**foo**', result: true },
{ event: 'onCapture', text: '***foo***', result: false },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '*foo*', result: false },
{ event: 'onHash', text: '**foo**', result: true },
{ event: 'onHash', text: '***foo***', result: false },
{ event: 'onHash', text: 'foo', result: false }
],
'emphasisAndStrong.emphasisAndStrong': [
{ event: 'onCapture', text: '*foo*', result: false },
{ event: 'onCapture', text: '**foo**', result: false },
{ event: 'onCapture', text: '***foo***', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '*foo*', result: false },
{ event: 'onHash', text: '**foo**', result: false },
{ event: 'onHash', text: '***foo***', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
githubCodeBlock: [
{ event: 'onStart', text: '```\nfoo\n```', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '```\nfoo\n```', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '```\nfoo\n```', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '```\nfoo\n```', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
heading: [
{ event: 'onStart', text: '# foo', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '# foo', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '# foo', result: true },
{ event: 'onCapture', text: 'foo\n---', result: true },
{ event: 'onCapture', text: 'foo\n===', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '# foo', result: true },
{ event: 'onHash', text: 'foo\n---', result: true },
{ event: 'onHash', text: 'foo\n===', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
horizontalRule: [
{ event: 'onStart', text: '---', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '---', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '---', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '---', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
image: [
{ event: 'onStart', text: '![foo](bar.jpg)', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '![foo](bar.jpg)', result: true },
{ event: 'onEnd', text: 'foo', result: true }
],
'image.inline': [
{ event: 'onCapture', text: '![foo](bar.jpg)', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '![foo](bar.jpg)', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
'image.reference': [
{ event: 'onCapture', text: '![foo][1]\n\n[1]: bar.jpg', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '![foo][1]\n\n[1]: bar.jpg', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
link: [
{ event: 'onStart', text: '[foo](bar.jpg)', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '[foo](bar.jpg)', result: true },
{ event: 'onEnd', text: 'foo', result: true }
],
'link.inline': [
{ event: 'onCapture', text: '[foo](bar.jpg)', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '[foo](bar.jpg)', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
'link.reference': [
{ event: 'onCapture', text: '[foo][1]\n\n[1]: bar.jpg', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '[foo][1]\n\n[1]: bar.jpg', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
list: [
{ event: 'onStart', text: '1. foo\n2.bar\n', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '1. foo\n2.bar\n', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '1. foo\n2.bar\n', result: true },
{ event: 'onCapture', text: 'foo', result: false },
//{ event: 'onHash', text: '1. foo\n2.bar\n', result: true },
//{ event: 'onHash', text: 'foo', result: false }
],
'list.listItem': [
{ event: 'onCapture', text: '1. foo\n2.bar\n', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '1. foo\n2.bar\n', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
'list.taskListItem': [
{ event: 'onCapture', text: '1. [X] foo\n2. [x] bar\n', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '1. [X] foo\n2. [x] bar\n', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
'list.taskListItem.checkbox': [
{ event: 'onCapture', text: '1. [X] foo\n2. [x] bar\n', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '1. [X] foo\n2. [x] bar\n', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
metadata: [
{ event: 'onStart', text: '«««yaml\nfoo: bar\n»»»\n', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '«««yaml\nfoo: bar\n»»»\n', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '«««yaml\nfoo: bar\n»»»\n', result: true },
{ event: 'onCapture', text: '---yaml\nfoo: bar\n---\n', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '«««yaml\nfoo: bar\n»»»\n', result: true },
{ event: 'onHash', text: '---yaml\nfoo: bar\n---\n', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
strikethrough: [
{ event: 'onStart', text: '~~foo~~', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '~~foo~~', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '~~foo~~', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '~~foo~~', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
table: [
{ event: 'onStart', text: '|foo|bar|\n|---|---|\n|1|2|', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '|foo|bar|\n|---|---|\n|1|2|', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '|foo|bar|\n|---|---|\n|1|2|', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '|foo|bar|\n|---|---|\n|1|2|', result: true },
{ event: 'onHash', text: 'foo', result: false }
],
underline: [
{ event: 'onStart', text: '__foo__', result: true },
{ event: 'onStart', text: 'foo', result: true },
{ event: 'onEnd', text: '__foo__', result: true },
{ event: 'onEnd', text: 'foo', result: true },
{ event: 'onCapture', text: '__foo__', result: true },
{ event: 'onCapture', text: 'foo', result: false },
{ event: 'onHash', text: '__foo__', result: true },
{ event: 'onHash', text: 'foo', result: false }
]
}
};
describe('event triggering', function () {
let converter;
before(function () {
converter = new showdown.Converter({
strikethrough: true,
tables: true,
ghCodeBlocks: true,
tasklists: true,
ghMentions: true,
emoji: true,
underline: true,
ellipsis: true,
metadata: true
});
});
describe('makehtml', function () {
/* jshint -W083*/
for (let parser in testSpec.makehtml) {
describe(parser, function () {
for (let ts in testSpec.makehtml[parser]) {
let event = 'makehtml.' + parser + '.' + testSpec.makehtml[parser][ts].event;
let md = testSpec.makehtml[parser][ts].text;
let title = (testSpec.makehtml[parser][ts].result) ? 'should ' : 'should NOT ';
title += 'trigger "' + event + ' event"';
let expected = testSpec.makehtml[parser][ts].result;
let actual = false;
it(title, function () {
converter.listen(event, function () {
actual = true;
});
converter.makeHtml(md);
expected.should.equal(actual);
});
}
});
}
/* jshint +W083*/
});
});
});

View File

@ -1,7 +1,6 @@
/**
* Created by Tivie on 27/01/2017.
*/
//let showdown = require('../../.build/showdown.js') || require('showdown');
describe('showdown.options', function () {
'use strict';