showdown.js 52 KB

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