You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1272 lines
28 KiB

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