showdown.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. ;/*! showdown 11-07-2015 */
  2. (function(){
  3. /**
  4. * Created by Tivie on 06-01-2015.
  5. */
  6. // Private properties
  7. var showdown = {},
  8. parsers = {},
  9. extensions = {},
  10. defaultOptions = {
  11. omitExtraWLInCodeBlocks: false,
  12. prefixHeaderId: false,
  13. noHeaderId: false,
  14. headerLevelStart: 1,
  15. parseImgDimensions: false,
  16. simplifiedAutoLink: false
  17. },
  18. globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
  19. /**
  20. * helper namespace
  21. * @type {{}}
  22. */
  23. showdown.helper = {};
  24. /**
  25. * TODO LEGACY SUPPORT CODE
  26. * @type {{}}
  27. */
  28. showdown.extensions = {};
  29. /**
  30. * Set a global option
  31. * @static
  32. * @param {string} key
  33. * @param {*} value
  34. * @returns {showdown}
  35. */
  36. showdown.setOption = function (key, value) {
  37. 'use strict';
  38. globalOptions[key] = value;
  39. return this;
  40. };
  41. /**
  42. * Get a global option
  43. * @static
  44. * @param {string} key
  45. * @returns {*}
  46. */
  47. showdown.getOption = function (key) {
  48. 'use strict';
  49. return globalOptions[key];
  50. };
  51. /**
  52. * Get the global options
  53. * @static
  54. * @returns {{}}
  55. */
  56. showdown.getOptions = function () {
  57. 'use strict';
  58. return globalOptions;
  59. };
  60. /**
  61. * Reset global options to the default values
  62. * @static
  63. */
  64. showdown.resetOptions = function () {
  65. 'use strict';
  66. globalOptions = JSON.parse(JSON.stringify(defaultOptions));
  67. };
  68. /**
  69. * Get the default options
  70. * @static
  71. * @returns {{}}
  72. */
  73. showdown.getDefaultOptions = function () {
  74. 'use strict';
  75. return defaultOptions;
  76. };
  77. /**
  78. * Get or set a subParser
  79. *
  80. * subParser(name) - Get a registered subParser
  81. * subParser(name, func) - Register a subParser
  82. * @static
  83. * @param {string} name
  84. * @param {function} [func]
  85. * @returns {*}
  86. */
  87. showdown.subParser = function (name, func) {
  88. 'use strict';
  89. if (showdown.helper.isString(name)) {
  90. if (typeof func !== 'undefined') {
  91. parsers[name] = func;
  92. } else {
  93. if (parsers.hasOwnProperty(name)) {
  94. return parsers[name];
  95. } else {
  96. throw Error('SubParser named ' + name + ' not registered!');
  97. }
  98. }
  99. }
  100. };
  101. /**
  102. * Gets or registers an extension
  103. * @static
  104. * @param {string} name
  105. * @param {object|function=} ext
  106. * @returns {*}
  107. */
  108. showdown.extension = function (name, ext) {
  109. 'use strict';
  110. if (!showdown.helper.isString(name)) {
  111. throw Error('Extension \'name\' must be a string');
  112. }
  113. name = showdown.helper.stdExtName(name);
  114. // Getter
  115. if (showdown.helper.isUndefined(ext)) {
  116. if (!extensions.hasOwnProperty(name)) {
  117. throw Error('Extension named ' + name + ' is not registered!');
  118. }
  119. return extensions[name];
  120. // Setter
  121. } else {
  122. // Expand extension if it's wrapped in a function
  123. if (typeof ext === 'function') {
  124. ext = ext();
  125. }
  126. // Ensure extension is an array
  127. if (!showdown.helper.isArray(ext)) {
  128. ext = [ext];
  129. }
  130. var validExtension = validate(ext, name);
  131. if (validExtension.valid) {
  132. extensions[name] = ext;
  133. } else {
  134. throw Error(validExtension.error);
  135. }
  136. }
  137. };
  138. /**
  139. * Gets all extensions registered
  140. * @returns {{}}
  141. */
  142. showdown.getAllExtensions = function () {
  143. 'use strict';
  144. return extensions;
  145. };
  146. /**
  147. * Remove an extension
  148. * @param {string} name
  149. */
  150. showdown.removeExtension = function (name) {
  151. 'use strict';
  152. delete extensions[name];
  153. };
  154. /**
  155. * Removes all extensions
  156. */
  157. showdown.resetExtensions = function () {
  158. 'use strict';
  159. extensions = {};
  160. };
  161. /**
  162. * Validate extension
  163. * @param {array} extension
  164. * @param {string} name
  165. * @returns {{valid: boolean, error: string}}
  166. */
  167. function validate(extension, name) {
  168. 'use strict';
  169. var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension',
  170. ret = {
  171. valid: true,
  172. error: ''
  173. };
  174. if (!showdown.helper.isArray(extension)) {
  175. extension = [extension];
  176. }
  177. for (var i = 0; i < extension.length; ++i) {
  178. var baseMsg = errMsg + 'sub-extension ' + i + ': ',
  179. ext = extension[i];
  180. if (typeof ext !== 'object') {
  181. ret.valid = false;
  182. ret.error = baseMsg + 'must be an object, but ' + typeof ext + ' given';
  183. return ret;
  184. }
  185. if (!showdown.helper.isString(ext.type)) {
  186. ret.valid = false;
  187. ret.error = baseMsg + 'property "type" must be a string, but ' + typeof ext.type + ' given';
  188. return ret;
  189. }
  190. var type = ext.type = ext.type.toLowerCase();
  191. // normalize extension type
  192. if (type === 'language') {
  193. type = ext.type = 'lang';
  194. }
  195. if (type === 'html') {
  196. type = ext.type = 'output';
  197. }
  198. if (type !== 'lang' && type !== 'output') {
  199. ret.valid = false;
  200. ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang" or "output"';
  201. return ret;
  202. }
  203. if (ext.filter) {
  204. if (typeof ext.filter !== 'function') {
  205. ret.valid = false;
  206. ret.error = baseMsg + '"filter" must be a function, but ' + typeof ext.filter + ' given';
  207. return ret;
  208. }
  209. } else if (ext.regex) {
  210. if (showdown.helper.isString(ext.regex)) {
  211. ext.regex = new RegExp(ext.regex, 'g');
  212. }
  213. if (!ext.regex instanceof RegExp) {
  214. ret.valid = false;
  215. ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' +
  216. typeof ext.regex + ' given';
  217. return ret;
  218. }
  219. if (showdown.helper.isUndefined(ext.replace)) {
  220. ret.valid = false;
  221. ret.error = baseMsg + '"regex" extensions must implement a replace string or function';
  222. return ret;
  223. }
  224. } else {
  225. ret.valid = false;
  226. ret.error = baseMsg + 'extensions must define either a "regex" property or a "filter" method';
  227. return ret;
  228. }
  229. if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) {
  230. ret.valid = false;
  231. ret.error = baseMsg + 'output extensions must define a filter property';
  232. return ret;
  233. }
  234. }
  235. return ret;
  236. }
  237. /**
  238. * Validate extension
  239. * @param {object} ext
  240. * @returns {boolean}
  241. */
  242. showdown.validateExtension = function (ext) {
  243. 'use strict';
  244. var validateExtension = validate(ext, null);
  245. if (!validateExtension.valid) {
  246. console.warn(validateExtension.error);
  247. return false;
  248. }
  249. return true;
  250. };
  251. /**
  252. * showdownjs helper functions
  253. */
  254. if (!showdown.hasOwnProperty('helper')) {
  255. showdown.helper = {};
  256. }
  257. /**
  258. * Check if var is string
  259. * @static
  260. * @param {string} a
  261. * @returns {boolean}
  262. */
  263. showdown.helper.isString = function isString(a) {
  264. 'use strict';
  265. return (typeof a === 'string' || a instanceof String);
  266. };
  267. /**
  268. * ForEach helper function
  269. * @static
  270. * @param {*} obj
  271. * @param {function} callback
  272. */
  273. showdown.helper.forEach = function forEach(obj, callback) {
  274. 'use strict';
  275. if (typeof obj.forEach === 'function') {
  276. obj.forEach(callback);
  277. } else {
  278. for (var i = 0; i < obj.length; i++) {
  279. callback(obj[i], i, obj);
  280. }
  281. }
  282. };
  283. /**
  284. * isArray helper function
  285. * @static
  286. * @param {*} a
  287. * @returns {boolean}
  288. */
  289. showdown.helper.isArray = function isArray(a) {
  290. 'use strict';
  291. return a.constructor === Array;
  292. };
  293. /**
  294. * Check if value is undefined
  295. * @static
  296. * @param {*} value The value to check.
  297. * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
  298. */
  299. showdown.helper.isUndefined = function isUndefined(value) {
  300. 'use strict';
  301. return typeof value === 'undefined';
  302. };
  303. /**
  304. * Standardidize extension name
  305. * @static
  306. * @param {string} s extension name
  307. * @returns {string}
  308. */
  309. showdown.helper.stdExtName = function (s) {
  310. 'use strict';
  311. return s.replace(/[_-]||\s/g, '').toLowerCase();
  312. };
  313. function escapeCharactersCallback(wholeMatch, m1) {
  314. 'use strict';
  315. var charCodeToEscape = m1.charCodeAt(0);
  316. return '~E' + charCodeToEscape + 'E';
  317. }
  318. /**
  319. * Callback used to escape characters when passing through String.replace
  320. * @static
  321. * @param {string} wholeMatch
  322. * @param {string} m1
  323. * @returns {string}
  324. */
  325. showdown.helper.escapeCharactersCallback = escapeCharactersCallback;
  326. /**
  327. * Escape characters in a string
  328. * @static
  329. * @param {string} text
  330. * @param {string} charsToEscape
  331. * @param {boolean} afterBackslash
  332. * @returns {XML|string|void|*}
  333. */
  334. showdown.helper.escapeCharacters = function escapeCharacters(text, charsToEscape, afterBackslash) {
  335. 'use strict';
  336. // First we have to escape the escape characters so that
  337. // we can build a character class out of them
  338. var regexString = '([' + charsToEscape.replace(/([\[\]\\])/g, '\\$1') + '])';
  339. if (afterBackslash) {
  340. regexString = '\\\\' + regexString;
  341. }
  342. var regex = new RegExp(regexString, 'g');
  343. text = text.replace(regex, escapeCharactersCallback);
  344. return text;
  345. };
  346. /**
  347. * POLYFILLS
  348. */
  349. if (showdown.helper.isUndefined(console)) {
  350. console = {
  351. warn: function (msg) {
  352. 'use strict';
  353. alert(msg);
  354. },
  355. log: function (msg) {
  356. 'use strict';
  357. alert(msg);
  358. }
  359. };
  360. }
  361. /**
  362. * Created by Estevao on 31-05-2015.
  363. */
  364. /**
  365. * Showdown Converter class
  366. * @class
  367. * @param {object} [converterOptions]
  368. * @returns {
  369. * {makeHtml: Function},
  370. * {setOption: Function},
  371. * {getOption: Function},
  372. * {getOptions: Function}
  373. * }
  374. */
  375. showdown.Converter = function (converterOptions) {
  376. 'use strict';
  377. var
  378. /**
  379. * Options used by this converter
  380. * @private
  381. * @type {{}}
  382. */
  383. options = {
  384. omitExtraWLInCodeBlocks: false,
  385. prefixHeaderId: false,
  386. noHeaderId: false
  387. },
  388. /**
  389. * Language extensions used by this converter
  390. * @private
  391. * @type {Array}
  392. */
  393. langExtensions = [],
  394. /**
  395. * Output modifiers extensions used by this converter
  396. * @private
  397. * @type {Array}
  398. */
  399. outputModifiers = [],
  400. /**
  401. * The parser Order
  402. * @private
  403. * @type {string[]}
  404. */
  405. parserOrder = [
  406. 'githubCodeBlocks',
  407. 'hashHTMLBlocks',
  408. 'stripLinkDefinitions',
  409. 'blockGamut',
  410. 'unescapeSpecialChars'
  411. ];
  412. _constructor();
  413. /**
  414. * Converter constructor
  415. * @private
  416. */
  417. function _constructor() {
  418. converterOptions = converterOptions || {};
  419. for (var gOpt in globalOptions) {
  420. if (globalOptions.hasOwnProperty(gOpt)) {
  421. options[gOpt] = globalOptions[gOpt];
  422. }
  423. }
  424. // Merge options
  425. if (typeof converterOptions === 'object') {
  426. for (var opt in converterOptions) {
  427. if (converterOptions.hasOwnProperty(opt)) {
  428. options[opt] = converterOptions[opt];
  429. }
  430. }
  431. } else {
  432. throw Error('Converter expects the passed parameter to be an object, but ' + typeof converterOptions +
  433. ' was passed instead.');
  434. }
  435. if (options.extensions) {
  436. showdown.helper.forEach(options.extensions, _parseExtension);
  437. }
  438. }
  439. /**
  440. * Parse extension
  441. * @param {*} ext
  442. * @private
  443. */
  444. function _parseExtension(ext) {
  445. // If it's a string, the extension was previously loaded
  446. if (showdown.helper.isString(ext)) {
  447. ext = showdown.helper.stdExtName(ext);
  448. // LEGACY_SUPPORT CODE
  449. if (showdown.extensions[ext]) {
  450. console.warn('DEPRECATION WARNING: ' + ext + ' is an old extension that uses a deprecated loading method.' +
  451. 'Please inform the developer that the extension should be updated!');
  452. legacyExtensionLoading(showdown.extensions[ext], ext);
  453. return;
  454. // END LEGACY SUPPORT CODE
  455. } else if (!showdown.helper.isUndefined(extensions[ext])) {
  456. ext = extensions[ext];
  457. } else {
  458. throw Error('Extension "' + ext + '" could not be loaded. It was either not found or is not a valid extension.');
  459. }
  460. }
  461. if (typeof ext === 'function') {
  462. ext = ext();
  463. }
  464. if (!showdown.helper.isArray(ext)) {
  465. ext = [ext];
  466. }
  467. if (!showdown.validateExtension(ext)) {
  468. return;
  469. }
  470. for (var i = 0; i < ext.length; ++i) {
  471. switch (ext[i].type) {
  472. case 'lang':
  473. langExtensions.push(ext[i]);
  474. break;
  475. case 'output':
  476. outputModifiers.push(ext[i]);
  477. break;
  478. default:
  479. // should never reach here
  480. throw Error('Extension loader error: Type unrecognized!!!');
  481. }
  482. }
  483. }
  484. /**
  485. * LEGACY_SUPPORT
  486. * @param {*} ext
  487. * @param {string} name
  488. */
  489. function legacyExtensionLoading(ext, name) {
  490. if (typeof ext === 'function') {
  491. ext = ext(new showdown.Converter());
  492. }
  493. if (!showdown.helper.isArray(ext)) {
  494. ext = [ext];
  495. }
  496. var valid = validate(ext, name);
  497. if (!valid.valid) {
  498. throw Error(valid.error);
  499. }
  500. for (var i = 0; i < ext.length; ++i) {
  501. switch (ext[i].type) {
  502. case 'lang':
  503. langExtensions.push(ext[i]);
  504. break;
  505. case 'output':
  506. outputModifiers.push(ext[i]);
  507. break;
  508. default:// should never reach here
  509. throw Error('Extension loader error: Type unrecognized!!!');
  510. }
  511. }
  512. }
  513. /**
  514. * Converts a markdown string into HTML
  515. * @param {string} text
  516. * @returns {*}
  517. */
  518. this.makeHtml = function (text) {
  519. //check if text is not falsy
  520. if (!text) {
  521. return text;
  522. }
  523. var globals = {
  524. gHtmlBlocks: [],
  525. gUrls: {},
  526. gTitles: {},
  527. gDimensions: {},
  528. gListLevel: 0,
  529. hashLinkCounts: {},
  530. langExtensions: langExtensions,
  531. outputModifiers: outputModifiers,
  532. converter: this
  533. };
  534. // attacklab: Replace ~ with ~T
  535. // This lets us use tilde as an escape char to avoid md5 hashes
  536. // The choice of character is arbitrary; anything that isn't
  537. // magic in Markdown will work.
  538. text = text.replace(/~/g, '~T');
  539. // attacklab: Replace $ with ~D
  540. // RegExp interprets $ as a special character
  541. // when it's in a replacement string
  542. text = text.replace(/\$/g, '~D');
  543. // Standardize line endings
  544. text = text.replace(/\r\n/g, '\n'); // DOS to Unix
  545. text = text.replace(/\r/g, '\n'); // Mac to Unix
  546. // Make sure text begins and ends with a couple of newlines:
  547. text = '\n\n' + text + '\n\n';
  548. // detab
  549. text = showdown.subParser('detab')(text, options, globals);
  550. // stripBlankLines
  551. text = showdown.subParser('stripBlankLines')(text, options, globals);
  552. //run languageExtensions
  553. showdown.helper.forEach(langExtensions, function (ext) {
  554. text = showdown.subParser('runExtension')(ext, text, options, globals);
  555. });
  556. // Run all registered parsers
  557. for (var i = 0; i < parserOrder.length; ++i) {
  558. var name = parserOrder[i];
  559. text = parsers[name](text, options, globals);
  560. }
  561. // attacklab: Restore dollar signs
  562. text = text.replace(/~D/g, '$$');
  563. // attacklab: Restore tildes
  564. text = text.replace(/~T/g, '~');
  565. // Run output modifiers
  566. showdown.helper.forEach(outputModifiers, function (ext) {
  567. text = showdown.subParser('runExtension')(ext, text, options, globals);
  568. });
  569. return text;
  570. };
  571. /**
  572. * Set an option of this Converter instance
  573. * @param {string} key
  574. * @param {*} value
  575. */
  576. this.setOption = function (key, value) {
  577. options[key] = value;
  578. };
  579. /**
  580. * Get the option of this Converter instance
  581. * @param {string} key
  582. * @returns {*}
  583. */
  584. this.getOption = function (key) {
  585. return options[key];
  586. };
  587. /**
  588. * Get the options of this Converter instance
  589. * @returns {{}}
  590. */
  591. this.getOptions = function () {
  592. return options;
  593. };
  594. /**
  595. * Add extension to THIS converter
  596. * @param {{}} extension
  597. */
  598. this.addExtension = function (extension) {
  599. _parseExtension(extension);
  600. };
  601. /**
  602. * Use a global registered extension with THIS converter
  603. * @param {string} extensionName Name of the previously registered extension
  604. */
  605. this.useExtension = function (extensionName) {
  606. _parseExtension(extensionName);
  607. };
  608. /**
  609. * Remove an extension from THIS converter.
  610. * Note: This is a costly operation. It's better to initialize a new converter
  611. * and specify the extensions you wish to use
  612. * @param {Array} extension
  613. */
  614. this.removeExtension = function (extension) {
  615. if (!showdown.helper.isArray(extension)) {
  616. extension = [extension];
  617. }
  618. for (var a = 0; a < extension.length; ++a) {
  619. var ext = extension[a];
  620. for (var i = 0; i < langExtensions.length; ++i) {
  621. if (langExtensions[i] === ext) {
  622. langExtensions[i].splice(i, 1);
  623. }
  624. }
  625. for (var ii = 0; ii < outputModifiers.length; ++i) {
  626. if (outputModifiers[ii] === ext) {
  627. outputModifiers[ii].splice(i, 1);
  628. }
  629. }
  630. }
  631. };
  632. /**
  633. * Get all extension of THIS converter
  634. * @returns {{language: Array, output: Array}}
  635. */
  636. this.getAllExtensions = function () {
  637. return {
  638. language: langExtensions,
  639. output: outputModifiers
  640. };
  641. };
  642. };
  643. /**
  644. * Turn Markdown link shortcuts into XHTML <a> tags.
  645. */
  646. showdown.subParser('anchors', function (text, config, globals) {
  647. 'use strict';
  648. var writeAnchorTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
  649. if (showdown.helper.isUndefined(m7)) {
  650. m7 = '';
  651. }
  652. wholeMatch = m1;
  653. var linkText = m2,
  654. linkId = m3.toLowerCase(),
  655. url = m4,
  656. title = m7;
  657. if (!url) {
  658. if (!linkId) {
  659. // lower-case and turn embedded newlines into spaces
  660. linkId = linkText.toLowerCase().replace(/ ?\n/g, ' ');
  661. }
  662. url = '#' + linkId;
  663. if (!showdown.helper.isUndefined(globals.gUrls[linkId])) {
  664. url = globals.gUrls[linkId];
  665. if (!showdown.helper.isUndefined(globals.gTitles[linkId])) {
  666. title = globals.gTitles[linkId];
  667. }
  668. } else {
  669. if (wholeMatch.search(/\(\s*\)$/m) > -1) {
  670. // Special case for explicit empty url
  671. url = '';
  672. } else {
  673. return wholeMatch;
  674. }
  675. }
  676. }
  677. url = showdown.helper.escapeCharacters(url, '*_', false);
  678. var result = '<a href="' + url + '"';
  679. if (title !== '' && title !== null) {
  680. title = title.replace(/"/g, '&quot;');
  681. title = showdown.helper.escapeCharacters(title, '*_', false);
  682. result += ' title="' + title + '"';
  683. }
  684. result += '>' + linkText + '</a>';
  685. return result;
  686. };
  687. // First, handle reference-style links: [link text] [id]
  688. /*
  689. text = text.replace(/
  690. ( // wrap whole match in $1
  691. \[
  692. (
  693. (?:
  694. \[[^\]]*\] // allow brackets nested one level
  695. |
  696. [^\[] // or anything else
  697. )*
  698. )
  699. \]
  700. [ ]? // one optional space
  701. (?:\n[ ]*)? // one optional newline followed by spaces
  702. \[
  703. (.*?) // id = $3
  704. \]
  705. )()()()() // pad remaining backreferences
  706. /g,_DoAnchors_callback);
  707. */
  708. text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
  709. //
  710. // Next, inline-style links: [link text](url "optional title")
  711. //
  712. /*
  713. text = text.replace(/
  714. ( // wrap whole match in $1
  715. \[
  716. (
  717. (?:
  718. \[[^\]]*\] // allow brackets nested one level
  719. |
  720. [^\[\]] // or anything else
  721. )
  722. )
  723. \]
  724. \( // literal paren
  725. [ \t]*
  726. () // no id, so leave $3 empty
  727. <?(.*?)>? // href = $4
  728. [ \t]*
  729. ( // $5
  730. (['"]) // quote char = $6
  731. (.*?) // Title = $7
  732. \6 // matching quote
  733. [ \t]* // ignore any spaces/tabs between closing quote and )
  734. )? // title is optional
  735. \)
  736. )
  737. /g,writeAnchorTag);
  738. */
  739. text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
  740. writeAnchorTag);
  741. //
  742. // Last, handle reference-style shortcuts: [link text]
  743. // These must come last in case you've also got [link test][1]
  744. // or [link test](/foo)
  745. //
  746. /*
  747. text = text.replace(/
  748. ( // wrap whole match in $1
  749. \[
  750. ([^\[\]]+) // link text = $2; can't contain '[' or ']'
  751. \]
  752. )()()()()() // pad rest of backreferences
  753. /g, writeAnchorTag);
  754. */
  755. text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
  756. return text;
  757. });
  758. showdown.subParser('autoLinks', function (text, options) {
  759. 'use strict';
  760. //simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
  761. var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi,
  762. delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
  763. simpleMailRegex = /\b(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)\b/gi,
  764. delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
  765. text = text.replace(delimUrlRegex, '<a href=\"$1\">$1</a>');
  766. text = text.replace(delimMailRegex, replaceMail);
  767. //simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
  768. // Email addresses: <address@domain.foo>
  769. if (options.simplifiedAutoLink) {
  770. text = text.replace(simpleURLRegex, '<a href=\"$1\">$1</a>');
  771. text = text.replace(simpleMailRegex, '<a href=\"$1\">$1</a>');
  772. }
  773. function replaceMail(wholeMatch, m1) {
  774. var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
  775. return showdown.subParser('encodeEmailAddress')(unescapedStr);
  776. }
  777. return text;
  778. });
  779. /**
  780. * These are all the transformations that form block-level
  781. * tags like paragraphs, headers, and list items.
  782. */
  783. showdown.subParser('blockGamut', function (text, options, globals) {
  784. 'use strict';
  785. text = showdown.subParser('headers')(text, options, globals);
  786. // Do Horizontal Rules:
  787. var key = showdown.subParser('hashBlock')('<hr />', options, globals);
  788. text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, key);
  789. text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm, key);
  790. text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm, key);
  791. text = showdown.subParser('lists')(text, options, globals);
  792. text = showdown.subParser('codeBlocks')(text, options, globals);
  793. text = showdown.subParser('blockQuotes')(text, options, globals);
  794. // We already ran _HashHTMLBlocks() before, in Markdown(), but that
  795. // was to escape raw HTML in the original Markdown source. This time,
  796. // we're escaping the markup we've just created, so that we don't wrap
  797. // <p> tags around block-level tags.
  798. text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
  799. text = showdown.subParser('paragraphs')(text, options, globals);
  800. return text;
  801. });
  802. showdown.subParser('blockQuotes', function (text, options, globals) {
  803. 'use strict';
  804. /*
  805. text = text.replace(/
  806. ( // Wrap whole match in $1
  807. (
  808. ^[ \t]*>[ \t]? // '>' at the start of a line
  809. .+\n // rest of the first line
  810. (.+\n)* // subsequent consecutive lines
  811. \n* // blanks
  812. )+
  813. )
  814. /gm, function(){...});
  815. */
  816. text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
  817. var bq = m1;
  818. // attacklab: hack around Konqueror 3.5.4 bug:
  819. // "----------bug".replace(/^-/g,"") == "bug"
  820. bq = bq.replace(/^[ \t]*>[ \t]?/gm, '~0'); // trim one level of quoting
  821. // attacklab: clean up hack
  822. bq = bq.replace(/~0/g, '');
  823. bq = bq.replace(/^[ \t]+$/gm, ''); // trim whitespace-only lines
  824. bq = showdown.subParser('blockGamut')(bq, options, globals); // recurse
  825. bq = bq.replace(/(^|\n)/g, '$1 ');
  826. // These leading spaces screw with <pre> content, so we need to fix that:
  827. bq = bq.replace(/(\s*<pre>[^\r]+?<\/pre>)/gm, function (wholeMatch, m1) {
  828. var pre = m1;
  829. // attacklab: hack around Konqueror 3.5.4 bug:
  830. pre = pre.replace(/^ /mg, '~0');
  831. pre = pre.replace(/~0/g, '');
  832. return pre;
  833. });
  834. return showdown.subParser('hashBlock')('<blockquote>\n' + bq + '\n</blockquote>', options, globals);
  835. });
  836. return text;
  837. });
  838. /**
  839. * Process Markdown `<pre><code>` blocks.
  840. */
  841. showdown.subParser('codeBlocks', function (text, options, globals) {
  842. 'use strict';
  843. /*
  844. text = text.replace(text,
  845. /(?:\n\n|^)
  846. ( // $1 = the code block -- one or more lines, starting with a space/tab
  847. (?:
  848. (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
  849. .*\n+
  850. )+
  851. )
  852. (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
  853. /g,function(){...});
  854. */
  855. // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
  856. text += '~0';
  857. var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
  858. text = text.replace(pattern, function (wholeMatch, m1, m2) {
  859. var codeblock = m1,
  860. nextChar = m2,
  861. end = '\n';
  862. codeblock = showdown.subParser('outdent')(codeblock);
  863. codeblock = showdown.subParser('encodeCode')(codeblock);
  864. codeblock = showdown.subParser('detab')(codeblock);
  865. codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
  866. codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
  867. if (options.omitExtraWLInCodeBlocks) {
  868. end = '';
  869. }
  870. codeblock = '<pre><code>' + codeblock + end + '</code></pre>';
  871. return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
  872. });
  873. // attacklab: strip sentinel
  874. text = text.replace(/~0/, '');
  875. return text;
  876. });
  877. /**
  878. *
  879. * * Backtick quotes are used for <code></code> spans.
  880. *
  881. * * You can use multiple backticks as the delimiters if you want to
  882. * include literal backticks in the code span. So, this input:
  883. *
  884. * Just type ``foo `bar` baz`` at the prompt.
  885. *
  886. * Will translate to:
  887. *
  888. * <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
  889. *
  890. * There's no arbitrary limit to the number of backticks you
  891. * can use as delimters. If you need three consecutive backticks
  892. * in your code, use four for delimiters, etc.
  893. *
  894. * * You can use spaces to get literal backticks at the edges:
  895. *
  896. * ... type `` `bar` `` ...
  897. *
  898. * Turns to:
  899. *
  900. * ... type <code>`bar`</code> ...
  901. */
  902. showdown.subParser('codeSpans', function (text) {
  903. 'use strict';
  904. /*
  905. text = text.replace(/
  906. (^|[^\\]) // Character before opening ` can't be a backslash
  907. (`+) // $2 = Opening run of `
  908. ( // $3 = The code block
  909. [^\r]*?
  910. [^`] // attacklab: work around lack of lookbehind
  911. )
  912. \2 // Matching closer
  913. (?!`)
  914. /gm, function(){...});
  915. */
  916. text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, function (wholeMatch, m1, m2, m3) {
  917. var c = m3;
  918. c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
  919. c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
  920. c = showdown.subParser('encodeCode')(c);
  921. return m1 + '<code>' + c + '</code>';
  922. });
  923. return text;
  924. });
  925. /**
  926. * Convert all tabs to spaces
  927. */
  928. showdown.subParser('detab', function (text) {
  929. 'use strict';
  930. // expand first n-1 tabs
  931. text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
  932. // replace the nth with two sentinels
  933. text = text.replace(/\t/g, '~A~B');
  934. // use the sentinel to anchor our regex so it doesn't explode
  935. text = text.replace(/~B(.+?)~A/g, function (wholeMatch, m1) {
  936. var leadingText = m1,
  937. numSpaces = 4 - leadingText.length % 4; // g_tab_width
  938. // there *must* be a better way to do this:
  939. for (var i = 0; i < numSpaces; i++) {
  940. leadingText += ' ';
  941. }
  942. return leadingText;
  943. });
  944. // clean up sentinels
  945. text = text.replace(/~A/g, ' '); // g_tab_width
  946. text = text.replace(/~B/g, '');
  947. return text;
  948. });
  949. /**
  950. * Smart processing for ampersands and angle brackets that need to be encoded.
  951. */
  952. showdown.subParser('encodeAmpsAndAngles', function (text) {
  953. 'use strict';
  954. // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
  955. // http://bumppo.net/projects/amputator/
  956. text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, '&amp;');
  957. // Encode naked <'s
  958. text = text.replace(/<(?![a-z\/?\$!])/gi, '&lt;');
  959. return text;
  960. });
  961. /**
  962. * Returns the string, with after processing the following backslash escape sequences.
  963. *
  964. * attacklab: The polite way to do this is with the new escapeCharacters() function:
  965. *
  966. * text = escapeCharacters(text,"\\",true);
  967. * text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
  968. *
  969. * ...but we're sidestepping its use of the (slow) RegExp constructor
  970. * as an optimization for Firefox. This function gets called a LOT.
  971. */
  972. showdown.subParser('encodeBackslashEscapes', function (text) {
  973. 'use strict';
  974. text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback);
  975. text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, showdown.helper.escapeCharactersCallback);
  976. return text;
  977. });
  978. /**
  979. * Encode/escape certain characters inside Markdown code runs.
  980. * The point is that in code, these characters are literals,
  981. * and lose their special Markdown meanings.
  982. */
  983. showdown.subParser('encodeCode', function (text) {
  984. 'use strict';
  985. // Encode all ampersands; HTML entities are not
  986. // entities within a Markdown code span.
  987. text = text.replace(/&/g, '&amp;');
  988. // Do the angle bracket song and dance:
  989. text = text.replace(/</g, '&lt;');
  990. text = text.replace(/>/g, '&gt;');
  991. // Now, escape characters that are magic in Markdown:
  992. text = showdown.helper.escapeCharacters(text, '*_{}[]\\', false);
  993. // jj the line above breaks this:
  994. //---
  995. //* Item
  996. // 1. Subitem
  997. // special char: *
  998. // ---
  999. return text;
  1000. });
  1001. /**
  1002. * Input: an email address, e.g. "foo@example.com"
  1003. *
  1004. * Output: the email address as a mailto link, with each character
  1005. * of the address encoded as either a decimal or hex entity, in
  1006. * the hopes of foiling most address harvesting spam bots. E.g.:
  1007. *
  1008. * <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
  1009. * x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
  1010. * &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
  1011. *
  1012. * Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
  1013. * mailing list: <http://tinyurl.com/yu7ue>
  1014. *
  1015. */
  1016. showdown.subParser('encodeEmailAddress', function (addr) {
  1017. 'use strict';
  1018. var encode = [
  1019. function (ch) {
  1020. return '&#' + ch.charCodeAt(0) + ';';
  1021. },
  1022. function (ch) {
  1023. return '&#x' + ch.charCodeAt(0).toString(16) + ';';
  1024. },
  1025. function (ch) {
  1026. return ch;
  1027. }
  1028. ];
  1029. addr = 'mailto:' + addr;
  1030. addr = addr.replace(/./g, function (ch) {
  1031. if (ch === '@') {
  1032. // this *must* be encoded. I insist.
  1033. ch = encode[Math.floor(Math.random() * 2)](ch);
  1034. } else if (ch !== ':') {
  1035. // leave ':' alone (to spot mailto: later)
  1036. var r = Math.random();
  1037. // roughly 10% raw, 45% hex, 45% dec
  1038. ch = (
  1039. r > 0.9 ? encode[2](ch) : r > 0.45 ? encode[1](ch) : encode[0](ch)
  1040. );
  1041. }
  1042. return ch;
  1043. });
  1044. addr = '<a href="' + addr + '">' + addr + '</a>';
  1045. addr = addr.replace(/">.+:/g, '">'); // strip the mailto: from the visible part
  1046. return addr;
  1047. });
  1048. /**
  1049. * Within tags -- meaning between < and > -- encode [\ ` * _] so they
  1050. * don't conflict with their use in Markdown for code, italics and strong.
  1051. */
  1052. showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text) {
  1053. 'use strict';
  1054. // Build a regex to find HTML tags and comments. See Friedl's
  1055. // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
  1056. var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
  1057. text = text.replace(regex, function (wholeMatch) {
  1058. var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, '$1`');
  1059. tag = showdown.helper.escapeCharacters(tag, '\\`*_', false);
  1060. return tag;
  1061. });
  1062. return text;
  1063. });
  1064. /**
  1065. * Handle github codeblocks prior to running HashHTML so that
  1066. * HTML contained within the codeblock gets escaped properly
  1067. * Example:
  1068. * ```ruby
  1069. * def hello_world(x)
  1070. * puts "Hello, #{x}"
  1071. * end
  1072. * ```
  1073. */
  1074. showdown.subParser('githubCodeBlocks', function (text, options, globals) {
  1075. 'use strict';
  1076. text += '~0';
  1077. text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, m1, m2) {
  1078. var language = m1,
  1079. codeblock = m2,
  1080. end = '\n';
  1081. if (options.omitExtraWLInCodeBlocks) {
  1082. end = '';
  1083. }
  1084. codeblock = showdown.subParser('encodeCode')(codeblock);
  1085. codeblock = showdown.subParser('detab')(codeblock);
  1086. codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
  1087. codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
  1088. codeblock = '<pre><code' + (language ? ' class="' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
  1089. return showdown.subParser('hashBlock')(codeblock, options, globals);
  1090. });
  1091. // attacklab: strip sentinel
  1092. text = text.replace(/~0/, '');
  1093. return text;
  1094. });
  1095. showdown.subParser('hashBlock', function (text, options, globals) {
  1096. 'use strict';
  1097. text = text.replace(/(^\n+|\n+$)/g, '');
  1098. return '\n\n~K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
  1099. });
  1100. showdown.subParser('hashElement', function (text, options, globals) {
  1101. 'use strict';
  1102. return function (wholeMatch, m1) {
  1103. var blockText = m1;
  1104. // Undo double lines
  1105. blockText = blockText.replace(/\n\n/g, '\n');
  1106. blockText = blockText.replace(/^\n/, '');
  1107. // strip trailing blank lines
  1108. blockText = blockText.replace(/\n+$/g, '');
  1109. // Replace the element text with a marker ("~KxK" where x is its key)
  1110. blockText = '\n\n~K' + (globals.gHtmlBlocks.push(blockText) - 1) + 'K\n\n';
  1111. return blockText;
  1112. };
  1113. });
  1114. showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
  1115. 'use strict';
  1116. // attacklab: Double up blank lines to reduce lookaround
  1117. text = text.replace(/\n/g, '\n\n');
  1118. // Hashify HTML blocks:
  1119. // We only want to do this for block-level HTML tags, such as headers,
  1120. // lists, and tables. That's because we still want to wrap <p>s around
  1121. // "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  1122. // phrase emphasis, and spans. The list of tags we're looking for is
  1123. // hard-coded:
  1124. //var block_tags_a =
  1125. // 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del|style|section|header|footer|nav|article|aside';
  1126. // var block_tags_b =
  1127. // 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside';
  1128. // First, look for nested blocks, e.g.:
  1129. // <div>
  1130. // <div>
  1131. // tags for inner block must be indented.
  1132. // </div>
  1133. // </div>
  1134. //
  1135. // The outermost tags must start at the left margin for this to match, and
  1136. // the inner nested divs must be indented.
  1137. // We need to do this before the next, more liberal match, because the next
  1138. // match will start at the first `<div>` and stop at the first `</div>`.
  1139. // attacklab: This regex can be expensive when it fails.
  1140. /*
  1141. var text = text.replace(/
  1142. ( // save in $1
  1143. ^ // start of line (with /m)
  1144. <($block_tags_a) // start tag = $2
  1145. \b // word break
  1146. // attacklab: hack around khtml/pcre bug...
  1147. [^\r]*?\n // any number of lines, minimally matching
  1148. </\2> // the matching end tag
  1149. [ \t]* // trailing spaces/tabs
  1150. (?=\n+) // followed by a newline
  1151. ) // attacklab: there are sentinel newlines at end of document
  1152. /gm,function(){...}};
  1153. */
  1154. text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm,
  1155. showdown.subParser('hashElement')(text, options, globals));
  1156. //
  1157. // Now match more liberally, simply from `\n<tag>` to `</tag>\n`
  1158. //
  1159. /*
  1160. var text = text.replace(/
  1161. ( // save in $1
  1162. ^ // start of line (with /m)
  1163. <($block_tags_b) // start tag = $2
  1164. \b // word break
  1165. // attacklab: hack around khtml/pcre bug...
  1166. [^\r]*? // any number of lines, minimally matching
  1167. </\2> // the matching end tag
  1168. [ \t]* // trailing spaces/tabs
  1169. (?=\n+) // followed by a newline
  1170. ) // attacklab: there are sentinel newlines at end of document
  1171. /gm,function(){...}};
  1172. */
  1173. text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside|address|audio|canvas|figure|hgroup|output|video)\b[^\r]*?<\/\2>[ \t]*(?=\n+)\n)/gm,
  1174. showdown.subParser('hashElement')(text, options, globals));
  1175. // Special case just for <hr />. It was easier to make a special case than
  1176. // to make the other regex more complicated.
  1177. /*
  1178. text = text.replace(/
  1179. ( // save in $1
  1180. \n\n // Starting after a blank line
  1181. [ ]{0,3}
  1182. (<(hr) // start tag = $2
  1183. \b // word break
  1184. ([^<>])*? //
  1185. \/?>) // the matching end tag
  1186. [ \t]*
  1187. (?=\n{2,}) // followed by a blank line
  1188. )
  1189. /g,showdown.subParser('hashElement')(text, options, globals));
  1190. */
  1191. text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
  1192. showdown.subParser('hashElement')(text, options, globals));
  1193. // Special case for standalone HTML comments:
  1194. /*
  1195. text = text.replace(/
  1196. ( // save in $1
  1197. \n\n // Starting after a blank line
  1198. [ ]{0,3} // attacklab: g_tab_width - 1
  1199. <!
  1200. (--[^\r]*?--\s*)+
  1201. >
  1202. [ \t]*
  1203. (?=\n{2,}) // followed by a blank line
  1204. )
  1205. /g,showdown.subParser('hashElement')(text, options, globals));
  1206. */
  1207. text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g,
  1208. showdown.subParser('hashElement')(text, options, globals));
  1209. // PHP and ASP-style processor instructions (<?...?> and <%...%>)
  1210. /*
  1211. text = text.replace(/
  1212. (?:
  1213. \n\n // Starting after a blank line
  1214. )
  1215. ( // save in $1
  1216. [ ]{0,3} // attacklab: g_tab_width - 1
  1217. (?:
  1218. <([?%]) // $2
  1219. [^\r]*?
  1220. \2>
  1221. )
  1222. [ \t]*
  1223. (?=\n{2,}) // followed by a blank line
  1224. )
  1225. /g,showdown.subParser('hashElement')(text, options, globals));
  1226. */
  1227. text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
  1228. showdown.subParser('hashElement')(text, options, globals));
  1229. // attacklab: Undo double lines (see comment at top of this function)
  1230. text = text.replace(/\n\n/g, '\n');
  1231. return text;
  1232. });
  1233. showdown.subParser('headers', function (text, options, globals) {
  1234. 'use strict';
  1235. var prefixHeader = options.prefixHeaderId;
  1236. // Set text-style headers:
  1237. // Header 1
  1238. // ========
  1239. //
  1240. // Header 2
  1241. // --------
  1242. //
  1243. text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
  1244. var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
  1245. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
  1246. hLevel = parseInt(options.headerLevelStart),
  1247. hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
  1248. return showdown.subParser('hashBlock')(hashBlock, options, globals);
  1249. });
  1250. text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
  1251. var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
  1252. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
  1253. hLevel = parseInt(options.headerLevelStart) + 1,
  1254. hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
  1255. return showdown.subParser('hashBlock')(hashBlock, options, globals);
  1256. });
  1257. // atx-style headers:
  1258. // # Header 1
  1259. // ## Header 2
  1260. // ## Header 2 with closing hashes ##
  1261. // ...
  1262. // ###### Header 6
  1263. //
  1264. /*
  1265. text = text.replace(/
  1266. ^(\#{1,6}) // $1 = string of #'s
  1267. [ \t]*
  1268. (.+?) // $2 = Header text
  1269. [ \t]*
  1270. \#* // optional closing #'s (not counted)
  1271. \n+
  1272. /gm, function() {...});
  1273. */
  1274. text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
  1275. var span = showdown.subParser('spanGamut')(m2, options, globals),
  1276. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
  1277. hLevel = parseInt(options.headerLevelStart) - 1 + m1.length,
  1278. header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';
  1279. return showdown.subParser('hashBlock')(header, options, globals);
  1280. });
  1281. function headerId(m) {
  1282. var title, escapedId = m.replace(/[^\w]/g, '').toLowerCase();
  1283. if (globals.hashLinkCounts[escapedId]) {
  1284. title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
  1285. } else {
  1286. title = escapedId;
  1287. globals.hashLinkCounts[escapedId] = 1;
  1288. }
  1289. // Prefix id to prevent causing inadvertent pre-existing style matches.
  1290. if (prefixHeader === true) {
  1291. prefixHeader = 'section';
  1292. }
  1293. if (showdown.helper.isString(prefixHeader)) {
  1294. return prefixHeader + title;
  1295. }
  1296. return title;
  1297. }
  1298. return text;
  1299. });
  1300. /**
  1301. * Turn Markdown image shortcuts into <img> tags.
  1302. */
  1303. showdown.subParser('images', function (text, options, globals) {
  1304. 'use strict';
  1305. var inlineRegExp = /!\[(.*?)]\s?\([ \t]*()<?(\S+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(['"])(.*?)\6[ \t]*)?\)/g,
  1306. referenceRegExp = /!\[(.*?)][ ]?(?:\n[ ]*)?\[(.*?)]()()()()()/g;
  1307. function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
  1308. var gUrls = globals.gUrls,
  1309. gTitles = globals.gTitles,
  1310. gDims = globals.gDimensions;
  1311. linkId = linkId.toLowerCase();
  1312. if (!title) {
  1313. title = '';
  1314. }
  1315. if (url === '' || url === null) {
  1316. if (linkId === '' || linkId === null) {
  1317. // lower-case and turn embedded newlines into spaces
  1318. linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
  1319. }
  1320. url = '#' + linkId;
  1321. if (!showdown.helper.isUndefined(gUrls[linkId])) {
  1322. url = gUrls[linkId];
  1323. if (!showdown.helper.isUndefined(gTitles[linkId])) {
  1324. title = gTitles[linkId];
  1325. }
  1326. if (!showdown.helper.isUndefined(gDims[linkId])) {
  1327. width = gDims[linkId].width;
  1328. height = gDims[linkId].height;
  1329. }
  1330. } else {
  1331. return wholeMatch;
  1332. }
  1333. }
  1334. altText = altText.replace(/"/g, '&quot;');
  1335. url = showdown.helper.escapeCharacters(url, '*_', false);
  1336. var result = '<img src="' + url + '" alt="' + altText + '"';
  1337. if (title) {
  1338. title = title.replace(/"/g, '&quot;');
  1339. title = showdown.helper.escapeCharacters(title, '*_', false);
  1340. result += ' title="' + title + '"';
  1341. }
  1342. if (width && height) {
  1343. width = (width === '*') ? 'auto' : width;
  1344. height = (height === '*') ? 'auto' : height;
  1345. result += ' width="' + width + '"';
  1346. result += ' height="' + height + '"';
  1347. }
  1348. result += ' />';
  1349. return result;
  1350. }
  1351. // First, handle reference-style labeled images: ![alt text][id]
  1352. text = text.replace(referenceRegExp, writeImageTag);
  1353. // Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
  1354. text = text.replace(inlineRegExp, writeImageTag);
  1355. return text;
  1356. });
  1357. showdown.subParser('italicsAndBold', function (text) {
  1358. 'use strict';
  1359. // <strong> must go first:
  1360. text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
  1361. text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
  1362. return text;
  1363. });
  1364. /**
  1365. * Form HTML ordered (numbered) and unordered (bulleted) lists.
  1366. */
  1367. showdown.subParser('lists', function (text, options, globals) {
  1368. 'use strict';
  1369. var spl = '~1';
  1370. /**
  1371. * Process the contents of a single ordered or unordered list, splitting it
  1372. * into individual list items.
  1373. * @param {string} listStr
  1374. * @returns {string}
  1375. */
  1376. function processListItems (listStr) {
  1377. // The $g_list_level global keeps track of when we're inside a list.
  1378. // Each time we enter a list, we increment it; when we leave a list,
  1379. // we decrement. If it's zero, we're not in a list anymore.
  1380. //
  1381. // We do this because when we're not inside a list, we want to treat
  1382. // something like this:
  1383. //
  1384. // I recommend upgrading to version
  1385. // 8. Oops, now this line is treated
  1386. // as a sub-list.
  1387. //
  1388. // As a single paragraph, despite the fact that the second line starts
  1389. // with a digit-period-space sequence.
  1390. //
  1391. // Whereas when we're inside a list (or sub-list), that line will be
  1392. // treated as the start of a sub-list. What a kludge, huh? This is
  1393. // an aspect of Markdown's syntax that's hard to parse perfectly
  1394. // without resorting to mind-reading. Perhaps the solution is to
  1395. // change the syntax rules such that sub-lists must start with a
  1396. // starting cardinal number; e.g. "1." or "a.".
  1397. globals.gListLevel++;
  1398. // trim trailing blank lines:
  1399. listStr = listStr.replace(/\n{2,}$/, '\n');
  1400. // attacklab: add sentinel to emulate \z
  1401. listStr += '~0';
  1402. /*
  1403. list_str = list_str.replace(/
  1404. (\n)? // leading line = $1
  1405. (^[ \t]*) // leading whitespace = $2
  1406. ([*+-]|\d+[.]) [ \t]+ // list marker = $3
  1407. ([^\r]+? // list item text = $4
  1408. (\n{1,2}))
  1409. (?= \n* (~0 | \2 ([*+-]|\d+[.]) [ \t]+))
  1410. /gm, function(){...});
  1411. */
  1412. var rgx = /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm;
  1413. listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4) {
  1414. var item = showdown.subParser('outdent')(m4, options, globals);
  1415. //m1 - LeadingLine
  1416. if (m1 || (item.search(/\n{2,}/) > -1)) {
  1417. item = showdown.subParser('blockGamut')(item, options, globals);
  1418. } else {
  1419. // Recursion for sub-lists:
  1420. item = showdown.subParser('lists')(item, options, globals);
  1421. item = item.replace(/\n$/, ''); // chomp(item)
  1422. item = showdown.subParser('spanGamut')(item, options, globals);
  1423. }
  1424. // this is a "hack" to differentiate between ordered and unordered lists
  1425. // related to issue #142
  1426. var tp = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  1427. return spl + tp + '<li>' + item + '</li>\n';
  1428. });
  1429. // attacklab: strip sentinel
  1430. listStr = listStr.replace(/~0/g, '');
  1431. globals.gListLevel--;
  1432. return listStr;
  1433. }
  1434. /**
  1435. * Slit consecutive ol/ul lists (related to issue 142)
  1436. * @param {Array} results
  1437. * @param {string} listType
  1438. * @returns {string|*}
  1439. */
  1440. function splitConsecutiveLists (results, listType) {
  1441. var cthulhu = /(<p[^>]+?>|<p>|<\/p>)/img,
  1442. holder = [[]],
  1443. res = '',
  1444. y = 0;
  1445. // Initialize first sublist
  1446. holder[0].type = listType;
  1447. for (var i = 0; i < results.length; ++i) {
  1448. var txt = results[i].slice(2),
  1449. nListType = results[i].slice(0, 2);
  1450. if (listType !== nListType) {
  1451. y++;
  1452. holder[y] = [];
  1453. holder[y].type = nListType;
  1454. listType = nListType;
  1455. }
  1456. holder[y].push(txt);
  1457. }
  1458. for (i = 0; i < holder.length; ++i) {
  1459. res += '<' + holder[i].type + '>\n';
  1460. for (var ii = 0; ii < holder[i].length; ++ii) {
  1461. if (holder[i].length > 1 && ii === holder[i].length - 1 && !cthulhu.test(holder[i][ii - 1])) {
  1462. //holder[i][ii] = holder[i][ii].replace(cthulhu, '');
  1463. }
  1464. res += holder[i][ii];
  1465. }
  1466. res += '</' + holder[i].type + '>\n';
  1467. }
  1468. return res;
  1469. }
  1470. // attacklab: add sentinel to hack around khtml/safari bug:
  1471. // http://bugs.webkit.org/show_bug.cgi?id=11231
  1472. text += '~0';
  1473. // Re-usable pattern to match any entire ul or ol list:
  1474. /*
  1475. var whole_list = /
  1476. ( // $1 = whole list
  1477. ( // $2
  1478. [ ]{0,3} // attacklab: g_tab_width - 1
  1479. ([*+-]|\d+[.]) // $3 = first list item marker
  1480. [ \t]+
  1481. )
  1482. [^\r]+?
  1483. ( // $4
  1484. ~0 // sentinel for workaround; should be $
  1485. |
  1486. \n{2,}
  1487. (?=\S)
  1488. (?! // Negative lookahead for another list item marker
  1489. [ \t]*
  1490. (?:[*+-]|\d+[.])[ \t]+
  1491. )
  1492. )
  1493. )/g
  1494. */
  1495. var wholeList = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
  1496. if (globals.gListLevel) {
  1497. text = text.replace(wholeList, function (wholeMatch, m1, m2) {
  1498. var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol',
  1499. result = processListItems(m1);
  1500. // Turn double returns into triple returns, so that we can make a
  1501. // paragraph for the last item in a list, if necessary:
  1502. //list = list.replace(/\n{2,}/g, '\n\n\n');
  1503. //result = processListItems(list);
  1504. // Trim any trailing whitespace, to put the closing `</$list_type>`
  1505. // up on the preceding line, to get it past the current stupid
  1506. // HTML block parser. This is a hack to work around the terrible
  1507. // hack that is the HTML block parser.
  1508. result = result.replace(/\s+$/, '');
  1509. var splRes = result.split(spl);
  1510. splRes.shift();
  1511. result = splitConsecutiveLists(splRes, listType);
  1512. return result;
  1513. });
  1514. } else {
  1515. wholeList = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
  1516. //wholeList = /(\n\n|^\n?)( {0,3}([*+-]|\d+\.)[ \t]+[\s\S]+?)(?=(~0)|(\n\n(?!\t| {2,}| {0,3}([*+-]|\d+\.)[ \t])))/g;
  1517. text = text.replace(wholeList, function (wholeMatch, m1, m2, m3) {
  1518. // Turn double returns into triple returns, so that we can make a
  1519. // paragraph for the last item in a list, if necessary:
  1520. var list = m2.replace(/\n{2,}/g, '\n\n\n'),
  1521. //var list = (m2.slice(-2) !== '~0') ? m2 + '\n' : m2, //add a newline after the list
  1522. listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol',
  1523. result = processListItems(list),
  1524. splRes = result.split(spl);
  1525. splRes.shift();
  1526. return m1 + splitConsecutiveLists(splRes, listType) + '\n';
  1527. });
  1528. }
  1529. // attacklab: strip sentinel
  1530. text = text.replace(/~0/, '');
  1531. return text;
  1532. });
  1533. /**
  1534. * Remove one level of line-leading tabs or spaces
  1535. */
  1536. showdown.subParser('outdent', function (text) {
  1537. 'use strict';
  1538. // attacklab: hack around Konqueror 3.5.4 bug:
  1539. // "----------bug".replace(/^-/g,"") == "bug"
  1540. text = text.replace(/^(\t|[ ]{1,4})/gm, '~0'); // attacklab: g_tab_width
  1541. // attacklab: clean up hack
  1542. text = text.replace(/~0/g, '');
  1543. return text;
  1544. });
  1545. /**
  1546. *
  1547. */
  1548. showdown.subParser('paragraphs', function (text, options, globals) {
  1549. 'use strict';
  1550. // Strip leading and trailing lines:
  1551. text = text.replace(/^\n+/g, '');
  1552. text = text.replace(/\n+$/g, '');
  1553. var grafs = text.split(/\n{2,}/g),
  1554. grafsOut = [],
  1555. end = grafs.length; // Wrap <p> tags
  1556. for (var i = 0; i < end; i++) {
  1557. var str = grafs[i];
  1558. // if this is an HTML marker, copy it
  1559. if (str.search(/~K(\d+)K/g) >= 0) {
  1560. grafsOut.push(str);
  1561. } else if (str.search(/\S/) >= 0) {
  1562. str = showdown.subParser('spanGamut')(str, options, globals);
  1563. str = str.replace(/^([ \t]*)/g, '<p>');
  1564. str += '</p>';
  1565. grafsOut.push(str);
  1566. }
  1567. }
  1568. /** Unhashify HTML blocks */
  1569. end = grafsOut.length;
  1570. for (i = 0; i < end; i++) {
  1571. // if this is a marker for an html block...
  1572. while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
  1573. var blockText = globals.gHtmlBlocks[RegExp.$1];
  1574. blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
  1575. grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
  1576. }
  1577. }
  1578. return grafsOut.join('\n\n');
  1579. });
  1580. /**
  1581. * Run extension
  1582. */
  1583. showdown.subParser('runExtension', function (ext, text, options, globals) {
  1584. 'use strict';
  1585. if (ext.filter) {
  1586. text = ext.filter(text, globals.converter, options);
  1587. } else if (ext.regex) {
  1588. // TODO remove this when old extension loading mechanism is deprecated
  1589. var re = ext.regex;
  1590. if (!re instanceof RegExp) {
  1591. re = new RegExp(re, 'g');
  1592. }
  1593. text = text.replace(re, ext.replace);
  1594. }
  1595. return text;
  1596. });
  1597. /**
  1598. * These are all the transformations that occur *within* block-level
  1599. * tags like paragraphs, headers, and list items.
  1600. */
  1601. showdown.subParser('spanGamut', function (text, options, globals) {
  1602. 'use strict';
  1603. text = showdown.subParser('codeSpans')(text, options, globals);
  1604. text = showdown.subParser('escapeSpecialCharsWithinTagAttributes')(text, options, globals);
  1605. text = showdown.subParser('encodeBackslashEscapes')(text, options, globals);
  1606. // Process anchor and image tags. Images must come first,
  1607. // because ![foo][f] looks like an anchor.
  1608. text = showdown.subParser('images')(text, options, globals);
  1609. text = showdown.subParser('anchors')(text, options, globals);
  1610. // Make links out of things like `<http://example.com/>`
  1611. // Must come after _DoAnchors(), because you can use < and >
  1612. // delimiters in inline links like [this](<url>).
  1613. text = showdown.subParser('autoLinks')(text, options, globals);
  1614. text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals);
  1615. text = showdown.subParser('italicsAndBold')(text, options, globals);
  1616. // Do hard breaks:
  1617. text = text.replace(/ +\n/g, ' <br />\n');
  1618. return text;
  1619. });
  1620. /**
  1621. * Strip any lines consisting only of spaces and tabs.
  1622. * This makes subsequent regexs easier to write, because we can
  1623. * match consecutive blank lines with /\n+/ instead of something
  1624. * contorted like /[ \t]*\n+/
  1625. */
  1626. showdown.subParser('stripBlankLines', function (text) {
  1627. 'use strict';
  1628. return text.replace(/^[ \t]+$/mg, '');
  1629. });
  1630. /**
  1631. * Strips link definitions from text, stores the URLs and titles in
  1632. * hash references.
  1633. * Link defs are in the form: ^[id]: url "optional title"
  1634. *
  1635. * ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
  1636. * [ \t]*
  1637. * \n? // maybe *one* newline
  1638. * [ \t]*
  1639. * <?(\S+?)>? // url = $2
  1640. * [ \t]*
  1641. * \n? // maybe one newline
  1642. * [ \t]*
  1643. * (?:
  1644. * (\n*) // any lines skipped = $3 attacklab: lookbehind removed
  1645. * ["(]
  1646. * (.+?) // title = $4
  1647. * [")]
  1648. * [ \t]*
  1649. * )? // title is optional
  1650. * (?:\n+|$)
  1651. * /gm,
  1652. * function(){...});
  1653. *
  1654. */
  1655. showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
  1656. 'use strict';
  1657. 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;
  1658. // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
  1659. text += '~0';
  1660. text = text.replace(regex, function (wholeMatch, linkId, url, width, height, blankLines, title) {
  1661. linkId = linkId.toLowerCase();
  1662. globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url); // Link IDs are case-insensitive
  1663. if (blankLines) {
  1664. // Oops, found blank lines, so it's not a title.
  1665. // Put back the parenthetical statement we stole.
  1666. return blankLines + title;
  1667. } else {
  1668. if (title) {
  1669. globals.gTitles[linkId] = title.replace(/"|'/g, '&quot;');
  1670. }
  1671. if (options.parseImgDimensions && width && height) {
  1672. globals.gDimensions[linkId] = {
  1673. width: width,
  1674. height: height
  1675. };
  1676. }
  1677. }
  1678. // Completely remove the definition from the text
  1679. return '';
  1680. });
  1681. // attacklab: strip sentinel
  1682. text = text.replace(/~0/, '');
  1683. return text;
  1684. });
  1685. /**
  1686. * Swap back in all the special characters we've hidden.
  1687. */
  1688. showdown.subParser('unescapeSpecialChars', function (text) {
  1689. 'use strict';
  1690. text = text.replace(/~E(\d+)E/g, function (wholeMatch, m1) {
  1691. var charCodeToReplace = parseInt(m1);
  1692. return String.fromCharCode(charCodeToReplace);
  1693. });
  1694. return text;
  1695. });
  1696. var root = this;
  1697. // CommonJS/nodeJS Loader
  1698. if (typeof module !== 'undefined' && module.exports) {
  1699. module.exports = showdown;
  1700. // AMD Loader
  1701. } else if (typeof define === 'function' && define.amd) {
  1702. define('showdown', function () {
  1703. 'use strict';
  1704. return showdown;
  1705. });
  1706. // Regular Browser loader
  1707. } else {
  1708. root.showdown = showdown;
  1709. }
  1710. }).call(this);
  1711. //# sourceMappingURL=showdown.js.map