diff --git a/UI/Component/AdvancedRange.js b/UI/Component/AdvancedRange.js
new file mode 100644
index 0000000..e69de29
diff --git a/UI/Component/AdvancedSelect.js b/UI/Component/AdvancedSelect.js
index c19954e..353842d 100644
--- a/UI/Component/AdvancedSelect.js
+++ b/UI/Component/AdvancedSelect.js
@@ -1,6 +1,326 @@
// remote data
// select data could be template layout per element
// multi select
+// select with search feature for many options
// isn't this very similar to the advanced input? just a little different?
-// maybe not...
\ No newline at end of file
+// maybe not...
+
+import { Request } from '../../Message/Request/Request.js';
+/**
+ * Advanced input class.
+ *
+ * @copyright Dennis Eichhorn
+ * @license OMS License 1.0
+ * @version 1.0.0
+ * @since 1.0.0
+ *
+ * @todo: this class is probably the most stupid thing I've done in a long time. Seriously fix this!
+ * @todo: Passing self to every MEMBER function is just dumb.
+ */
+export class AdvancedSelect {
+ /**
+ * @constructor
+ *
+ * @param {object} e Element to bind
+ *
+ * @since 1.0.0
+ */
+ constructor(e) {
+ this.id = e.id;
+ this.selectComponent = e;
+ this.selectField = this.selectComponent.getElementsByClassName('input')[0];
+ this.dropdownElement = document.getElementById(this.id + '-dropdown');
+ this.tagElement = document.getElementById(this.id + '-tags');
+ this.dataList = this.dropdownElement.getElementsByTagName('table')[0];
+ this.dataListBody = this.dataList.getElementsByTagName('tbody')[0];
+ this.dataTpl = document.getElementById(this.id + '-rowElement');
+ this.tagTpl = this.tagElement.getElementsByTagName('template')[0];
+ this.src = this.selectField.getAttribute('data-src');
+
+ const self = this;
+ this.selectField.addEventListener('focusout', function (e) {
+ // todo: this also means that clicking on any other part of the result list that it disappears befor
+ // the click is registered in the result list since focusout has highest priority (e.g. sort button in table).
+ // so far i don't know a way to check if *any* element in the result div is clicked, if I could check this
+ // first then I could simply say, don't make the result div inactive!
+ if (e.relatedTarget === null ||
+ e.relatedTarget.parentElement === null ||
+ e.relatedTarget.parentElement.parentElement === null ||
+ !jsOMS.hasClass(e.relatedTarget.parentElement.parentElement.parentElement, 'dropdown')
+ ) {
+ jsOMS.removeClass(self.dropdownElement, 'active');
+ }
+ });
+
+ this.selectField.addEventListener('keydown', function (e) {
+ if (e.keyCode === 13 || e.keyCode === 40) {
+ jsOMS.preventAll(e);
+ }
+
+ if (e.keyCode === 40) {
+ // down-key
+ self.selectOption(self.dataListBody.firstElementChild);
+ jsOMS.preventAll(e);
+ } else {
+ // handle change delay
+ self.inputTimeDelay({ id: self.id, delay: 300 }, self.changeCallback, self, e);
+ }
+ });
+
+ this.selectField.addEventListener('focusin', function (e) {
+ jsOMS.addClass(self.dropdownElement, 'active');
+ });
+
+ this.dropdownElement.addEventListener('keydown', function (e) {
+ jsOMS.preventAll(e);
+
+ // todo: consider if it makes sense to have a none element always for phone users only to jump out?
+ // todo: if not remote then the suggestion dropdown should filter itself based on best match
+
+ if (e.keyCode === 27 || e.keyCode === 46 || e.keyCode === 8) {
+ // handle esc, del to go back to input field
+ self.inputField.focus();
+ self.clearDataListSelection(self);
+ } else if (e.keyCode === 38) {
+ // handle up-click
+ if (document.activeElement.previousElementSibling !== null) {
+ self.clearDataListSelection(self);
+ self.selectOption(document.activeElement.previousElementSibling);
+ }
+ } else if (e.keyCode === 40) {
+ // handle down-click
+ if (document.activeElement.nextElementSibling !== null) {
+ self.clearDataListSelection(self);
+ self.selectOption(document.activeElement.nextElementSibling);
+ }
+ } else if (e.keyCode === 13 || e.keyCode === 9) {
+ self.clearDataListSelection(self);
+ self.addToResultList(self);
+ }
+ });
+
+ this.dropdownElement.addEventListener('focusout', function (e) {
+ self.clearDataListSelection(self);
+ jsOMS.removeClass(self.dropdownElement, 'active');
+ });
+
+ this.dropdownElement.addEventListener('click', function (e) {
+ if (document.activeElement.tagName.toLowerCase() !== 'tr') {
+ return;
+ }
+
+ self.clearDataListSelection(self);
+ self.addToResultList(self);
+ jsOMS.removeClass(self.dropdownElement, 'active');
+ });
+ };
+
+ /**
+ * Handle remote data response result
+ *
+ * This method adds remote results to the dropdown list for selecting
+ *
+ * @param {object} self This reference
+ * @param {object} data Response data
+ *
+ * @since 1.0.0
+ */
+ remoteCallback(self, data) {
+ console.log(data);
+ data = JSON.parse(data.response)[0];
+ const dataLength = data.length;
+
+ console.table(data);
+
+ // if dropdown == true
+ if (self.dropdownElement.getAttribute('data-active') === 'true') {
+ while (self.dataListBody.firstChild) {
+ self.dataListBody.removeChild(self.dataListBody.firstChild);
+ }
+
+ for (let i = 0; i < dataLength; ++i) {
+ // set readable value
+ const newRow = self.dataTpl.content.cloneNode(true);
+ let fields = newRow.querySelectorAll('[data-tpl-text]');
+ let fieldLength = fields.length;
+
+ for (let j = 0; j < fieldLength; ++j) {
+ fields[j].appendChild(
+ document.createTextNode(
+ jsOMS.getArray(fields[j].getAttribute('data-tpl-text'), data[i])
+ )
+ );
+ }
+
+ // set internal value
+ fields = newRow.querySelectorAll('[data-tpl-value]');
+ fieldLength = fields.length;
+
+ for (let j = 0; j < fieldLength; ++j) {
+ fields[j].setAttribute(
+ 'data-value',
+ jsOMS.getArray(fields[j].getAttribute('data-tpl-value'), data[i])
+ );
+ }
+
+ self.dataListBody.appendChild(newRow);
+ self.dataListBody.lastElementChild.addEventListener('focusout', function (e) {
+ if (e.relatedTarget === null) {
+ return;
+ }
+
+ let sibling = e.relatedTarget.parentNode.firstElementChild;
+ do {
+ if (sibling === e.relatedTarget) {
+ jsOMS.preventAll(e);
+ return;
+ }
+ } while ((sibling = sibling.nextElementSibling) !== null);
+ });
+ }
+ }
+ };
+
+ /**
+ * Callback for input field content change
+ *
+ * @param {object} self This reference
+ *
+ * @since 1.0.0
+ */
+ changeCallback(self) {
+ // if remote data
+ if (typeof self.src !== 'undefined' && self.src !== '') {
+ const request = new Request(self.src);
+ request.setSuccess(function (data) { self.remoteCallback(self, data); });
+ request.send();
+ }
+ };
+
+ /**
+ * Select element in dropdown (only mark it as selected)
+ *
+ * @param {object} e Element to select in dropdown
+ *
+ * @since 1.0.0
+ */
+ selectOption(e) {
+ e.focus();
+ // todo: change to set style .active
+ e.setAttribute('style', 'background: #f00');
+ jsOMS.addClass(e, 'active');
+ };
+
+ /**
+ * Clear all selected/marked options in dropdown
+ *
+ * @param {object} self This reference
+ *
+ * @since 1.0.0
+ */
+ clearDataListSelection(self) {
+ const list = self.dataListBody.getElementsByTagName('tr'),
+ length = list.length;
+
+ for (let i = 0; i < length; ++i) {
+ // todo: remove the active class
+ list[i].setAttribute('style', '');
+ jsOMS.removeClass(list[i], 'active');
+ }
+ };
+
+ /**
+ * Add selected dropdown elements to some final result list
+ *
+ * This can add the selected dropdown elements to a table, badge list etc. depending on the template structure.
+ *
+ * @param {object} self This reference
+ *
+ * @since 1.0.0
+ */
+ addToResultList(self) {
+ if (self.inputField.getAttribute('data-autocomplete') === 'true') {
+ self.inputField.value = document.activeElement.querySelectorAll('[data-tpl-value="' + self.inputField.getAttribute('data-value') + '"]')[0].getAttribute('data-value');
+ }
+
+ if (self.tagElement.getAttribute('data-active') === 'true') {
+ // todo: make badges removable
+ const newTag = self.tagTpl.content.cloneNode(true);
+
+ // set internal value
+ let fields = newTag.querySelectorAll('[data-tpl-value]');
+ let fieldLength = fields.length;
+ let uuid = '';
+ let value = '';
+
+ for (let j = 0; j < fieldLength; ++j) {
+ value = document.activeElement.querySelectorAll('[data-tpl-value="' + fields[j].getAttribute('data-tpl-value') + '"]')[0].getAttribute('data-value');
+ fields[j].setAttribute('data-value', value);
+
+ uuid += value;
+ }
+
+ // don't allow duplicate
+ // todo: create a data-duplicat=true attribute to allow duplication and then have a count as part of the uuid (maybe row id)
+ if (self.tagElement.querySelectorAll('[data-tpl-uuid="' + uuid + '"').length !== 0) {
+ return;
+ }
+
+ newTag.firstElementChild.setAttribute('data-tpl-uuid', uuid);
+
+ // set readable text
+ fields = newTag.querySelectorAll('[data-tpl-text]');
+ fieldLength = fields.length;
+
+ for (let j = 0; j < fieldLength; ++j) {
+ fields[j].appendChild(
+ document.createTextNode(
+ document.activeElement.querySelectorAll('[data-tpl-text="' + fields[j].getAttribute('data-tpl-text') + '"]')[0].innerText
+ )
+ );
+ }
+
+ // allow limit
+ if (self.tagElement.childElementCount >= self.tagElement.getAttribute('data-limit')
+ && self.tagElement.getAttribute('data-limit') != 0
+ ) {
+ self.tagElement.removeChild(self.tagElement.firstElementChild);
+ }
+
+ self.tagElement.appendChild(newTag);
+ }
+
+ if (self.inputField.getAttribute('data-emptyAfter') === 'true') {
+ self.inputField.value = '';
+ }
+
+ self.inputField.focus();
+ };
+
+ /**
+ * Delay handler (e.g. delay after finishing typing)
+ *
+ * After waiting for a delay a callback can be triggered.
+ *
+ * @param {object} action Action type
+ * @param {function} callback Callback to be triggered
+ * @param {object} self This reference (passed to callback)
+ * @param {object} data Data (passed to callback)
+ *
+ * @since 1.0.0
+ */
+ inputTimeDelay(action, callback, self, data) {
+ if (AdvancedSelect.timerDelay[action.id]) {
+ clearTimeout(AdvancedSelect.timerDelay[action.id]);
+ delete AdvancedSelect.timerDelay[action.id]
+ }
+
+ AdvancedSelect.timerDelay[action.id] = setTimeout(function () {
+ delete AdvancedSelect.timerDelay[action.id];
+ callback(self, data);
+ }, action.delay);
+ };
+};
+
+AdvancedSelect.timerDelay = {};
\ No newline at end of file
diff --git a/UI/Component/Form.js b/UI/Component/Form.js
index a91fd68..60dd132 100644
--- a/UI/Component/Form.js
+++ b/UI/Component/Form.js
@@ -382,7 +382,7 @@ export class Form {
'#' + createForm + ' [data-tpl-value="' + fields[j].getAttribute('data-tpl-value') + '"], [data-form="' + createForm + '"][data-tpl-value="' + fields[j].getAttribute('data-tpl-value') + '"]')[0]
.getAttribute('data-value');
- // todo: we need to check what kind of tag the selector above returns in order to get the correct value. currently this only makes sense for input elements but for selection, checkboxes etc. this doesn't make sense there we need .innerHtml or [data-text=]
+ // todo: we need to check what kind of tag the selector above returns in order to get the correct value. currently this only makes sense for input elements but for selection, checkboxes etc. this doesn't make sense there we need .innerHTML or [data-text=]
fields[j].setAttribute('data-value', value);
@@ -407,7 +407,7 @@ export class Form {
)
);
- // todo: we need to check what kind of tag the selector above returns in order to get the correct value. currently this only makes sense for input elements but for selection, checkboxes etc. this doesn't make sense there we need .innerHtml or [data-text=]
+ // todo: we need to check what kind of tag the selector above returns in order to get the correct value. currently this only makes sense for input elements but for selection, checkboxes etc. this doesn't make sense there we need .innerHTML or [data-text=]
}
subMain.appendChild(newEle);
@@ -566,9 +566,27 @@ export class Form {
for (let i = 0; i < length; ++i) {
for (let j = 0; j < selectorLength; ++j) {
const matches = newEle[j].firstElementChild.querySelectorAll('[data-tpl-value="' + values[i].getAttribute('data-tpl-value') + '"');
- if (matches.length > 0) {
+
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
// todo: handle multiple matches
- matches[0].value = values[i].value;
+ // todo: urls remote src shouldn't start with http (it can but also the base path should be allowed or the current uri as basis... maybe define a data-tpl-value-srctype??? however this sounds stupid and might be too verbose or use http as identifier and use the fact that the request object uses internally the uri factory!!! sounds much smarter :))
+ // todo: implement this for other cases as well or potentially pull it out because it is very similar!!!
+ if (values[i].getAttribute('data-tpl-value').startsWith('http')
+ || values[i].getAttribute('data-tpl-value').startsWith('{')
+ ) {
+ const request = new Request(values[i].getAttribute('data-tpl-value'));
+ request.setResultCallback(200, function(xhr) {
+ matches[c].value = xhr.response;
+ // todo: the problem with this is that the response must only return the markdown or whatever is requested. It would be much nicer if it would also possible to define a path for the response in case a json object is returned which is very likely
+ });
+
+ request.send();
+ } else {
+ matches[c].value = self.getValueFromDataSource(values[i]);
+ matches[c].innerHTML = values[i].innerText;
+ }
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
// todo: handle different input types
}
@@ -579,10 +597,14 @@ export class Form {
for (let i = 0; i < length; ++i) {
for (let j = 0; j < selectorLength; ++j) {
const matches = newEle[j].firstElementChild.querySelectorAll('[data-tpl-text="' + text[i].getAttribute('data-tpl-text') + '"');
- if (matches.length > 0) {
+
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
// todo: handle multiple matches
- matches[0].value = text[i].innerText;
- // todo: handle different input types
+ matches[c].value = self.getTextFromDataSource(text[i]);
+ matches[c].innerHTML = text[i].innerHTML; // example for article instead of input field without value
+ // todo: handle different input types e.g. Article requires innerHTML instead of value
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
}
}
@@ -708,9 +730,13 @@ export class Form {
for (let i = 0; i < length; ++i) {
for (let j = 0; j < selectorLength; ++j) {
const matches = parentsContent[j].querySelectorAll('[data-tpl-value="' + values[i].getAttribute('data-tpl-value') + '"');
- if (matches.length > 0) {
+
+
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
// todo: handle multiple matches
- matches[0].value = values[i].value;
+ matches[c].value = self.getValueFromDataSource(values[i]);
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
// todo: handle different input types
}
@@ -721,10 +747,13 @@ export class Form {
for (let i = 0; i < length; ++i) {
for (let j = 0; j < selectorLength; ++j) {
const matches = parentsContent[j].querySelectorAll('[data-tpl-text="' + text[i].getAttribute('data-tpl-text') + '"');
- if (matches.length > 0) {
+
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
// todo: handle multiple matches
- matches[0].innerText = text[i].value;
+ matches[c].innerText = self.getTextFromDataSource(text[i]);
// todo: handle different input types
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
}
}
@@ -803,6 +832,8 @@ export class Form {
*/
bindUpdatableExternal(update, id)
{
+ const self = this;
+
update.addEventListener('click', function () {
const formElement = document.getElementById(id);
const parent = this.closest(formElement.getAttribute('data-ui-element'));
@@ -821,16 +852,29 @@ export class Form {
length = values.length;
for (let i = 0; i < length; ++i) {
// todo: handle multiple matches
- document.getElementById(formId).querySelectorAll('[data-tpl-value="' + values[i].getAttribute('data-tpl-value') + '"')[0].value = values[i].value;
+ const matches = document.getElementById(formId).querySelectorAll('[data-tpl-value="' + values[i].getAttribute('data-tpl-value') + '"');
+
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
+ matches[c].value = self.getValueFromDataSource(values[i]);
+ }
// todo: handle different input types
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
// insert row text data into form
length = text.length;
for (let i = 0; i < length; ++i) {
// todo: handle multiple matches
- document.getElementById(formId).querySelectorAll('[data-tpl-text="' + text[i].getAttribute('data-tpl-text') + '"')[0].value = text[i].innerText;
+ const matches = document.getElementById(formId).querySelectorAll('[data-tpl-text="' + text[i].getAttribute('data-tpl-text') + '"');
+
+ // consider pulling this out because it exists like 3x2 = 6 times in a similar way or at least 3 times very similarly
+ const matchLength = matches.length;
+ for (let c = 0; c < matchLength; ++c) {
+ matches[c].value = self.getTextFromDataSource(text[i]);
+ }
// todo: handle different input types
+ // todo handle remote data (e.g. value ist coming from backend. do special check for http)
}
// todo: replace add button with save button and add cancel button
@@ -875,4 +919,24 @@ export class Form {
}
});
};
+
+ setValueOfElement(src, dest)
+ {
+
+ };
+
+ setTextOfElement(src, dest)
+ {
+
+ };
+
+ getValueFromDataSource(src)
+ {
+ return src.value;
+ };
+
+ getTextFromDataSource(src)
+ {
+ return src.value;
+ };
};
\ No newline at end of file
diff --git a/Utils/Parser/Markdown.js b/Utils/Parser/Markdown.js
index e69de29..fb0f617 100644
--- a/Utils/Parser/Markdown.js
+++ b/Utils/Parser/Markdown.js
@@ -0,0 +1,5123 @@
+/*! showdown v 1.9.0 - 10-11-2018 */
+ /**
+ * Created by Tivie on 13-07-2015.
+ */
+
+ function getDefaultOpts(simple) {
+ 'use strict';
+
+ var defaultOptions = {
+ omitExtraWLInCodeBlocks: {
+ defaultValue: false,
+ describe: 'Omit the default extra whiteline added to code blocks',
+ type: 'boolean'
+ },
+ noHeaderId: {
+ defaultValue: false,
+ describe: 'Turn on/off generated header id',
+ type: 'boolean'
+ },
+ prefixHeaderId: {
+ defaultValue: false,
+ describe: 'Add a prefix to the generated header ids. Passing a string will prefix that string to the header id. Setting to true will add a generic \'section-\' prefix',
+ type: 'string'
+ },
+ rawPrefixHeaderId: {
+ defaultValue: false,
+ describe: 'Setting this option to true will prevent showdown from modifying the prefix. This might result in malformed IDs (if, for instance, the " char is used in the prefix)',
+ type: 'boolean'
+ },
+ ghCompatibleHeaderId: {
+ defaultValue: false,
+ describe: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
+ type: 'boolean'
+ },
+ rawHeaderId: {
+ defaultValue: false,
+ describe: 'Remove only spaces, \' and " from generated header ids (including prefixes), replacing them with dashes (-). WARNING: This might result in malformed ids',
+ type: 'boolean'
+ },
+ headerLevelStart: {
+ defaultValue: false,
+ describe: 'The header blocks level start',
+ type: 'integer'
+ },
+ parseImgDimensions: {
+ defaultValue: false,
+ describe: 'Turn on/off image dimension parsing',
+ type: 'boolean'
+ },
+ simplifiedAutoLink: {
+ defaultValue: false,
+ describe: 'Turn on/off GFM autolink style',
+ type: 'boolean'
+ },
+ excludeTrailingPunctuationFromURLs: {
+ defaultValue: false,
+ describe: 'Excludes trailing punctuation from links generated with autoLinking',
+ type: 'boolean'
+ },
+ literalMidWordUnderscores: {
+ defaultValue: false,
+ describe: 'Parse midword underscores as literal underscores',
+ type: 'boolean'
+ },
+ literalMidWordAsterisks: {
+ defaultValue: false,
+ describe: 'Parse midword asterisks as literal asterisks',
+ type: 'boolean'
+ },
+ strikethrough: {
+ defaultValue: false,
+ describe: 'Turn on/off strikethrough support',
+ type: 'boolean'
+ },
+ tables: {
+ defaultValue: false,
+ describe: 'Turn on/off tables support',
+ type: 'boolean'
+ },
+ tablesHeaderId: {
+ defaultValue: false,
+ describe: 'Add an id to table headers',
+ type: 'boolean'
+ },
+ ghCodeBlocks: {
+ defaultValue: true,
+ describe: 'Turn on/off GFM fenced code blocks support',
+ type: 'boolean'
+ },
+ tasklists: {
+ defaultValue: false,
+ describe: 'Turn on/off GFM tasklist support',
+ type: 'boolean'
+ },
+ smoothLivePreview: {
+ defaultValue: false,
+ describe: 'Prevents weird effects in live previews due to incomplete input',
+ type: 'boolean'
+ },
+ smartIndentationFix: {
+ defaultValue: false,
+ description: 'Tries to smartly fix indentation in es6 strings',
+ type: 'boolean'
+ },
+ disableForced4SpacesIndentedSublists: {
+ defaultValue: false,
+ description: 'Disables the requirement of indenting nested sublists by 4 spaces',
+ type: 'boolean'
+ },
+ simpleLineBreaks: {
+ defaultValue: false,
+ description: 'Parses simple line breaks as
(GFM Style)',
+ type: 'boolean'
+ },
+ requireSpaceBeforeHeadingText: {
+ defaultValue: false,
+ description: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
+ type: 'boolean'
+ },
+ ghMentions: {
+ defaultValue: false,
+ description: 'Enables github @mentions',
+ type: 'boolean'
+ },
+ ghMentionsLink: {
+ defaultValue: 'https://github.com/{u}',
+ description: 'Changes the link generated by @mentions. Only applies if ghMentions option is enabled.',
+ type: 'string'
+ },
+ encodeEmails: {
+ defaultValue: true,
+ description: 'Encode e-mail addresses through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities',
+ type: 'boolean'
+ },
+ openLinksInNewWindow: {
+ defaultValue: false,
+ description: 'Open all links in new windows',
+ type: 'boolean'
+ },
+ backslashEscapesHTMLTags: {
+ defaultValue: false,
+ description: 'Support for HTML Tag escaping. ex: \
tags around block-level tags. + text = showdown.subParser('hashHTMLBlocks')(text, options, globals); + text = showdown.subParser('paragraphs')(text, options, globals); + + text = globals.converter._dispatch('blockGamut.after', text, options, globals); + + return text; + }); + + showdown.subParser('blockQuotes', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('blockQuotes.before', text, options, globals); + + // 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('githubCodeBlocks')(bq, options, globals); + bq = showdown.subParser('blockGamut')(bq, options, globals); // recurse + + bq = bq.replace(/(^|\n)/g, '$1 '); + // These leading spaces screw with
content, so we need to fix that:
+ bq = bq.replace(/(\s*[^\r]+?<\/pre>)/gm, function (wholeMatch, m1) {
+ var pre = m1;
+ // attacklab: hack around Konqueror 3.5.4 bug:
+ pre = pre.replace(/^ /mg, '¨0');
+ pre = pre.replace(/¨0/g, '');
+ return pre;
+ });
+
+ return showdown.subParser('hashBlock')('\n' + bq + '\n
', options, globals);
+ });
+
+ text = globals.converter._dispatch('blockQuotes.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Process Markdown `` blocks.
+ */
+ showdown.subParser('codeBlocks', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
+
+ // 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('outdent')(codeblock, options, globals);
+ codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
+ codeblock = showdown.subParser('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 = '' + codeblock + end + '
';
+
+ return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
+ });
+
+ // strip sentinel
+ text = text.replace(/¨0/, '');
+
+ text = globals.converter._dispatch('codeBlocks.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ *
+ * * Backtick quotes are used for 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:
+ *
+ * Just type foo `bar` baz at the prompt.
+ *
+ * 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 `bar` ...
+ */
+ showdown.subParser('codeSpans', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('codeSpans.before', text, options, globals);
+
+ 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('encodeCode')(c, options, globals);
+ c = m1 + '' + c + '';
+ c = showdown.subParser('hashHTMLSpans')(c, options, globals);
+ return c;
+ }
+ );
+
+ text = globals.converter._dispatch('codeSpans.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Create a full HTML document from the processed markdown
+ */
+ showdown.subParser('completeHTMLDocument', function (text, options, globals) {
+ 'use strict';
+
+ if (!options.completeHTMLDocument) {
+ return text;
+ }
+
+ text = globals.converter._dispatch('completeHTMLDocument.before', text, options, globals);
+
+ var doctype = 'html',
+ doctypeParsed = '\n',
+ title = '',
+ charset = '\n',
+ lang = '',
+ metadata = '';
+
+ if (typeof globals.metadata.parsed.doctype !== 'undefined') {
+ doctypeParsed = '\n';
+ doctype = globals.metadata.parsed.doctype.toString().toLowerCase();
+ if (doctype === 'html' || doctype === 'html5') {
+ charset = '';
+ }
+ }
+
+ for (var meta in globals.metadata.parsed) {
+ if (globals.metadata.parsed.hasOwnProperty(meta)) {
+ switch (meta.toLowerCase()) {
+ case 'doctype':
+ break;
+
+ case 'title':
+ title = '' + globals.metadata.parsed.title + ' \n';
+ break;
+
+ case 'charset':
+ if (doctype === 'html' || doctype === 'html5') {
+ charset = '\n';
+ } else {
+ charset = '\n';
+ }
+ break;
+
+ case 'language':
+ case 'lang':
+ lang = ' lang="' + globals.metadata.parsed[meta] + '"';
+ metadata += '\n';
+ break;
+
+ default:
+ metadata += '\n';
+ }
+ }
+ }
+
+ text = doctypeParsed + '\n\n' + title + charset + metadata + '\n\n' + text.trim() + '\n\n';
+
+ text = globals.converter._dispatch('completeHTMLDocument.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Convert all tabs to spaces
+ */
+ showdown.subParser('detab', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('detab.before', text, options, globals);
+
+ // expand first n-1 tabs
+ text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
+
+ // replace the nth with two sentinels
+ text = text.replace(/\t/g, '¨A¨B');
+
+ // use the sentinel to anchor our regex so it doesn't explode
+ text = text.replace(/¨B(.+?)¨A/g, function (wholeMatch, m1) {
+ var leadingText = m1,
+ numSpaces = 4 - leadingText.length % 4; // g_tab_width
+
+ // there *must* be a better way to do this:
+ for (var i = 0; i < numSpaces; i++) {
+ leadingText += ' ';
+ }
+
+ return leadingText;
+ });
+
+ // clean up sentinels
+ text = text.replace(/¨A/g, ' '); // g_tab_width
+ text = text.replace(/¨B/g, '');
+
+ text = globals.converter._dispatch('detab.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('ellipsis', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('ellipsis.before', text, options, globals);
+
+ text = text.replace(/\.\.\./g, '…');
+
+ text = globals.converter._dispatch('ellipsis.after', text, options, globals);
+
+ return text;
+ });
+
+ /**
+ * Turn emoji codes into emojis
+ *
+ * List of supported emojis: https://github.com/showdownjs/showdown/wiki/Emojis
+ */
+ showdown.subParser('emoji', function (text, options, globals) {
+ 'use strict';
+
+ if (!options.emoji) {
+ return text;
+ }
+
+ text = globals.converter._dispatch('emoji.before', text, options, globals);
+
+ var emojiRgx = /:([\S]+?):/g;
+
+ text = text.replace(emojiRgx, function (wm, emojiCode) {
+ if (showdown.helper.emojis.hasOwnProperty(emojiCode)) {
+ return showdown.helper.emojis[emojiCode];
+ }
+ return wm;
+ });
+
+ text = globals.converter._dispatch('emoji.after', text, options, globals);
+
+ return text;
+ });
+
+ /**
+ * Smart processing for ampersands and angle brackets that need to be encoded.
+ */
+ showdown.subParser('encodeAmpsAndAngles', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('encodeAmpsAndAngles.before', text, options, globals);
+
+ // 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, '&');
+
+ // Encode naked <'s
+ text = text.replace(/<(?![a-z\/?$!])/gi, '<');
+
+ // Encode <
+ text = text.replace(/
+ text = text.replace(/>/g, '>');
+
+ text = globals.converter._dispatch('encodeAmpsAndAngles.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Returns the string, with after processing the following backslash escape sequences.
+ *
+ * attacklab: The polite way to do this is with the new escapeCharacters() function:
+ *
+ * text = escapeCharacters(text,"\\",true);
+ * text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
+ *
+ * ...but we're sidestepping its use of the (slow) RegExp constructor
+ * as an optimization for Firefox. This function gets called a LOT.
+ */
+ showdown.subParser('encodeBackslashEscapes', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('encodeBackslashEscapes.before', text, options, globals);
+
+ text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback);
+ text = text.replace(/\\([`*_{}\[\]()>#+.!~=|-])/g, showdown.helper.escapeCharactersCallback);
+
+ text = globals.converter._dispatch('encodeBackslashEscapes.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Encode/escape certain characters inside Markdown code runs.
+ * The point is that in code, these characters are literals,
+ * and lose their special Markdown meanings.
+ */
+ showdown.subParser('encodeCode', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('encodeCode.before', text, options, globals);
+
+ // Encode all ampersands; HTML entities are not
+ // entities within a Markdown code span.
+ text = text
+ .replace(/&/g, '&')
+ // Do the angle bracket song and dance:
+ .replace(//g, '>')
+ // Now, escape characters that are magic in Markdown:
+ .replace(/([*_{}\[\]\\=~-])/g, showdown.helper.escapeCharactersCallback);
+
+ text = globals.converter._dispatch('encodeCode.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Within tags -- meaning between < and > -- encode [\ ` * _ ~ =] so they
+ * don't conflict with their use in Markdown for code, italics and strong.
+ */
+ showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('escapeSpecialCharsWithinTagAttributes.before', text, options, globals);
+
+ // Build a regex to find HTML tags.
+ var tags = /<\/?[a-z\d_:-]+(?:[\s]+[\s\S]+?)?>/gi,
+ comments = /-]|-[^>])(?:[^-]|-[^-])*)--)>/gi;
+
+ text = text.replace(tags, function (wholeMatch) {
+ return wholeMatch
+ .replace(/(.)<\/?code>(?=.)/g, '$1`')
+ .replace(/([\\`*_~=|])/g, showdown.helper.escapeCharactersCallback);
+ });
+
+ text = text.replace(comments, function (wholeMatch) {
+ return wholeMatch
+ .replace(/([\\`*_~=|])/g, showdown.helper.escapeCharactersCallback);
+ });
+
+ text = globals.converter._dispatch('escapeSpecialCharsWithinTagAttributes.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Handle github codeblocks prior to running HashHTML so that
+ * HTML contained within the codeblock gets escaped properly
+ * Example:
+ * ```ruby
+ * def hello_world(x)
+ * puts "Hello, #{x}"
+ * end
+ * ```
+ */
+ showdown.subParser('githubCodeBlocks', function (text, options, globals) {
+ 'use strict';
+
+ // early exit if option is not enabled
+ if (!options.ghCodeBlocks) {
+ return text;
+ }
+
+ text = globals.converter._dispatch('githubCodeBlocks.before', text, options, globals);
+
+ text += '¨0';
+
+ text = text.replace(/(?:^|\n)(?: {0,3})(```+|~~~+)(?: *)([^\s`~]*)\n([\s\S]*?)\n(?: {0,3})\1/g, function (wholeMatch, delim, language, codeblock) {
+ var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
+
+ // First parse the github code block
+ codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
+ codeblock = showdown.subParser('detab')(codeblock, options, globals);
+ codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
+ codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
+
+ codeblock = '' + codeblock + end + '
';
+
+ codeblock = showdown.subParser('hashBlock')(codeblock, options, globals);
+
+ // Since GHCodeblocks can be false positives, we need to
+ // store the primitive text and the parsed text in a global var,
+ // and then return a token
+ return '\n\n¨G' + (globals.ghCodeBlocks.push({ text: wholeMatch, codeblock: codeblock }) - 1) + 'G\n\n';
+ });
+
+ // attacklab: strip sentinel
+ text = text.replace(/¨0/, '');
+
+ return globals.converter._dispatch('githubCodeBlocks.after', text, options, globals);
+ });
+
+ showdown.subParser('hashBlock', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('hashBlock.before', text, options, globals);
+ text = text.replace(/(^\n+|\n+$)/g, '');
+ text = '\n\n¨K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
+ text = globals.converter._dispatch('hashBlock.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Hash and escape elements that should not be parsed as markdown
+ */
+ showdown.subParser('hashCodeTags', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('hashCodeTags.before', text, options, globals);
+
+ var repFunc = function (wholeMatch, match, left, right) {
+ var codeblock = left + showdown.subParser('encodeCode')(match, options, globals) + right;
+ return '¨C' + (globals.gHtmlSpans.push(codeblock) - 1) + 'C';
+ };
+
+ // Hash naked
+ text = showdown.helper.replaceRecursiveRegExp(text, repFunc, ']*>', '', 'gim');
+
+ text = globals.converter._dispatch('hashCodeTags.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('hashElement', function (text, options, globals) {
+ 'use strict';
+
+ return function (wholeMatch, m1) {
+ var blockText = m1;
+
+ // Undo double lines
+ blockText = blockText.replace(/\n\n/g, '\n');
+ blockText = blockText.replace(/^\n/, '');
+
+ // strip trailing blank lines
+ blockText = blockText.replace(/\n+$/g, '');
+
+ // Replace the element text with a marker ("¨KxK" where x is its key)
+ blockText = '\n\n¨K' + (globals.gHtmlBlocks.push(blockText) - 1) + 'K\n\n';
+
+ return blockText;
+ };
+ });
+
+ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('hashHTMLBlocks.before', text, options, globals);
+
+ var blockTags = [
+ 'pre',
+ 'div',
+ 'h1',
+ 'h2',
+ 'h3',
+ 'h4',
+ 'h5',
+ 'h6',
+ 'blockquote',
+ 'table',
+ 'dl',
+ 'ol',
+ 'ul',
+ 'script',
+ 'noscript',
+ 'form',
+ 'fieldset',
+ 'iframe',
+ 'math',
+ 'style',
+ 'section',
+ 'header',
+ 'footer',
+ 'nav',
+ 'article',
+ 'aside',
+ 'address',
+ 'audio',
+ 'canvas',
+ 'figure',
+ 'hgroup',
+ 'output',
+ 'video',
+ 'p'
+ ],
+ repFunc = function (wholeMatch, match, left, right) {
+ var txt = wholeMatch;
+ // check if this html element is marked as markdown
+ // if so, it's contents should be parsed as markdown
+ if (left.search(/\bmarkdown\b/) !== -1) {
+ txt = left + globals.converter.makeHtml(match) + right;
+ }
+ return '\n\n¨K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
+ };
+
+ if (options.backslashEscapesHTMLTags) {
+ // encode backslash escaped HTML tags
+ text = text.replace(/\\<(\/?[^>]+?)>/g, function (wm, inside) {
+ return '<' + inside + '>';
+ });
+ }
+
+ // hash HTML Blocks
+ for (var i = 0; i < blockTags.length; ++i) {
+
+ var opTagPos,
+ rgx1 = new RegExp('^ {0,3}(<' + blockTags[i] + '\\b[^>]*>)', 'im'),
+ patLeft = '<' + blockTags[i] + '\\b[^>]*>',
+ patRight = '' + blockTags[i] + '>';
+ // 1. Look for the first position of the first opening HTML tag in the text
+ while ((opTagPos = showdown.helper.regexIndexOf(text, rgx1)) !== -1) {
+
+ // if the HTML tag is \ escaped, we need to escape it and break
+
+
+ //2. Split the text in that position
+ var subTexts = showdown.helper.splitAtIndex(text, opTagPos),
+ //3. Match recursively
+ newSubText1 = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');
+
+ // prevent an infinite loop
+ if (newSubText1 === subTexts[1]) {
+ break;
+ }
+ text = subTexts[0].concat(newSubText1);
+ }
+ }
+ // HR SPECIAL CASE
+ text = text.replace(/(\n {0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
+ showdown.subParser('hashElement')(text, options, globals));
+
+ // Special case for standalone HTML comments
+ text = showdown.helper.replaceRecursiveRegExp(text, function (txt) {
+ return '\n\n¨K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
+ }, '^ {0,3}', 'gm');
+
+ // PHP and ASP-style processor instructions (...?> and <%...%>)
+ text = text.replace(/(?:\n\n)( {0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
+ showdown.subParser('hashElement')(text, options, globals));
+
+ text = globals.converter._dispatch('hashHTMLBlocks.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Hash span elements that should not be parsed as markdown
+ */
+ showdown.subParser('hashHTMLSpans', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('hashHTMLSpans.before', text, options, globals);
+
+ function hashHTMLSpan(html) {
+ return '¨C' + (globals.gHtmlSpans.push(html) - 1) + 'C';
+ }
+
+ // Hash Self Closing tags
+ text = text.replace(/<[^>]+?\/>/gi, function (wm) {
+ return hashHTMLSpan(wm);
+ });
+
+ // Hash tags without properties
+ text = text.replace(/<([^>]+?)>[\s\S]*?<\/\1>/g, function (wm) {
+ return hashHTMLSpan(wm);
+ });
+
+ // Hash tags with properties
+ text = text.replace(/<([^>]+?)\s[^>]+?>[\s\S]*?<\/\1>/g, function (wm) {
+ return hashHTMLSpan(wm);
+ });
+
+ // Hash self closing tags without />
+ text = text.replace(/<[^>]+?>/gi, function (wm) {
+ return hashHTMLSpan(wm);
+ });
+
+ /*showdown.helper.matchRecursiveRegExp(text, ']*>', '', 'gi');*/
+
+ text = globals.converter._dispatch('hashHTMLSpans.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Unhash HTML spans
+ */
+ showdown.subParser('unhashHTMLSpans', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('unhashHTMLSpans.before', text, options, globals);
+
+ 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('unhashHTMLSpans.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Hash and escape elements that should not be parsed as markdown
+ */
+ showdown.subParser('hashPreCodeTags', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('hashPreCodeTags.before', text, options, globals);
+
+ var repFunc = function (wholeMatch, match, left, right) {
+ // encode html entities
+ var codeblock = left + showdown.subParser('encodeCode')(match, options, globals) + right;
+ return '\n\n¨G' + (globals.ghCodeBlocks.push({ text: wholeMatch, codeblock: codeblock }) - 1) + 'G\n\n';
+ };
+
+ // Hash
+ text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^ {0,3}]*>\\s*]*>', '^ {0,3}\\s*
', 'gim');
+
+ text = globals.converter._dispatch('hashPreCodeTags.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('headers', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('headers.before', text, options, globals);
+
+ 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('spanGamut')(m1, options, globals),
+ hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
+ hLevel = headerLevelStart,
+ hashBlock = '' + spanGamut + ' ';
+ return showdown.subParser('hashBlock')(hashBlock, options, globals);
+ });
+
+ text = text.replace(setextRegexH2, function (matchFound, m1) {
+ var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
+ hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
+ hLevel = headerLevelStart + 1,
+ hashBlock = '' + spanGamut + ' ';
+ return showdown.subParser('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('spanGamut')(hText, options, globals),
+ hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
+ hLevel = headerLevelStart - 1 + m1.length,
+ header = '' + span + ' ';
+
+ return showdown.subParser('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(/&/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(/&/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('headers.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Turn Markdown link shortcuts into XHTML tags.
+ */
+ showdown.subParser('horizontalRule', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('horizontalRule.before', text, options, globals);
+
+ var key = showdown.subParser('hashBlock')('
', options, globals);
+ text = text.replace(/^ {0,2}( ?-){3,}[ \t]*$/gm, key);
+ text = text.replace(/^ {0,2}( ?\*){3,}[ \t]*$/gm, key);
+ text = text.replace(/^ {0,2}( ?_){3,}[ \t]*$/gm, key);
+
+ text = globals.converter._dispatch('horizontalRule.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Turn Markdown image shortcuts into
tags.
+ */
+ showdown.subParser('images', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('images.before', text, options, globals);
+
+ 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 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, '"')
+ //altText = showdown.helper.escapeCharacters(altText, '*_', false);
+ .replace(showdown.helper.regexes.asteriskDashAndColon, showdown.helper.escapeCharactersCallback);
+ //url = showdown.helper.escapeCharacters(url, '*_', false);
+ url = url.replace(showdown.helper.regexes.asteriskDashAndColon, showdown.helper.escapeCharactersCallback);
+ var result = '
';
+
+ return result;
+ }
+
+ // First, handle reference-style labeled images: ![alt text][id]
+ text = text.replace(referenceRegExp, writeImageTag);
+
+ // Next, handle inline images: 
+
+ // base64 encoded images
+ text = text.replace(base64RegExp, writeImageTagBase64);
+
+ // cases with crazy urls like ./image/cat1).png
+ text = text.replace(crazyRegExp, writeImageTag);
+
+ // normal cases
+ text = text.replace(inlineRegExp, writeImageTag);
+
+ // handle reference-style shortcuts: ![img text]
+ text = text.replace(refShortcutRegExp, writeImageTag);
+
+ text = globals.converter._dispatch('images.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('italicsAndBold', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('italicsAndBold.before', text, options, globals);
+
+ // 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) {
+ /*
+ if (options.simplifiedAutoLink) {
+ txt = showdown.subParser('simplifiedAutoLinks')(txt, options, globals);
+ }
+ */
+ return left + txt + right;
+ }
+
+ // Parse underscores
+ if (options.literalMidWordUnderscores) {
+ text = text.replace(/\b___(\S[\s\S]*?)___\b/g, function (wm, txt) {
+ return parseInside(txt, '', '');
+ });
+ text = text.replace(/\b__(\S[\s\S]*?)__\b/g, function (wm, txt) {
+ return parseInside(txt, '', '');
+ });
+ text = text.replace(/\b_(\S[\s\S]*?)_\b/g, function (wm, txt) {
+ return parseInside(txt, '', '');
+ });
+ } else {
+ text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
+ return (/\S$/.test(m)) ? parseInside(m, '', '') : wm;
+ });
+ text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
+ return (/\S$/.test(m)) ? parseInside(m, '', '') : 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, '', '') : wm;
+ });
+ }
+
+ // Now parse asterisks
+ if (options.literalMidWordAsterisks) {
+ text = text.replace(/([^*]|^)\B\*\*\*(\S[\s\S]*?)\*\*\*\B(?!\*)/g, function (wm, lead, txt) {
+ return parseInside(txt, lead + '', '');
+ });
+ text = text.replace(/([^*]|^)\B\*\*(\S[\s\S]*?)\*\*\B(?!\*)/g, function (wm, lead, txt) {
+ return parseInside(txt, lead + '', '');
+ });
+ text = text.replace(/([^*]|^)\B\*(\S[\s\S]*?)\*\B(?!\*)/g, function (wm, lead, txt) {
+ return parseInside(txt, lead + '', '');
+ });
+ } else {
+ text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) {
+ return (/\S$/.test(m)) ? parseInside(m, '', '') : wm;
+ });
+ text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
+ return (/\S$/.test(m)) ? parseInside(m, '', '') : 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, '', '') : wm;
+ });
+ }
+
+
+ text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Form HTML ordered (numbered) and unordered (bulleted) lists.
+ */
+ showdown.subParser('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]+((\[(x|X| )?])?[ \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
+ if (options.disableForced4SpacesIndentedSublists) {
+ rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0|\2([*+-]|\d+[.])[ \t]+))/gm;
+ }
+
+ listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) {
+ checked = (checked && checked.trim() !== '');
+
+ var item = showdown.subParser('outdent')(m4, options, globals),
+ bulletStyle = '';
+
+ // Support for github tasklists
+ if (taskbtn && options.tasklists) {
+ bulletStyle = ' class="task-list-item" style="list-style-type: none;"';
+ item = item.replace(/^[ \t]*\[(x|X| )?]/m, function () {
+ var otp = '';
+ return otp;
+ });
+ }
+
+ // ISSUE #312
+ // This input: - - - a
+ // causes trouble to the parser, since it interprets it as:
+ // - a
+ // instead of:
+ // - - - a
+ // 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;
+ });
+
+ // m1 - Leading line or
+ // Has a double return (multi paragraph) or
+ // Has sublist
+ if (m1 || (item.search(/\n{2,}/) > -1)) {
+ item = showdown.subParser('githubCodeBlocks')(item, options, globals);
+ item = showdown.subParser('blockGamut')(item, options, globals);
+ } else {
+ // Recursion for sub-lists:
+ item = showdown.subParser('lists')(item, options, globals);
+ item = item.replace(/\n$/, ''); // chomp(item)
+ item = showdown.subParser('hashHTMLBlocks')(item, options, globals);
+
+ // Colapse double linebreaks
+ item = item.replace(/\n\n+/g, '\n\n');
+ if (isParagraphed) {
+ item = showdown.subParser('paragraphs')(item, options, globals);
+ } else {
+ item = showdown.subParser('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 = '' + item + ' \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 **/
+ text = globals.converter._dispatch('lists.before', text, options, globals);
+ // 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(/^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
+ function (wholeMatch, list, m2) {
+ var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
+ return parseConsecutiveLists(list, listType, true);
+ }
+ );
+ } else {
+ text = text.replace(/(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
+ function (wholeMatch, m1, list, m3) {
+ var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
+ return parseConsecutiveLists(list, listType, false);
+ }
+ );
+ }
+
+ // strip sentinel
+ text = text.replace(/¨0/, '');
+ text = globals.converter._dispatch('lists.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Parse metadata at the top of the document
+ */
+ showdown.subParser('metadata', function (text, options, globals) {
+ 'use strict';
+
+ if (!options.metadata) {
+ return text;
+ }
+
+ text = globals.converter._dispatch('metadata.before', text, options, globals);
+
+ function parseMetadataContents(content) {
+ // raw is raw so it's not changed in any way
+ globals.metadata.raw = content;
+
+ // escape chars forbidden in html attributes
+ // double quotes
+ content = content
+ // ampersand first
+ .replace(/&/g, '&')
+ // double quotes
+ .replace(/"/g, '"');
+
+ content = content.replace(/\n {4}/g, ' ');
+ content.replace(/^([\S ]+): +([\s\S]+?)$/gm, function (wm, key, value) {
+ globals.metadata.parsed[key] = value;
+ return '';
+ });
+ }
+
+ text = text.replace(/^\s*«««+(\S*?)\n([\s\S]+?)\n»»»+\n/, function (wholematch, format, content) {
+ parseMetadataContents(content);
+ return '¨M';
+ });
+
+ text = text.replace(/^\s*---+(\S*?)\n([\s\S]+?)\n---+\n/, function (wholematch, format, content) {
+ if (format) {
+ globals.metadata.format = format;
+ }
+ parseMetadataContents(content);
+ return '¨M';
+ });
+
+ text = text.replace(/¨M/g, '');
+
+ text = globals.converter._dispatch('metadata.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ * Remove one level of line-leading tabs or spaces
+ */
+ showdown.subParser('outdent', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('outdent.before', text, options, globals);
+
+ // 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('outdent.after', text, options, globals);
+ return text;
+ });
+
+ /**
+ *
+ */
+ showdown.subParser('paragraphs', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('paragraphs.before', text, options, globals);
+ // Strip leading and trailing lines:
+ text = text.replace(/^\n+/g, '');
+ text = text.replace(/\n+$/g, '');
+
+ var grafs = text.split(/\n{2,}/g),
+ grafsOut = [],
+ end = grafs.length; // Wrap tags
+
+ for (var i = 0; i < end; i++) {
+ var str = grafs[i];
+ // if this is an HTML marker, copy it
+ if (str.search(/¨(K|G)(\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('spanGamut')(str, options, globals);
+ str = str.replace(/^([ \t]*)/g, '
');
+ str += '
';
+ grafsOut.push(str);
+ }
+ }
+
+ /** Unhashify HTML blocks */
+ end = grafsOut.length;
+ for (i = 0; i < end; i++) {
+ var blockText = '',
+ grafsOutIt = grafsOut[i],
+ 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)) {
+ var delim = RegExp.$1,
+ num = RegExp.$2;
+
+ if (delim === 'K') {
+ blockText = globals.gHtmlBlocks[num];
+ } else {
+ // we need to check if ghBlock is a false positive
+ if (codeFlag) {
+ // use encoded version of all text
+ blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text, options, globals);
+ } else {
+ blockText = globals.ghCodeBlocks[num].codeblock;
+ }
+ }
+ blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
+
+ grafsOutIt = grafsOutIt.replace(/(\n\n)?¨(K|G)\d+\2(\n\n)?/, blockText);
+ // Check if grafsOutIt is a pre->code
+ if (/^]*>\s*]*>/.test(grafsOutIt)) {
+ codeFlag = true;
+ }
+ }
+ grafsOut[i] = grafsOutIt;
+ }
+ text = grafsOut.join('\n');
+ // Strip leading and trailing lines:
+ text = text.replace(/^\n+/g, '');
+ text = text.replace(/\n+$/g, '');
+ return globals.converter._dispatch('paragraphs.after', text, options, globals);
+ });
+
+ /**
+ * Run extension
+ */
+ showdown.subParser('runExtension', function (ext, text, options, globals) {
+ 'use strict';
+
+ if (ext.filter) {
+ text = ext.filter(text, globals.converter, options);
+
+ } else if (ext.regex) {
+ // TODO remove this when old extension loading mechanism is deprecated
+ var re = ext.regex;
+ if (!(re instanceof RegExp)) {
+ re = new RegExp(re, 'g');
+ }
+ text = text.replace(re, ext.replace);
+ }
+
+ return text;
+ });
+
+ /**
+ * These are all the transformations that occur *within* block-level
+ * tags like paragraphs, headers, and list items.
+ */
+ showdown.subParser('spanGamut', function (text, options, globals) {
+ 'use strict';
+
+ text = globals.converter._dispatch('spanGamut.before', text, options, globals);
+ text = showdown.subParser('codeSpans')(text, options, globals);
+ text = showdown.subParser('escapeSpecialCharsWithinTagAttributes')(text, options, globals);
+ text = showdown.subParser('encodeBackslashEscapes')(text, options, globals);
+
+ // Process anchor and image tags. Images must come first,
+ // because ![foo][f] looks like an anchor.
+ text = showdown.subParser('images')(text, options, globals);
+ text = showdown.subParser('anchors')(text, options, globals);
+
+ // Make links out of things like ` `
+ // Must come after anchors, because you can use < and >
+ // delimiters in inline links like [this]().
+ text = showdown.subParser('autoLinks')(text, options, globals);
+ text = showdown.subParser('simplifiedAutoLinks')(text, options, globals);
+ text = showdown.subParser('emoji')(text, options, globals);
+ text = showdown.subParser('underline')(text, options, globals);
+ text = showdown.subParser('italicsAndBold')(text, options, globals);
+ text = showdown.subParser('strikethrough')(text, options, globals);
+ text = showdown.subParser('ellipsis')(text, options, globals);
+
+ // we need to hash HTML tags inside spans
+ text = showdown.subParser('hashHTMLSpans')(text, options, globals);
+
+ // now we encode amps and angles
+ text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals);
+
+ // Do hard breaks
+ if (options.simpleLineBreaks) {
+ // GFM style hard breaks
+ // only add line breaks if the text does not contain a block (special case for lists)
+ if (!/\n\n¨K/.test(text)) {
+ text = text.replace(/\n+/g, '
\n');
+ }
+ } else {
+ // Vanilla hard breaks
+ text = text.replace(/ +\n/g, '
\n');
+ }
+
+ text = globals.converter._dispatch('spanGamut.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('strikethrough', function (text, options, globals) {
+ 'use strict';
+
+ function parseInside(txt) {
+ if (options.simplifiedAutoLink) {
+ txt = showdown.subParser('simplifiedAutoLinks')(txt, options, globals);
+ }
+ return '' + txt + '';
+ }
+
+ if (options.strikethrough) {
+ text = globals.converter._dispatch('strikethrough.before', text, options, globals);
+ text = text.replace(/(?:~){2}([\s\S]+?)(?:~){2}/g, function (wm, txt) { return parseInside(txt); });
+ text = globals.converter._dispatch('strikethrough.after', text, options, globals);
+ }
+
+ return text;
+ });
+
+ /**
+ * Strips link definitions from text, stores the URLs and titles in
+ * hash references.
+ * Link defs are in the form: ^[id]: url "optional title"
+ */
+ showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
+ '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;
+
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
+ text += '¨0';
+
+ var replaceFunc = function (wholeMatch, linkId, url, width, height, blankLines, title) {
+ linkId = linkId.toLowerCase();
+ if (url.match(/^data:.+?\/.+?;base64,/)) {
+ // remove newlines
+ globals.gUrls[linkId] = url.replace(/\s/g, '');
+ } else {
+ globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url, options, globals); // Link IDs are case-insensitive
+ }
+
+ if (blankLines) {
+ // Oops, found blank lines, so it's not a title.
+ // Put back the parenthetical statement we stole.
+ return blankLines + title;
+
+ } else {
+ if (title) {
+ globals.gTitles[linkId] = title.replace(/"|'/g, '"');
+ }
+ if (options.parseImgDimensions && width && height) {
+ globals.gDimensions[linkId] = {
+ width: width,
+ height: height
+ };
+ }
+ }
+ // Completely remove the definition from the text
+ return '';
+ };
+
+ // first we try to find base64 link references
+ text = text.replace(base64Regex, replaceFunc);
+
+ text = text.replace(regex, replaceFunc);
+
+ // attacklab: strip sentinel
+ text = text.replace(/¨0/, '');
+
+ return text;
+ });
+
+ showdown.subParser('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('spanGamut')(header, options, globals);
+
+ return '' + header + ' \n';
+ }
+
+ function parseCells(cell, style) {
+ var subText = showdown.subParser('spanGamut')(cell, options, globals);
+ return '' + subText + ' \n';
+ }
+
+ function buildTable(headers, cells) {
+ var tb = '\n\n\n',
+ tblLgn = headers.length;
+
+ for (var i = 0; i < tblLgn; ++i) {
+ tb += headers[i];
+ }
+ tb += ' \n\n\n';
+
+ for (i = 0; i < cells.length; ++i) {
+ tb += '\n';
+ for (var ii = 0; ii < tblLgn; ++ii) {
+ tb += cells[i][ii];
+ }
+ tb += ' \n';
+ }
+ tb += '\n
\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('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('tables.before', text, options, globals);
+
+ // 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('tables.after', text, options, globals);
+
+ return text;
+ });
+
+ showdown.subParser('underline', function (text, options, globals) {
+ 'use strict';
+
+ if (!options.underline) {
+ return text;
+ }
+
+ text = globals.converter._dispatch('underline.before', text, options, globals);
+
+ if (options.literalMidWordUnderscores) {
+ text = text.replace(/\b___(\S[\s\S]*?)___\b/g, function (wm, txt) {
+ return '' + txt + '';
+ });
+ text = text.replace(/\b__(\S[\s\S]*?)__\b/g, function (wm, txt) {
+ return '' + txt + '';
+ });
+ } else {
+ text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
+ return (/\S$/.test(m)) ? '' + m + '' : wm;
+ });
+ text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
+ return (/\S$/.test(m)) ? '' + m + '' : wm;
+ });
+ }
+
+ // escape remaining underscores to prevent them being parsed by italic and bold
+ text = text.replace(/(_)/g, showdown.helper.escapeCharactersCallback);
+
+ text = globals.converter._dispatch('underline.after', text, options, globals);
+
+ return text;
+ });
+
+ /**
+ * Swap back in all the special characters we've hidden.
+ */
+ showdown.subParser('unescapeSpecialChars', function (text, options, globals) {
+ 'use strict';
+ text = globals.converter._dispatch('unescapeSpecialChars.before', text, options, globals);
+
+ text = text.replace(/¨E(\d+)E/g, function (wholeMatch, m1) {
+ var charCodeToReplace = parseInt(m1);
+ return String.fromCharCode(charCodeToReplace);
+ });
+
+ text = globals.converter._dispatch('unescapeSpecialChars.after', text, options, globals);
+ return text;
+ });
+
+ showdown.subParser('makeMarkdown.blockquote', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes()) {
+ var children = node.childNodes,
+ childrenLength = children.length;
+
+ for (var i = 0; i < childrenLength; ++i) {
+ var innerTxt = showdown.subParser('makeMarkdown.node')(children[i], globals);
+
+ if (innerTxt === '') {
+ continue;
+ }
+ txt += innerTxt;
+ }
+ }
+ // cleanup
+ txt = txt.trim();
+ txt = '> ' + txt.split('\n').join('\n> ');
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.codeBlock', function (node, globals) {
+ 'use strict';
+
+ var lang = node.getAttribute('language'),
+ num = node.getAttribute('precodenum');
+ return '```' + lang + '\n' + globals.preList[num] + '\n```';
+ });
+
+ showdown.subParser('makeMarkdown.codeSpan', function (node) {
+ 'use strict';
+
+ return '`' + node.innerHTML + '`';
+ });
+
+ showdown.subParser('makeMarkdown.emphasis', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes()) {
+ txt += '*';
+ var children = node.childNodes,
+ childrenLength = children.length;
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ txt += '*';
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.header', function (node, globals, headerLevel) {
+ 'use strict';
+
+ var headerMark = new Array(headerLevel + 1).join('#'),
+ txt = '';
+
+ if (node.hasChildNodes()) {
+ txt = headerMark + ' ';
+ var children = node.childNodes,
+ childrenLength = children.length;
+
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.hr', function () {
+ 'use strict';
+
+ return '---';
+ });
+
+ showdown.subParser('makeMarkdown.image', function (node) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasAttribute('src')) {
+ txt += ' + '>';
+ if (node.hasAttribute('width') && node.hasAttribute('height')) {
+ txt += ' =' + node.getAttribute('width') + 'x' + node.getAttribute('height');
+ }
+
+ if (node.hasAttribute('title')) {
+ txt += ' "' + node.getAttribute('title') + '"';
+ }
+ txt += ')';
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.links', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes() && node.hasAttribute('href')) {
+ var children = node.childNodes,
+ childrenLength = children.length;
+ txt = '[';
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ txt += '](';
+ txt += '<' + node.getAttribute('href') + '>';
+ if (node.hasAttribute('title')) {
+ txt += ' "' + node.getAttribute('title') + '"';
+ }
+ txt += ')';
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.list', function (node, globals, type) {
+ 'use strict';
+
+ var txt = '';
+ if (!node.hasChildNodes()) {
+ return '';
+ }
+ var listItems = node.childNodes,
+ listItemsLenght = listItems.length,
+ listNum = node.getAttribute('start') || 1;
+
+ for (var i = 0; i < listItemsLenght; ++i) {
+ if (typeof listItems[i].tagName === 'undefined' || listItems[i].tagName.toLowerCase() !== 'li') {
+ continue;
+ }
+
+ // define the bullet to use in list
+ var bullet = '';
+ if (type === 'ol') {
+ bullet = listNum.toString() + '. ';
+ } else {
+ bullet = '- ';
+ }
+
+ // parse list item
+ txt += bullet + showdown.subParser('makeMarkdown.listItem')(listItems[i], globals);
+ ++listNum;
+ }
+
+ // add comment at the end to prevent consecutive lists to be parsed as one
+ txt += '\n\n';
+ return txt.trim();
+ });
+
+ showdown.subParser('makeMarkdown.listItem', function (node, globals) {
+ 'use strict';
+
+ var listItemTxt = '';
+
+ var children = node.childNodes,
+ childrenLenght = children.length;
+
+ for (var i = 0; i < childrenLenght; ++i) {
+ listItemTxt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ // if it's only one liner, we need to add a newline at the end
+ if (!/\n$/.test(listItemTxt)) {
+ listItemTxt += '\n';
+ } else {
+ // it's multiparagraph, so we need to indent
+ listItemTxt = listItemTxt
+ .split('\n')
+ .join('\n ')
+ .replace(/^ {4}$/gm, '')
+ .replace(/\n\n+/g, '\n\n');
+ }
+
+ return listItemTxt;
+ });
+
+
+
+ showdown.subParser('makeMarkdown.node', function (node, globals, spansOnly) {
+ 'use strict';
+
+ spansOnly = spansOnly || false;
+
+ var txt = '';
+
+ // edge case of text without wrapper paragraph
+ if (node.nodeType === 3) {
+ return showdown.subParser('makeMarkdown.txt')(node, globals);
+ }
+
+ // HTML comment
+ if (node.nodeType === 8) {
+ return '\n\n';
+ }
+
+ // process only node elements
+ if (node.nodeType !== 1) {
+ return '';
+ }
+
+ var tagName = node.tagName.toLowerCase();
+
+ switch (tagName) {
+
+ //
+ // BLOCKS
+ //
+ case 'h1':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 1) + '\n\n'; }
+ break;
+ case 'h2':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 2) + '\n\n'; }
+ break;
+ case 'h3':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 3) + '\n\n'; }
+ break;
+ case 'h4':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 4) + '\n\n'; }
+ break;
+ case 'h5':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 5) + '\n\n'; }
+ break;
+ case 'h6':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 6) + '\n\n'; }
+ break;
+
+ case 'p':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.paragraph')(node, globals) + '\n\n'; }
+ break;
+
+ case 'blockquote':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.blockquote')(node, globals) + '\n\n'; }
+ break;
+
+ case 'hr':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.hr')(node, globals) + '\n\n'; }
+ break;
+
+ case 'ol':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, globals, 'ol') + '\n\n'; }
+ break;
+
+ case 'ul':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, globals, 'ul') + '\n\n'; }
+ break;
+
+ case 'precode':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.codeBlock')(node, globals) + '\n\n'; }
+ break;
+
+ case 'pre':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.pre')(node, globals) + '\n\n'; }
+ break;
+
+ case 'table':
+ if (!spansOnly) { txt = showdown.subParser('makeMarkdown.table')(node, globals) + '\n\n'; }
+ break;
+
+ //
+ // SPANS
+ //
+ case 'code':
+ txt = showdown.subParser('makeMarkdown.codeSpan')(node, globals);
+ break;
+
+ case 'em':
+ case 'i':
+ txt = showdown.subParser('makeMarkdown.emphasis')(node, globals);
+ break;
+
+ case 'strong':
+ case 'b':
+ txt = showdown.subParser('makeMarkdown.strong')(node, globals);
+ break;
+
+ case 'del':
+ txt = showdown.subParser('makeMarkdown.strikethrough')(node, globals);
+ break;
+
+ case 'a':
+ txt = showdown.subParser('makeMarkdown.links')(node, globals);
+ break;
+
+ case 'img':
+ txt = showdown.subParser('makeMarkdown.image')(node, globals);
+ break;
+
+ default:
+ txt = node.outerHTML + '\n\n';
+ }
+
+ // common normalization
+ // TODO eventually
+
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.paragraph', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes()) {
+ var children = node.childNodes,
+ childrenLength = children.length;
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ }
+
+ // some text normalization
+ txt = txt.trim();
+
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.pre', function (node, globals) {
+ 'use strict';
+
+ var num = node.getAttribute('prenum');
+ return '' + globals.preList[num] + '
';
+ });
+
+ showdown.subParser('makeMarkdown.strikethrough', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes()) {
+ txt += '~~';
+ var children = node.childNodes,
+ childrenLength = children.length;
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ txt += '~~';
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.strong', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (node.hasChildNodes()) {
+ txt += '**';
+ var children = node.childNodes,
+ childrenLength = children.length;
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals);
+ }
+ txt += '**';
+ }
+ return txt;
+ });
+
+ showdown.subParser('makeMarkdown.table', function (node, globals) {
+ 'use strict';
+
+ var txt = '',
+ tableArray = [[], []],
+ headings = node.querySelectorAll('thead>tr>th'),
+ rows = node.querySelectorAll('tbody>tr'),
+ i, ii;
+ for (i = 0; i < headings.length; ++i) {
+ var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals),
+ allign = '---';
+
+ if (headings[i].hasAttribute('style')) {
+ var style = headings[i].getAttribute('style').toLowerCase().replace(/\s/g, '');
+ switch (style) {
+ case 'text-align:left;':
+ allign = ':---';
+ break;
+ case 'text-align:right;':
+ allign = '---:';
+ break;
+ case 'text-align:center;':
+ allign = ':---:';
+ break;
+ }
+ }
+ tableArray[0][i] = headContent.trim();
+ tableArray[1][i] = allign;
+ }
+
+ for (i = 0; i < rows.length; ++i) {
+ var r = tableArray.push([]) - 1,
+ cols = rows[i].getElementsByTagName('td');
+
+ for (ii = 0; ii < headings.length; ++ii) {
+ var cellContent = ' ';
+ if (typeof cols[ii] !== 'undefined') {
+ cellContent = showdown.subParser('makeMarkdown.tableCell')(cols[ii], globals);
+ }
+ tableArray[r].push(cellContent);
+ }
+ }
+
+ var cellSpacesCount = 3;
+ for (i = 0; i < tableArray.length; ++i) {
+ for (ii = 0; ii < tableArray[i].length; ++ii) {
+ var strLen = tableArray[i][ii].length;
+ if (strLen > cellSpacesCount) {
+ cellSpacesCount = strLen;
+ }
+ }
+ }
+
+ for (i = 0; i < tableArray.length; ++i) {
+ for (ii = 0; ii < tableArray[i].length; ++ii) {
+ if (i === 1) {
+ if (tableArray[i][ii].slice(-1) === ':') {
+ tableArray[i][ii] = showdown.helper.padEnd(tableArray[i][ii].slice(-1), cellSpacesCount - 1, '-') + ':';
+ } else {
+ tableArray[i][ii] = showdown.helper.padEnd(tableArray[i][ii], cellSpacesCount, '-');
+ }
+ } else {
+ tableArray[i][ii] = showdown.helper.padEnd(tableArray[i][ii], cellSpacesCount);
+ }
+ }
+ txt += '| ' + tableArray[i].join(' | ') + ' |\n';
+ }
+
+ return txt.trim();
+ });
+
+ showdown.subParser('makeMarkdown.tableCell', function (node, globals) {
+ 'use strict';
+
+ var txt = '';
+ if (!node.hasChildNodes()) {
+ return '';
+ }
+ var children = node.childNodes,
+ childrenLength = children.length;
+
+ for (var i = 0; i < childrenLength; ++i) {
+ txt += showdown.subParser('makeMarkdown.node')(children[i], globals, true);
+ }
+ return txt.trim();
+ });
+
+ showdown.subParser('makeMarkdown.txt', function (node) {
+ 'use strict';
+
+ var txt = node.nodeValue;
+
+ // multiple spaces are collapsed
+ txt = txt.replace(/ +/g, ' ');
+
+ // replace the custom ¨NBSP; with a space
+ txt = txt.replace(/¨NBSP;/g, ' ');
+
+ // ", <, > and & should replace escaped html entities
+ txt = showdown.helper.unescapeHTMLEntities(txt);
+
+ // escape markdown magic characters
+ // emphasis, strong and strikethrough - can appear everywhere
+ // we also escape pipe (|) because of tables
+ // and escape ` because of code blocks and spans
+ txt = txt.replace(/([*_~|`])/g, '\\$1');
+
+ // escape > because of blockquotes
+ txt = txt.replace(/^(\s*)>/g, '\\$1>');
+
+ // hash character, only troublesome at the beginning of a line because of headers
+ txt = txt.replace(/^#/gm, '\\#');
+
+ // horizontal rules
+ txt = txt.replace(/^(\s*)([-=]{3,})(\s*)$/, '$1\\$2$3');
+
+ // dot, because of ordered lists, only troublesome at the beginning of a line when preceded by an integer
+ txt = txt.replace(/^( {0,3}\d+)\./gm, '$1\\.');
+
+ // +, * and -, at the beginning of a line becomes a list, so we need to escape them also (asterisk was already escaped)
+ txt = txt.replace(/^( {0,3})([+-])/gm, '$1\\$2');
+
+ // images and links, ] followed by ( is problematic, so we escape it
+ txt = txt.replace(/]([\s]*)\(/g, '\\]$1\\(');
+
+ // reference URIs must also be escaped
+ txt = txt.replace(/^ {0,3}\[([\S \t]*?)]:/gm, '\\[$1]:');
+
+ return txt;
+ });
+
+ export var Markdown = showdown;
\ No newline at end of file
diff --git a/Utils/Parser/license.txt b/Utils/Parser/license.txt
new file mode 100644
index 0000000..2e8af8a
--- /dev/null
+++ b/Utils/Parser/license.txt
@@ -0,0 +1,34 @@
+Showdown Copyright (c) 2007, John Fraser
+
+All rights reserved.
+
+Original Markdown copyright (c) 2004, John Gruber
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+* Neither the name "Markdown" nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+This software is provided by the copyright holders and contributors "as
+is" and any express or implied warranties, including, but not limited
+to, the implied warranties of merchantability and fitness for a
+particular purpose are disclaimed. In no event shall the copyright owner
+or contributors be liable for any direct, indirect, incidental, special,
+exemplary, or consequential damages (including, but not limited to,
+procurement of substitute goods or services; loss of use, data, or
+profits; or business interruption) however caused and on any theory of
+liability, whether in contract, strict liability, or tort (including
+negligence or otherwise) arising in any way out of the use of this
+software, even if advised of the possibility of such damage.