showdown.js 55 KB

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