marked.js 32 KB

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