12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- /**
- * Created by Estevao on 31-05-2015.
- */
- /**
- * Showdown Converter class
- * @class
- * @param {object} [converterOptions]
- * @returns {Converter}
- */
- showdown.Converter = function (converterOptions) {
- 'use strict';
- var
- /**
- * Options used by this converter
- * @private
- * @type {{}}
- */
- options = {},
- /**
- * Language extensions used by this converter
- * @private
- * @type {Array}
- */
- langExtensions = [],
- /**
- * Output modifiers extensions used by this converter
- * @private
- * @type {Array}
- */
- outputModifiers = [],
- /**
- * Event listeners
- * @private
- * @type {{}}
- */
- listeners = {},
- /**
- * The flavor set in this converter
- */
- setConvFlavor = setFlavor,
- /**
- * Metadata of the document
- * @type {{parsed: {}, raw: string, format: string}}
- */
- metadata = {
- parsed: {},
- raw: '',
- format: ''
- };
- _constructor();
- /**
- * Converter constructor
- * @private
- */
- function _constructor () {
- converterOptions = converterOptions || {};
- for (var gOpt in globalOptions) {
- if (globalOptions.hasOwnProperty(gOpt)) {
- options[gOpt] = globalOptions[gOpt];
- }
- }
- // Merge options
- if (typeof converterOptions === 'object') {
- for (var opt in converterOptions) {
- if (converterOptions.hasOwnProperty(opt)) {
- options[opt] = converterOptions[opt];
- }
- }
- } else {
- throw Error('Converter expects the passed parameter to be an object, but ' + typeof converterOptions +
- ' was passed instead.');
- }
- if (options.extensions) {
- showdown.helper.forEach(options.extensions, _parseExtension);
- }
- }
- /**
- * Parse extension
- * @param {*} ext
- * @param {string} [name='']
- * @private
- */
- function _parseExtension (ext, name) {
- name = name || null;
- // If it's a string, the extension was previously loaded
- if (showdown.helper.isString(ext)) {
- ext = showdown.helper.stdExtName(ext);
- name = ext;
- // LEGACY_SUPPORT CODE
- if (showdown.extensions[ext]) {
- console.warn('DEPRECATION WARNING: ' + ext + ' is an old extension that uses a deprecated loading method.' +
- 'Please inform the developer that the extension should be updated!');
- legacyExtensionLoading(showdown.extensions[ext], ext);
- return;
- // END LEGACY SUPPORT CODE
- } else if (!showdown.helper.isUndefined(extensions[ext])) {
- ext = extensions[ext];
- } else {
- throw Error('Extension "' + ext + '" could not be loaded. It was either not found or is not a valid extension.');
- }
- }
- if (typeof ext === 'function') {
- ext = ext();
- }
- if (!showdown.helper.isArray(ext)) {
- ext = [ext];
- }
- var validExt = validate(ext, name);
- if (!validExt.valid) {
- throw Error(validExt.error);
- }
- for (var i = 0; i < ext.length; ++i) {
- switch (ext[i].type) {
- case 'lang':
- langExtensions.push(ext[i]);
- break;
- case 'output':
- outputModifiers.push(ext[i]);
- break;
- }
- if (ext[i].hasOwnProperty('listeners')) {
- for (var ln in ext[i].listeners) {
- if (ext[i].listeners.hasOwnProperty(ln)) {
- listen(ln, ext[i].listeners[ln]);
- }
- }
- }
- }
- }
- /**
- * LEGACY_SUPPORT
- * @param {*} ext
- * @param {string} name
- */
- function legacyExtensionLoading (ext, name) {
- if (typeof ext === 'function') {
- ext = ext(new showdown.Converter());
- }
- if (!showdown.helper.isArray(ext)) {
- ext = [ext];
- }
- var valid = validate(ext, name);
- if (!valid.valid) {
- throw Error(valid.error);
- }
- for (var i = 0; i < ext.length; ++i) {
- switch (ext[i].type) {
- case 'lang':
- langExtensions.push(ext[i]);
- break;
- case 'output':
- outputModifiers.push(ext[i]);
- break;
- default:// should never reach here
- throw Error('Extension loader error: Type unrecognized!!!');
- }
- }
- }
- /**
- * Listen to an event
- * @param {string} name
- * @param {function} callback
- */
- function listen (name, callback) {
- if (!showdown.helper.isString(name)) {
- throw Error('Invalid argument in converter.listen() method: name must be a string, but ' + typeof name + ' given');
- }
- if (typeof callback !== 'function') {
- throw Error('Invalid argument in converter.listen() method: callback must be a function, but ' + typeof callback + ' given');
- }
- if (!listeners.hasOwnProperty(name)) {
- listeners[name] = [];
- }
- listeners[name].push(callback);
- }
- function rTrimInputText (text) {
- var rsp = text.match(/^\s*/)[0].length,
- rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm');
- return text.replace(rgx, '');
- }
- /**
- * Dispatch an event
- * @private
- * @param {string} evtName Event name
- * @param {string} text Text
- * @param {{}} options Converter Options
- * @param {{}} globals
- * @returns {string}
- */
- this._dispatch = function dispatch (evtName, text, options, globals) {
- if (listeners.hasOwnProperty(evtName)) {
- for (var ei = 0; ei < listeners[evtName].length; ++ei) {
- var nText = listeners[evtName][ei](evtName, text, this, options, globals);
- if (nText && typeof nText !== 'undefined') {
- text = nText;
- }
- }
- }
- return text;
- };
- /**
- * Listen to an event
- * @param {string} name
- * @param {function} callback
- * @returns {showdown.Converter}
- */
- this.listen = function (name, callback) {
- listen(name, callback);
- return this;
- };
- /**
- * Converts a markdown string into HTML string
- * @param {string} text
- * @returns {*}
- */
- this.makeHtml = function (text) {
- //check if text is not falsy
- if (!text) {
- return text;
- }
- var globals = {
- gHtmlBlocks: [],
- gHtmlMdBlocks: [],
- gHtmlSpans: [],
- gUrls: {},
- gTitles: {},
- gDimensions: {},
- gListLevel: 0,
- hashLinkCounts: {},
- langExtensions: langExtensions,
- outputModifiers: outputModifiers,
- converter: this,
- ghCodeBlocks: [],
- metadata: {
- parsed: {},
- raw: '',
- format: ''
- }
- };
- // This lets us use ¨ trema as an escape char to avoid md5 hashes
- // The choice of character is arbitrary; anything that isn't
- // magic in Markdown will work.
- text = text.replace(/¨/g, '¨T');
- // Replace $ with ¨D
- // RegExp interprets $ as a special character
- // when it's in a replacement string
- text = text.replace(/\$/g, '¨D');
- // Standardize line endings
- text = text.replace(/\r\n/g, '\n'); // DOS to Unix
- text = text.replace(/\r/g, '\n'); // Mac to Unix
- // Stardardize line spaces (nbsp causes trouble in older browsers and some regex flavors)
- text = text.replace(/\u00A0/g, ' ');
- if (options.smartIndentationFix) {
- text = rTrimInputText(text);
- }
- // Make sure text begins and ends with a couple of newlines:
- text = '\n\n' + text + '\n\n';
- // detab
- text = showdown.subParser('makehtml.detab')(text, options, globals);
- /**
- * Strip any lines consisting only of spaces and tabs.
- * This makes subsequent regexs easier to write, because we can
- * match consecutive blank lines with /\n+/ instead of something
- * contorted like /[ \t]*\n+/
- */
- text = text.replace(/^[ \t]+$/mg, '');
- //run languageExtensions
- showdown.helper.forEach(langExtensions, function (ext) {
- text = showdown.subParser('makehtml.runExtension')(ext, text, options, globals);
- });
- // 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.hashHTMLBlocks')(text, options, globals);
- text = showdown.subParser('makehtml.hashCodeTags')(text, options, globals);
- text = showdown.subParser('makehtml.stripLinkDefinitions')(text, options, globals);
- text = showdown.subParser('makehtml.blockGamut')(text, options, globals);
- text = showdown.subParser('makehtml.unhashHTMLSpans')(text, options, globals);
- text = showdown.subParser('makehtml.unescapeSpecialChars')(text, options, globals);
- // attacklab: Restore dollar signs
- text = text.replace(/¨D/g, '$$');
- // attacklab: Restore tremas
- text = text.replace(/¨T/g, '¨');
- // render a complete html document instead of a partial if the option is enabled
- text = showdown.subParser('makehtml.completeHTMLDocument')(text, options, globals);
- // Run output modifiers
- showdown.helper.forEach(outputModifiers, function (ext) {
- text = showdown.subParser('makehtml.runExtension')(ext, text, options, globals);
- });
- // update metadata
- metadata = globals.metadata;
- return text;
- };
- /**
- * Converts an HTML string into a markdown string
- * @param src
- * @returns {string}
- */
- this.makeMarkdown = function (src) {
- // replace \r\n with \n
- src = src.replace(/\r\n/g, '\n');
- src = src.replace(/\r/g, '\n'); // old macs
- // due to an edge case, we need to find this: > <
- // to prevent removing of non silent white spaces
- // ex: <em>this is</em> <strong>sparta</strong>
- src = src.replace(/>[ \t]+</, '>¨NBSP;<');
- var doc = showdown.helper.document.createElement('div');
- doc.innerHTML = src;
- var preList = substitutePreCodeTags(doc);
- // remove all newlines and collapse spaces
- clean(doc);
- // some stuff, like accidental reference links must now be escaped
- // TODO
- // doc.innerHTML = doc.innerHTML.replace(/\[[\S\t ]]/);
- var nodes = doc.childNodes,
- mdDoc = '';
- for (var i = 0; i < nodes.length; i++) {
- mdDoc += parseNode(nodes[i]);
- }
- function parseNode (node, spansOnly) {
- spansOnly = spansOnly || false;
- var txt = '';
- // edge case of text without wrapper paragraph
- if (node.nodeType === 3) {
- return parseTxt(node);
- }
- // HTML comment
- if (node.nodeType === 8) {
- return '<!--' + node.data + '-->\n\n';
- }
- // process only node elements
- if (node.nodeType !== 1) {
- return '';
- }
- var tagName = node.tagName.toLowerCase();
- switch (tagName) {
- //
- // BLOCKS
- //
- case 'h1':
- if (!spansOnly) { txt = parseHeader(node, 1) + '\n\n'; }
- break;
- case 'h2':
- if (!spansOnly) { txt = parseHeader(node, 2) + '\n\n'; }
- break;
- case 'h3':
- if (!spansOnly) { txt = parseHeader(node, 3) + '\n\n'; }
- break;
- case 'h4':
- if (!spansOnly) { txt = parseHeader(node, 4) + '\n\n'; }
- break;
- case 'h5':
- if (!spansOnly) { txt = parseHeader(node, 5) + '\n\n'; }
- break;
- case 'h6':
- if (!spansOnly) { txt = parseHeader(node, 6) + '\n\n'; }
- break;
- case 'p':
- if (!spansOnly) { txt = parseParagraph(node) + '\n\n'; }
- break;
- case 'blockquote':
- if (!spansOnly) { txt = parseBlockquote(node) + '\n\n'; }
- break;
- case 'hr':
- if (!spansOnly) { txt = parseHr(node) + '\n\n'; }
- break;
- case 'ol':
- if (!spansOnly) { txt = parseList(node, 'ol') + '\n\n'; }
- break;
- case 'ul':
- if (!spansOnly) { txt = parseList(node, 'ul') + '\n\n'; }
- break;
- case 'precode':
- if (!spansOnly) { txt = parsePreCode(node) + '\n\n'; }
- break;
- case 'pre':
- if (!spansOnly) { txt = parsePre(node) + '\n\n'; }
- break;
- case 'table':
- if (!spansOnly) { txt = parseTable(node) + '\n\n'; }
- break;
- //
- // SPANS
- //
- case 'code':
- txt = parseCodeSpan(node);
- break;
- case 'em':
- case 'i':
- txt = parseEmphasis(node);
- break;
- case 'strong':
- case 'b':
- txt = parseStrong(node);
- break;
- case 'del':
- txt = parseDel(node);
- break;
- case 'a':
- txt = parseLinks(node);
- break;
- case 'img':
- txt = parseImage(node);
- break;
- default:
- txt = node.outerHTML + '\n\n';
- }
- // common normalization
- return txt;
- }
- function parseTxt (node) {
- 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;
- }
- function parseList (node, type) {
- 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 + parseListItem(listItems[i]);
- ++listNum;
- }
- return txt.trim();
- }
- function parseListItem (node) {
- var listItemTxt = '';
- var children = node.childNodes,
- childrenLenght = children.length;
- for (var i = 0; i < childrenLenght; ++i) {
- listItemTxt += parseNode(children[i]);
- }
- // 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;
- }
- function parseHr () {
- return '---';
- }
- function parseBlockquote (node) {
- var txt = '';
- if (node.hasChildNodes()) {
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- var innerTxt = parseNode(children[i]);
- if (innerTxt === '') {
- continue;
- }
- txt += innerTxt;
- }
- }
- // cleanup
- txt = txt.trim();
- txt = '> ' + txt.split('\n').join('\n> ');
- return txt;
- }
- function parseCodeSpan (node) {
- return '`' + node.innerHTML + '`';
- }
- function parseStrong (node) {
- var txt = '';
- if (node.hasChildNodes()) {
- txt += '**';
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i]);
- }
- txt += '**';
- }
- return txt;
- }
- function parseEmphasis (node) {
- var txt = '';
- if (node.hasChildNodes()) {
- txt += '*';
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i]);
- }
- txt += '*';
- }
- return txt;
- }
- function parseDel (node) {
- var txt = '';
- if (node.hasChildNodes()) {
- txt += '~~';
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i]);
- }
- txt += '~~';
- }
- return txt;
- }
- function parseLinks (node) {
- var txt = '';
- if (node.hasChildNodes() && node.hasAttribute('href')) {
- var children = node.childNodes,
- childrenLength = children.length;
- txt = '[';
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i]);
- }
- txt += '](';
- txt += '<' + node.getAttribute('href') + '>';
- if (node.hasAttribute('title')) {
- txt += ' "' + node.getAttribute('title') + '"';
- }
- txt += ')';
- }
- return txt;
- }
- function parseImage (node) {
- 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;
- }
- function parseHeader (node, headerLevel) {
- 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 += parseNode(children[i]);
- }
- }
- return txt;
- }
- function parseParagraph (node) {
- var txt = '';
- if (node.hasChildNodes()) {
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i]);
- }
- }
- // some text normalization
- txt = txt.trim();
- return txt;
- }
- function parsePreCode (node) {
- var lang = node.getAttribute('language'),
- num = node.getAttribute('precodenum');
- return '```' + lang + '\n' + preList[num] + '\n```';
- }
- function parsePre (node) {
- var num = node.getAttribute('prenum');
- return '<pre>' + preList[num] + '</pre>';
- }
- function parseTable (node) {
- 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 = parseTableCell(headings[i]),
- 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 = parseTableCell(cols[ii]);
- }
- 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();
- }
- function parseTableCell (node) {
- var txt = '';
- if (!node.hasChildNodes()) {
- return '';
- }
- var children = node.childNodes,
- childrenLength = children.length;
- for (var i = 0; i < childrenLength; ++i) {
- txt += parseNode(children[i], true);
- }
- return txt.trim();
- }
- function clean (node) {
- for (var n = 0; n < node.childNodes.length; ++n) {
- var child = node.childNodes[n];
- if (child.nodeType === 3) {
- if (!/\S/.test(child.nodeValue)) {
- node.removeChild(child);
- --n;
- } else {
- child.nodeValue = child.nodeValue.split('\n').join(' ');
- child.nodeValue = child.nodeValue.replace(/(\s)+/g, '$1');
- }
- } else if (child.nodeType === 1) {
- clean(child);
- }
- }
- }
- // find all pre tags and replace contents with placeholder
- // we need this so that we can remove all indentation from html
- // to ease up parsing
- function substitutePreCodeTags (doc) {
- var pres = doc.querySelectorAll('pre'),
- presPH = [];
- for (var i = 0; i < pres.length; ++i) {
- if (pres[i].childElementCount === 1 && pres[i].firstChild.tagName.toLowerCase() === 'code') {
- var content = pres[i].firstChild.innerHTML.trim(),
- language = pres[i].firstChild.getAttribute('data-language') || '';
- // if data-language attribute is not defined, then we look for class language-*
- if (language === '') {
- var classes = pres[i].firstChild.className.split(' ');
- for (var c = 0; c < classes.length; ++c) {
- var matches = classes[c].match(/^language-(.+)$/);
- if (matches !== null) {
- language = matches[1];
- break;
- }
- }
- }
- // unescape html entities in content
- content = showdown.helper.unescapeHTMLEntities(content);
- presPH.push(content);
- pres[i].outerHTML = '<precode language="' + language + '" precodenum="' + i.toString() + '"></precode>';
- } else {
- presPH.push(pres[i].innerHTML);
- pres[i].innerHTML = '';
- pres[i].setAttribute('prenum', i.toString());
- }
- }
- return presPH;
- }
- return mdDoc;
- };
- /**
- * Set an option of this Converter instance
- * @param {string} key
- * @param {*} value
- */
- this.setOption = function (key, value) {
- options[key] = value;
- };
- /**
- * Get the option of this Converter instance
- * @param {string} key
- * @returns {*}
- */
- this.getOption = function (key) {
- return options[key];
- };
- /**
- * Get the options of this Converter instance
- * @returns {{}}
- */
- this.getOptions = function () {
- return options;
- };
- /**
- * Add extension to THIS converter
- * @param {{}} extension
- * @param {string} [name=null]
- */
- this.addExtension = function (extension, name) {
- name = name || null;
- _parseExtension(extension, name);
- };
- /**
- * Use a global registered extension with THIS converter
- * @param {string} extensionName Name of the previously registered extension
- */
- this.useExtension = function (extensionName) {
- _parseExtension(extensionName);
- };
- /**
- * Set the flavor THIS converter should use
- * @param {string} name
- */
- this.setFlavor = function (name) {
- if (!flavor.hasOwnProperty(name)) {
- throw Error(name + ' flavor was not found');
- }
- var preset = flavor[name];
- setConvFlavor = name;
- for (var option in preset) {
- if (preset.hasOwnProperty(option)) {
- options[option] = preset[option];
- }
- }
- };
- /**
- * Get the currently set flavor of this converter
- * @returns {string}
- */
- this.getFlavor = function () {
- return setConvFlavor;
- };
- /**
- * Remove an extension from THIS converter.
- * Note: This is a costly operation. It's better to initialize a new converter
- * and specify the extensions you wish to use
- * @param {Array} extension
- */
- this.removeExtension = function (extension) {
- if (!showdown.helper.isArray(extension)) {
- extension = [extension];
- }
- for (var a = 0; a < extension.length; ++a) {
- var ext = extension[a];
- for (var i = 0; i < langExtensions.length; ++i) {
- if (langExtensions[i] === ext) {
- langExtensions[i].splice(i, 1);
- }
- }
- for (var ii = 0; ii < outputModifiers.length; ++i) {
- if (outputModifiers[ii] === ext) {
- outputModifiers[ii].splice(i, 1);
- }
- }
- }
- };
- /**
- * Get all extension of THIS converter
- * @returns {{language: Array, output: Array}}
- */
- this.getAllExtensions = function () {
- return {
- language: langExtensions,
- output: outputModifiers
- };
- };
- /**
- * Get the metadata of the previously parsed document
- * @param raw
- * @returns {string|{}}
- */
- this.getMetadata = function (raw) {
- if (raw) {
- return metadata.raw;
- } else {
- return metadata.parsed;
- }
- };
- /**
- * Get the metadata format of the previously parsed document
- * @returns {string}
- */
- this.getMetadataFormat = function () {
- return metadata.format;
- };
- /**
- * Private: set a single key, value metadata pair
- * @param {string} key
- * @param {string} value
- */
- this._setMetadataPair = function (key, value) {
- metadata.parsed[key] = value;
- };
- /**
- * Private: set metadata format
- * @param {string} format
- */
- this._setMetadataFormat = function (format) {
- metadata.format = format;
- };
- /**
- * Private: set metadata raw text
- * @param {string} raw
- */
- this._setMetadataRaw = function (raw) {
- metadata.raw = raw;
- };
- };
|