marked.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /**
  2. * marked - a markdown parser
  3. * Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
  4. * https://github.com/chjj/marked
  5. */
  6. ;
  7. (function() {
  8. /**
  9. * Block-Level Grammar
  10. */
  11. var block = {
  12. newline: /^\n+/,
  13. code: /^( {4}[^\n]+\n*)+/,
  14. fences: noop,
  15. hr: /^( *[-*_]){3,} *(?:\n+|$)/,
  16. heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
  17. nptable: noop,
  18. lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
  19. blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
  20. list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
  21. html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
  22. def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
  23. table: noop,
  24. paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
  25. text: /^[^\n]+/
  26. };
  27. block.bullet = /(?:[*+-]|\d+\.)/;
  28. block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
  29. block.item = replace(block.item, 'gm')
  30. (/bull/g, block.bullet)
  31. ();
  32. block.list = replace(block.list)
  33. (/bull/g, block.bullet)
  34. ('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))')
  35. ('def', '\\n+(?=' + block.def.source + ')')
  36. ();
  37. block.blockquote = replace(block.blockquote)
  38. ('def', block.def)
  39. ();
  40. block._tag = '(?!(?:' +
  41. 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code' +
  42. '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo' +
  43. '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
  44. block.html = replace(block.html)
  45. ('comment', /<!--[\s\S]*?-->/)
  46. ('closed', /<(tag)[\s\S]+?<\/\1>/)
  47. ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
  48. (/tag/g, block._tag)
  49. ();
  50. block.paragraph = replace(block.paragraph)
  51. ('hr', block.hr)
  52. ('heading', block.heading)
  53. ('lheading', block.lheading)
  54. ('blockquote', block.blockquote)
  55. ('tag', '<' + block._tag)
  56. ('def', block.def)
  57. ();
  58. /**
  59. * Normal Block Grammar
  60. */
  61. block.normal = merge({}, block);
  62. /**
  63. * GFM Block Grammar
  64. */
  65. block.gfm = merge({}, block.normal, {
  66. fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/,
  67. paragraph: /^/,
  68. heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/
  69. });
  70. block.gfm.paragraph = replace(block.paragraph)
  71. ('(?!', '(?!' +
  72. block.gfm.fences.source.replace('\\1', '\\2') + '|' +
  73. block.list.source.replace('\\1', '\\3') + '|')
  74. ();
  75. /**
  76. * GFM + Tables Block Grammar
  77. */
  78. block.tables = merge({}, block.gfm, {
  79. nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
  80. table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
  81. });
  82. /**
  83. * Block Lexer
  84. */
  85. function Lexer(options) {
  86. this.tokens = [];
  87. this.tokens.links = {};
  88. this.options = options || marked.defaults;
  89. this.rules = block.normal;
  90. if (this.options.gfm) {
  91. if (this.options.tables) {
  92. this.rules = block.tables;
  93. } else {
  94. this.rules = block.gfm;
  95. }
  96. }
  97. }
  98. /**
  99. * Expose Block Rules
  100. */
  101. Lexer.rules = block;
  102. /**
  103. * Static Lex Method
  104. */
  105. Lexer.lex = function(src, options) {
  106. var lexer = new Lexer(options);
  107. return lexer.lex(src);
  108. };
  109. /**
  110. * Preprocessing
  111. */
  112. Lexer.prototype.lex = function(src) {
  113. src = src
  114. .replace(/\r\n|\r/g, '\n')
  115. .replace(/\t/g, ' ')
  116. .replace(/\u00a0/g, ' ')
  117. .replace(/\u2424/g, '\n');
  118. return this.token(src, true);
  119. };
  120. /**
  121. * Lexing
  122. */
  123. Lexer.prototype.token = function(src, top, bq) {
  124. var src = src.replace(/^ +$/gm, ''),
  125. next, loose, cap, bull, b, item, space, i, l;
  126. while (src) {
  127. // newline
  128. if (cap = this.rules.newline.exec(src)) {
  129. src = src.substring(cap[0].length);
  130. if (cap[0].length > 1) {
  131. this.tokens.push({
  132. type: 'space'
  133. });
  134. }
  135. }
  136. // code
  137. if (cap = this.rules.code.exec(src)) {
  138. src = src.substring(cap[0].length);
  139. cap = cap[0].replace(/^ {4}/gm, '');
  140. this.tokens.push({
  141. type: 'code',
  142. text: !this.options.pedantic ?
  143. cap.replace(/\n+$/, '') :
  144. cap
  145. });
  146. continue;
  147. }
  148. // fences (gfm)
  149. if (cap = this.rules.fences.exec(src)) {
  150. src = src.substring(cap[0].length);
  151. this.tokens.push({
  152. type: 'code',
  153. lang: cap[2],
  154. text: cap[3] || ''
  155. });
  156. continue;
  157. }
  158. // heading
  159. if (cap = this.rules.heading.exec(src)) {
  160. src = src.substring(cap[0].length);
  161. this.tokens.push({
  162. type: 'heading',
  163. depth: cap[1].length,
  164. text: cap[2]
  165. });
  166. continue;
  167. }
  168. // table no leading pipe (gfm)
  169. if (top && (cap = this.rules.nptable.exec(src))) {
  170. src = src.substring(cap[0].length);
  171. item = {
  172. type: 'table',
  173. header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
  174. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  175. cells: cap[3].replace(/\n$/, '').split('\n')
  176. };
  177. for (i = 0; i < item.align.length; i++) {
  178. if (/^ *-+: *$/.test(item.align[i])) {
  179. item.align[i] = 'right';
  180. } else if (/^ *:-+: *$/.test(item.align[i])) {
  181. item.align[i] = 'center';
  182. } else if (/^ *:-+ *$/.test(item.align[i])) {
  183. item.align[i] = 'left';
  184. } else {
  185. item.align[i] = null;
  186. }
  187. }
  188. for (i = 0; i < item.cells.length; i++) {
  189. item.cells[i] = item.cells[i].split(/ *\| */);
  190. }
  191. this.tokens.push(item);
  192. continue;
  193. }
  194. // lheading
  195. if (cap = this.rules.lheading.exec(src)) {
  196. src = src.substring(cap[0].length);
  197. this.tokens.push({
  198. type: 'heading',
  199. depth: cap[2] === '=' ? 1 : 2,
  200. text: cap[1]
  201. });
  202. continue;
  203. }
  204. // hr
  205. if (cap = this.rules.hr.exec(src)) {
  206. src = src.substring(cap[0].length);
  207. this.tokens.push({
  208. type: 'hr'
  209. });
  210. continue;
  211. }
  212. // blockquote
  213. if (cap = this.rules.blockquote.exec(src)) {
  214. src = src.substring(cap[0].length);
  215. this.tokens.push({
  216. type: 'blockquote_start'
  217. });
  218. cap = cap[0].replace(/^ *> ?/gm, '');
  219. // Pass `top` to keep the current
  220. // "toplevel" state. This is exactly
  221. // how markdown.pl works.
  222. this.token(cap, top, true);
  223. this.tokens.push({
  224. type: 'blockquote_end'
  225. });
  226. continue;
  227. }
  228. // list
  229. if (cap = this.rules.list.exec(src)) {
  230. src = src.substring(cap[0].length);
  231. bull = cap[2];
  232. this.tokens.push({
  233. type: 'list_start',
  234. ordered: bull.length > 1
  235. });
  236. // Get each top-level item.
  237. cap = cap[0].match(this.rules.item);
  238. next = false;
  239. l = cap.length;
  240. i = 0;
  241. for (; i < l; i++) {
  242. item = cap[i];
  243. // Remove the list item's bullet
  244. // so it is seen as the next token.
  245. space = item.length;
  246. item = item.replace(/^ *([*+-]|\d+\.) +/, '');
  247. // Outdent whatever the
  248. // list item contains. Hacky.
  249. if (~item.indexOf('\n ')) {
  250. space -= item.length;
  251. item = !this.options.pedantic ?
  252. item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') :
  253. item.replace(/^ {1,4}/gm, '');
  254. }
  255. // Determine whether the next list item belongs here.
  256. // Backpedal if it does not belong in this list.
  257. if (this.options.smartLists && i !== l - 1) {
  258. b = block.bullet.exec(cap[i + 1])[0];
  259. if (bull !== b && !(bull.length > 1 && b.length > 1)) {
  260. src = cap.slice(i + 1).join('\n') + src;
  261. i = l - 1;
  262. }
  263. }
  264. // Determine whether item is loose or not.
  265. // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
  266. // for discount behavior.
  267. loose = next || /\n\n(?!\s*$)/.test(item);
  268. if (i !== l - 1) {
  269. next = item.charAt(item.length - 1) === '\n';
  270. if (!loose) loose = next;
  271. }
  272. this.tokens.push({
  273. type: loose ?
  274. 'loose_item_start' :
  275. 'list_item_start'
  276. });
  277. // Recurse.
  278. this.token(item, false, bq);
  279. this.tokens.push({
  280. type: 'list_item_end'
  281. });
  282. }
  283. this.tokens.push({
  284. type: 'list_end'
  285. });
  286. continue;
  287. }
  288. // html
  289. if (cap = this.rules.html.exec(src)) {
  290. src = src.substring(cap[0].length);
  291. this.tokens.push({
  292. type: this.options.sanitize ?
  293. 'paragraph' :
  294. 'html',
  295. pre: !this.options.sanitizer &&
  296. (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
  297. text: cap[0]
  298. });
  299. continue;
  300. }
  301. // def
  302. if ((!bq && top) && (cap = this.rules.def.exec(src))) {
  303. src = src.substring(cap[0].length);
  304. this.tokens.links[cap[1].toLowerCase()] = {
  305. href: cap[2],
  306. title: cap[3]
  307. };
  308. continue;
  309. }
  310. // table (gfm)
  311. if (top && (cap = this.rules.table.exec(src))) {
  312. src = src.substring(cap[0].length);
  313. item = {
  314. type: 'table',
  315. header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
  316. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  317. cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
  318. };
  319. for (i = 0; i < item.align.length; i++) {
  320. if (/^ *-+: *$/.test(item.align[i])) {
  321. item.align[i] = 'right';
  322. } else if (/^ *:-+: *$/.test(item.align[i])) {
  323. item.align[i] = 'center';
  324. } else if (/^ *:-+ *$/.test(item.align[i])) {
  325. item.align[i] = 'left';
  326. } else {
  327. item.align[i] = null;
  328. }
  329. }
  330. for (i = 0; i < item.cells.length; i++) {
  331. item.cells[i] = item.cells[i]
  332. .replace(/^ *\| *| *\| *$/g, '')
  333. .split(/ *\| */);
  334. }
  335. this.tokens.push(item);
  336. continue;
  337. }
  338. // top-level paragraph
  339. if (top && (cap = this.rules.paragraph.exec(src))) {
  340. src = src.substring(cap[0].length);
  341. this.tokens.push({
  342. type: 'paragraph',
  343. text: cap[1].charAt(cap[1].length - 1) === '\n' ?
  344. cap[1].slice(0, -1) :
  345. cap[1]
  346. });
  347. continue;
  348. }
  349. // text
  350. if (cap = this.rules.text.exec(src)) {
  351. // Top-level should never reach here.
  352. src = src.substring(cap[0].length);
  353. this.tokens.push({
  354. type: 'text',
  355. text: cap[0]
  356. });
  357. continue;
  358. }
  359. if (src) {
  360. throw new
  361. Error('Infinite loop on byte: ' + src.charCodeAt(0));
  362. }
  363. }
  364. return this.tokens;
  365. };
  366. /**
  367. * Inline-Level Grammar
  368. */
  369. var inline = {
  370. escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
  371. autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
  372. url: noop,
  373. tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
  374. link: /^!?\[(inside)\]\(href\)/,
  375. reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
  376. nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
  377. strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
  378. em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
  379. code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
  380. br: /^ {2,}\n(?!\s*$)/,
  381. del: noop,
  382. text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
  383. };
  384. inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
  385. inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
  386. inline.link = replace(inline.link)
  387. ('inside', inline._inside)
  388. ('href', inline._href)
  389. ();
  390. inline.reflink = replace(inline.reflink)
  391. ('inside', inline._inside)
  392. ();
  393. /**
  394. * Normal Inline Grammar
  395. */
  396. inline.normal = merge({}, inline);
  397. /**
  398. * Pedantic Inline Grammar
  399. */
  400. inline.pedantic = merge({}, inline.normal, {
  401. strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
  402. em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
  403. });
  404. /**
  405. * GFM Inline Grammar
  406. */
  407. inline.gfm = merge({}, inline.normal, {
  408. escape: replace(inline.escape)('])', '~|])')(),
  409. url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
  410. del: /^~~(?=\S)([\s\S]*?\S)~~/,
  411. text: replace(inline.text)
  412. (']|', '~]|')
  413. ('|', '|https?://|')
  414. ()
  415. });
  416. /**
  417. * GFM + Line Breaks Inline Grammar
  418. */
  419. inline.breaks = merge({}, inline.gfm, {
  420. br: replace(inline.br)('{2,}', '*')(),
  421. text: replace(inline.gfm.text)('{2,}', '*')()
  422. });
  423. /**
  424. * Inline Lexer & Compiler
  425. */
  426. function InlineLexer(links, options) {
  427. this.options = options || marked.defaults;
  428. this.links = links;
  429. this.rules = inline.normal;
  430. this.renderer = this.options.renderer || new Renderer;
  431. this.renderer.options = this.options;
  432. if (!this.links) {
  433. throw new
  434. Error('Tokens array requires a `links` property.');
  435. }
  436. if (this.options.gfm) {
  437. if (this.options.breaks) {
  438. this.rules = inline.breaks;
  439. } else {
  440. this.rules = inline.gfm;
  441. }
  442. } else if (this.options.pedantic) {
  443. this.rules = inline.pedantic;
  444. }
  445. }
  446. /**
  447. * Expose Inline Rules
  448. */
  449. InlineLexer.rules = inline;
  450. /**
  451. * Static Lexing/Compiling Method
  452. */
  453. InlineLexer.output = function(src, links, options) {
  454. var inline = new InlineLexer(links, options);
  455. return inline.output(src);
  456. };
  457. /**
  458. * Lexing/Compiling
  459. */
  460. InlineLexer.prototype.output = function(src) {
  461. var out = '',
  462. link, text, href, cap;
  463. while (src) {
  464. // escape
  465. if (cap = this.rules.escape.exec(src)) {
  466. src = src.substring(cap[0].length);
  467. out += cap[1];
  468. continue;
  469. }
  470. // autolink
  471. if (cap = this.rules.autolink.exec(src)) {
  472. src = src.substring(cap[0].length);
  473. if (cap[2] === '@') {
  474. text = cap[1].charAt(6) === ':' ?
  475. this.mangle(cap[1].substring(7)) :
  476. this.mangle(cap[1]);
  477. href = this.mangle('mailto:') + text;
  478. } else {
  479. text = escape(cap[1]);
  480. href = text;
  481. }
  482. out += this.renderer.link(href, null, text);
  483. continue;
  484. }
  485. // url (gfm)
  486. if (!this.inLink && (cap = this.rules.url.exec(src))) {
  487. src = src.substring(cap[0].length);
  488. text = escape(cap[1]);
  489. href = text;
  490. out += this.renderer.link(href, null, text);
  491. continue;
  492. }
  493. // tag
  494. if (cap = this.rules.tag.exec(src)) {
  495. if (!this.inLink && /^<a /i.test(cap[0])) {
  496. this.inLink = true;
  497. } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
  498. this.inLink = false;
  499. }
  500. src = src.substring(cap[0].length);
  501. out += this.options.sanitize ?
  502. this.options.sanitizer ?
  503. this.options.sanitizer(cap[0]) :
  504. escape(cap[0]) :
  505. cap[0]
  506. continue;
  507. }
  508. // link
  509. if (cap = this.rules.link.exec(src)) {
  510. src = src.substring(cap[0].length);
  511. this.inLink = true;
  512. out += this.outputLink(cap, {
  513. href: cap[2],
  514. title: cap[3]
  515. });
  516. this.inLink = false;
  517. continue;
  518. }
  519. // reflink, nolink
  520. if ((cap = this.rules.reflink.exec(src)) ||
  521. (cap = this.rules.nolink.exec(src))) {
  522. src = src.substring(cap[0].length);
  523. link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  524. link = this.links[link.toLowerCase()];
  525. if (!link || !link.href) {
  526. out += cap[0].charAt(0);
  527. src = cap[0].substring(1) + src;
  528. continue;
  529. }
  530. this.inLink = true;
  531. out += this.outputLink(cap, link);
  532. this.inLink = false;
  533. continue;
  534. }
  535. // strong
  536. if (cap = this.rules.strong.exec(src)) {
  537. src = src.substring(cap[0].length);
  538. out += this.renderer.strong(this.output(cap[2] || cap[1]));
  539. continue;
  540. }
  541. // em
  542. if (cap = this.rules.em.exec(src)) {
  543. src = src.substring(cap[0].length);
  544. out += this.renderer.em(this.output(cap[2] || cap[1]));
  545. continue;
  546. }
  547. // code
  548. if (cap = this.rules.code.exec(src)) {
  549. src = src.substring(cap[0].length);
  550. out += this.renderer.codespan(escape(cap[2], true));
  551. continue;
  552. }
  553. // br
  554. if (cap = this.rules.br.exec(src)) {
  555. src = src.substring(cap[0].length);
  556. out += this.renderer.br();
  557. continue;
  558. }
  559. // del (gfm)
  560. if (cap = this.rules.del.exec(src)) {
  561. src = src.substring(cap[0].length);
  562. out += this.renderer.del(this.output(cap[1]));
  563. continue;
  564. }
  565. // text
  566. if (cap = this.rules.text.exec(src)) {
  567. src = src.substring(cap[0].length);
  568. out += this.renderer.text(escape(this.smartypants(cap[0])));
  569. continue;
  570. }
  571. if (src) {
  572. throw new
  573. Error('Infinite loop on byte: ' + src.charCodeAt(0));
  574. }
  575. }
  576. return out;
  577. };
  578. /**
  579. * Compile Link
  580. */
  581. InlineLexer.prototype.outputLink = function(cap, link) {
  582. var href = escape(link.href),
  583. title = link.title ? escape(link.title) : null;
  584. return cap[0].charAt(0) !== '!' ?
  585. this.renderer.link(href, title, this.output(cap[1])) :
  586. this.renderer.image(href, title, escape(cap[1]));
  587. };
  588. /**
  589. * Smartypants Transformations
  590. */
  591. InlineLexer.prototype.smartypants = function(text) {
  592. if (!this.options.smartypants) return text;
  593. return text
  594. // em-dashes
  595. .replace(/---/g, '\u2014')
  596. // en-dashes
  597. .replace(/--/g, '\u2013')
  598. // opening singles
  599. .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
  600. // closing singles & apostrophes
  601. .replace(/'/g, '\u2019')
  602. // opening doubles
  603. .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
  604. // closing doubles
  605. .replace(/"/g, '\u201d')
  606. // ellipses
  607. .replace(/\.{3}/g, '\u2026');
  608. };
  609. /**
  610. * Mangle Links
  611. */
  612. InlineLexer.prototype.mangle = function(text) {
  613. if (!this.options.mangle) return text;
  614. var out = '',
  615. l = text.length,
  616. i = 0,
  617. ch;
  618. for (; i < l; i++) {
  619. ch = text.charCodeAt(i);
  620. if (Math.random() > 0.5) {
  621. ch = 'x' + ch.toString(16);
  622. }
  623. out += '&#' + ch + ';';
  624. }
  625. return out;
  626. };
  627. /**
  628. * Renderer
  629. */
  630. function Renderer(options) {
  631. this.options = options || {};
  632. }
  633. Renderer.prototype.code = function(code, lang, escaped) {
  634. if (this.options.highlight) {
  635. var out = this.options.highlight(code, lang);
  636. if (out != null && out !== code) {
  637. escaped = true;
  638. code = out;
  639. }
  640. }
  641. if (!lang) {
  642. return '<pre><code>' +
  643. (escaped ? code : escape(code, true)) +
  644. '\n</code></pre>';
  645. }
  646. return '<pre><code class="' +
  647. this.options.langPrefix +
  648. escape(lang, true) +
  649. '">' +
  650. (escaped ? code : escape(code, true)) +
  651. '\n</code></pre>\n';
  652. };
  653. Renderer.prototype.blockquote = function(quote) {
  654. return '<blockquote>\n' + quote + '</blockquote>\n';
  655. };
  656. Renderer.prototype.html = function(html) {
  657. return html;
  658. };
  659. Renderer.prototype.heading = function(text, level, raw) {
  660. return '<h' +
  661. level +
  662. ' id="' +
  663. this.options.headerPrefix +
  664. raw.toLowerCase().replace(/[^\w]+/g, '-') +
  665. '">' +
  666. text +
  667. '</h' +
  668. level +
  669. '>\n';
  670. };
  671. Renderer.prototype.hr = function() {
  672. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  673. };
  674. Renderer.prototype.list = function(body, ordered) {
  675. var type = ordered ? 'ol' : 'ul';
  676. return '<' + type + '>\n' + body + '</' + type + '>\n';
  677. };
  678. Renderer.prototype.listitem = function(text) {
  679. return '<li>' + text + '</li>\n';
  680. };
  681. Renderer.prototype.paragraph = function(text) {
  682. return '<p>' + text + '</p>\n';
  683. };
  684. Renderer.prototype.table = function(header, body) {
  685. return '<table>\n' +
  686. '<thead>\n' +
  687. header +
  688. '</thead>\n' +
  689. '<tbody>\n' +
  690. body +
  691. '</tbody>\n' +
  692. '</table>\n';
  693. };
  694. Renderer.prototype.tablerow = function(content) {
  695. return '<tr>\n' + content + '</tr>\n';
  696. };
  697. Renderer.prototype.tablecell = function(content, flags) {
  698. var type = flags.header ? 'th' : 'td';
  699. var tag = flags.align ?
  700. '<' + type + ' style="text-align:' + flags.align + '">' :
  701. '<' + type + '>';
  702. return tag + content + '</' + type + '>\n';
  703. };
  704. // span level renderer
  705. Renderer.prototype.strong = function(text) {
  706. return '<strong>' + text + '</strong>';
  707. };
  708. Renderer.prototype.em = function(text) {
  709. return '<em>' + text + '</em>';
  710. };
  711. Renderer.prototype.codespan = function(text) {
  712. return '<code>' + text + '</code>';
  713. };
  714. Renderer.prototype.br = function() {
  715. return this.options.xhtml ? '<br/>' : '<br>';
  716. };
  717. Renderer.prototype.del = function(text) {
  718. return '<del>' + text + '</del>';
  719. };
  720. Renderer.prototype.link = function(href, title, text) {
  721. if (this.options.sanitize) {
  722. try {
  723. var prot = decodeURIComponent(unescape(href))
  724. .replace(/[^\w:]/g, '')
  725. .toLowerCase();
  726. } catch (e) {
  727. return '';
  728. }
  729. if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0) {
  730. return '';
  731. }
  732. }
  733. var out = '<a href="' + href + '"';
  734. if (title) {
  735. out += ' title="' + title + '"';
  736. }
  737. out += '>' + text + '</a>';
  738. return out;
  739. };
  740. Renderer.prototype.image = function(href, title, text) {
  741. var out = '<img src="' + href + '" alt="' + text + '"';
  742. if (title) {
  743. out += ' title="' + title + '"';
  744. }
  745. out += this.options.xhtml ? '/>' : '>';
  746. return out;
  747. };
  748. Renderer.prototype.text = function(text) {
  749. return text;
  750. };
  751. /**
  752. * Parsing & Compiling
  753. */
  754. function Parser(options) {
  755. this.tokens = [];
  756. this.token = null;
  757. this.options = options || marked.defaults;
  758. this.options.renderer = this.options.renderer || new Renderer;
  759. this.renderer = this.options.renderer;
  760. this.renderer.options = this.options;
  761. }
  762. /**
  763. * Static Parse Method
  764. */
  765. Parser.parse = function(src, options, renderer) {
  766. var parser = new Parser(options, renderer);
  767. return parser.parse(src);
  768. };
  769. /**
  770. * Parse Loop
  771. */
  772. Parser.prototype.parse = function(src) {
  773. this.inline = new InlineLexer(src.links, this.options, this.renderer);
  774. this.tokens = src.reverse();
  775. var out = '';
  776. while (this.next()) {
  777. out += this.tok();
  778. }
  779. return out;
  780. };
  781. /**
  782. * Next Token
  783. */
  784. Parser.prototype.next = function() {
  785. return this.token = this.tokens.pop();
  786. };
  787. /**
  788. * Preview Next Token
  789. */
  790. Parser.prototype.peek = function() {
  791. return this.tokens[this.tokens.length - 1] || 0;
  792. };
  793. /**
  794. * Parse Text Tokens
  795. */
  796. Parser.prototype.parseText = function() {
  797. var body = this.token.text;
  798. while (this.peek().type === 'text') {
  799. body += '\n' + this.next().text;
  800. }
  801. return this.inline.output(body);
  802. };
  803. /**
  804. * Parse Current Token
  805. */
  806. Parser.prototype.tok = function() {
  807. switch (this.token.type) {
  808. case 'space':
  809. {
  810. return '';
  811. }
  812. case 'hr':
  813. {
  814. return this.renderer.hr();
  815. }
  816. case 'heading':
  817. {
  818. return this.renderer.heading(
  819. this.inline.output(this.token.text),
  820. this.token.depth,
  821. this.token.text);
  822. }
  823. case 'code':
  824. {
  825. return this.renderer.code(this.token.text,
  826. this.token.lang,
  827. this.token.escaped);
  828. }
  829. case 'table':
  830. {
  831. var header = '',
  832. body = '',
  833. i, row, cell, flags, j;
  834. // header
  835. cell = '';
  836. for (i = 0; i < this.token.header.length; i++) {
  837. flags = { header: true, align: this.token.align[i] };
  838. cell += this.renderer.tablecell(
  839. this.inline.output(this.token.header[i]), { header: true, align: this.token.align[i] }
  840. );
  841. }
  842. header += this.renderer.tablerow(cell);
  843. for (i = 0; i < this.token.cells.length; i++) {
  844. row = this.token.cells[i];
  845. cell = '';
  846. for (j = 0; j < row.length; j++) {
  847. cell += this.renderer.tablecell(
  848. this.inline.output(row[j]), { header: false, align: this.token.align[j] }
  849. );
  850. }
  851. body += this.renderer.tablerow(cell);
  852. }
  853. return this.renderer.table(header, body);
  854. }
  855. case 'blockquote_start':
  856. {
  857. var body = '';
  858. while (this.next().type !== 'blockquote_end') {
  859. body += this.tok();
  860. }
  861. return this.renderer.blockquote(body);
  862. }
  863. case 'list_start':
  864. {
  865. var body = '',
  866. ordered = this.token.ordered;
  867. while (this.next().type !== 'list_end') {
  868. body += this.tok();
  869. }
  870. return this.renderer.list(body, ordered);
  871. }
  872. case 'list_item_start':
  873. {
  874. var body = '';
  875. while (this.next().type !== 'list_item_end') {
  876. body += this.token.type === 'text' ?
  877. this.parseText() :
  878. this.tok();
  879. }
  880. return this.renderer.listitem(body);
  881. }
  882. case 'loose_item_start':
  883. {
  884. var body = '';
  885. while (this.next().type !== 'list_item_end') {
  886. body += this.tok();
  887. }
  888. return this.renderer.listitem(body);
  889. }
  890. case 'html':
  891. {
  892. var html = !this.token.pre && !this.options.pedantic ?
  893. this.inline.output(this.token.text) :
  894. this.token.text;
  895. return this.renderer.html(html);
  896. }
  897. case 'paragraph':
  898. {
  899. return this.renderer.paragraph(this.inline.output(this.token.text));
  900. }
  901. case 'text':
  902. {
  903. return this.renderer.paragraph(this.parseText());
  904. }
  905. }
  906. };
  907. /**
  908. * Helpers
  909. */
  910. function escape(html, encode) {
  911. return html
  912. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  913. .replace(/</g, '&lt;')
  914. .replace(/>/g, '&gt;')
  915. .replace(/"/g, '&quot;')
  916. .replace(/'/g, '&#39;');
  917. }
  918. function unescape(html) {
  919. // explicitly match decimal, hex, and named HTML entities
  920. return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
  921. n = n.toLowerCase();
  922. if (n === 'colon') return ':';
  923. if (n.charAt(0) === '#') {
  924. return n.charAt(1) === 'x' ?
  925. String.fromCharCode(parseInt(n.substring(2), 16)) :
  926. String.fromCharCode(+n.substring(1));
  927. }
  928. return '';
  929. });
  930. }
  931. function replace(regex, opt) {
  932. regex = regex.source;
  933. opt = opt || '';
  934. return function self(name, val) {
  935. if (!name) return new RegExp(regex, opt);
  936. val = val.source || val;
  937. val = val.replace(/(^|[^\[])\^/g, '$1');
  938. regex = regex.replace(name, val);
  939. return self;
  940. };
  941. }
  942. function noop() {}
  943. noop.exec = noop;
  944. function merge(obj) {
  945. var i = 1,
  946. target, key;
  947. for (; i < arguments.length; i++) {
  948. target = arguments[i];
  949. for (key in target) {
  950. if (Object.prototype.hasOwnProperty.call(target, key)) {
  951. obj[key] = target[key];
  952. }
  953. }
  954. }
  955. return obj;
  956. }
  957. /**
  958. * Marked
  959. */
  960. function marked(src, opt, callback) {
  961. if (callback || typeof opt === 'function') {
  962. if (!callback) {
  963. callback = opt;
  964. opt = null;
  965. }
  966. opt = merge({}, marked.defaults, opt || {});
  967. var highlight = opt.highlight,
  968. tokens, pending, i = 0;
  969. try {
  970. tokens = Lexer.lex(src, opt)
  971. } catch (e) {
  972. return callback(e);
  973. }
  974. pending = tokens.length;
  975. var done = function(err) {
  976. if (err) {
  977. opt.highlight = highlight;
  978. return callback(err);
  979. }
  980. var out;
  981. try {
  982. out = Parser.parse(tokens, opt);
  983. } catch (e) {
  984. err = e;
  985. }
  986. opt.highlight = highlight;
  987. return err ?
  988. callback(err) :
  989. callback(null, out);
  990. };
  991. if (!highlight || highlight.length < 3) {
  992. return done();
  993. }
  994. delete opt.highlight;
  995. if (!pending) return done();
  996. for (; i < tokens.length; i++) {
  997. (function(token) {
  998. if (token.type !== 'code') {
  999. return --pending || done();
  1000. }
  1001. return highlight(token.text, token.lang, function(err, code) {
  1002. if (err) return done(err);
  1003. if (code == null || code === token.text) {
  1004. return --pending || done();
  1005. }
  1006. token.text = code;
  1007. token.escaped = true;
  1008. --pending || done();
  1009. });
  1010. })(tokens[i]);
  1011. }
  1012. return;
  1013. }
  1014. try {
  1015. if (opt) opt = merge({}, marked.defaults, opt);
  1016. return Parser.parse(Lexer.lex(src, opt), opt);
  1017. } catch (e) {
  1018. e.message += '\nPlease report this to https://github.com/chjj/marked.';
  1019. if ((opt || marked.defaults).silent) {
  1020. return '<p>An error occured:</p><pre>' +
  1021. escape(e.message + '', true) +
  1022. '</pre>';
  1023. }
  1024. throw e;
  1025. }
  1026. }
  1027. /**
  1028. * Options
  1029. */
  1030. marked.options =
  1031. marked.setOptions = function(opt) {
  1032. merge(marked.defaults, opt);
  1033. return marked;
  1034. };
  1035. marked.defaults = {
  1036. gfm: true,
  1037. tables: true,
  1038. breaks: false,
  1039. pedantic: false,
  1040. sanitize: false,
  1041. sanitizer: null,
  1042. mangle: true,
  1043. smartLists: false,
  1044. silent: false,
  1045. highlight: null,
  1046. langPrefix: 'lang-',
  1047. smartypants: false,
  1048. headerPrefix: '',
  1049. renderer: new Renderer,
  1050. xhtml: false
  1051. };
  1052. /**
  1053. * Expose
  1054. */
  1055. marked.Parser = Parser;
  1056. marked.parser = Parser.parse;
  1057. marked.Renderer = Renderer;
  1058. marked.Lexer = Lexer;
  1059. marked.lexer = Lexer.lex;
  1060. marked.InlineLexer = InlineLexer;
  1061. marked.inlineLexer = InlineLexer.output;
  1062. marked.parse = marked;
  1063. if (typeof module !== 'undefined' && typeof exports === 'object') {
  1064. module.exports = marked;
  1065. } else if (typeof define === 'function' && define.amd) {
  1066. define(function() { return marked; });
  1067. } else {
  1068. this.marked = marked;
  1069. }
  1070. }).call(function() {
  1071. return this || (typeof window !== 'undefined' ? window : global);
  1072. }());