Tokenizer.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. import { defaults } from './defaults.js';
  2. import {
  3. rtrim,
  4. splitCells,
  5. escape,
  6. findClosingBracket
  7. } from './helpers.js';
  8. function outputLink(cap, link, raw, lexer) {
  9. const href = link.href;
  10. const title = link.title ? escape(link.title) : null;
  11. const text = cap[1].replace(/\\([\[\]])/g, '$1');
  12. if (cap[0].charAt(0) !== '!') {
  13. lexer.state.inLink = true;
  14. const token = {
  15. type: 'link',
  16. raw,
  17. href,
  18. title,
  19. text,
  20. tokens: lexer.inlineTokens(text, [])
  21. };
  22. lexer.state.inLink = false;
  23. return token;
  24. } else {
  25. return {
  26. type: 'image',
  27. raw,
  28. href,
  29. title,
  30. text: escape(text)
  31. };
  32. }
  33. }
  34. function indentCodeCompensation(raw, text) {
  35. const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
  36. if (matchIndentToCode === null) {
  37. return text;
  38. }
  39. const indentToCode = matchIndentToCode[1];
  40. return text
  41. .split('\n')
  42. .map(node => {
  43. const matchIndentInNode = node.match(/^\s+/);
  44. if (matchIndentInNode === null) {
  45. return node;
  46. }
  47. const [indentInNode] = matchIndentInNode;
  48. if (indentInNode.length >= indentToCode.length) {
  49. return node.slice(indentToCode.length);
  50. }
  51. return node;
  52. })
  53. .join('\n');
  54. }
  55. /**
  56. * Tokenizer
  57. */
  58. export class Tokenizer {
  59. constructor(options) {
  60. this.options = options || defaults;
  61. }
  62. space(src) {
  63. const cap = this.rules.block.newline.exec(src);
  64. if (cap && cap[0].length > 0) {
  65. return {
  66. type: 'space',
  67. raw: cap[0]
  68. };
  69. }
  70. }
  71. code(src) {
  72. const cap = this.rules.block.code.exec(src);
  73. if (cap) {
  74. const text = cap[0].replace(/^ {1,4}/gm, '');
  75. return {
  76. type: 'code',
  77. raw: cap[0],
  78. codeBlockStyle: 'indented',
  79. text: !this.options.pedantic
  80. ? rtrim(text, '\n')
  81. : text
  82. };
  83. }
  84. }
  85. fences(src) {
  86. const cap = this.rules.block.fences.exec(src);
  87. if (cap) {
  88. const raw = cap[0];
  89. const text = indentCodeCompensation(raw, cap[3] || '');
  90. return {
  91. type: 'code',
  92. raw,
  93. lang: cap[2] ? cap[2].trim() : cap[2],
  94. text
  95. };
  96. }
  97. }
  98. heading(src) {
  99. const cap = this.rules.block.heading.exec(src);
  100. if (cap) {
  101. let text = cap[2].trim();
  102. // remove trailing #s
  103. if (/#$/.test(text)) {
  104. const trimmed = rtrim(text, '#');
  105. if (this.options.pedantic) {
  106. text = trimmed.trim();
  107. } else if (!trimmed || / $/.test(trimmed)) {
  108. // CommonMark requires space before trailing #s
  109. text = trimmed.trim();
  110. }
  111. }
  112. const token = {
  113. type: 'heading',
  114. raw: cap[0],
  115. depth: cap[1].length,
  116. text: text,
  117. tokens: []
  118. };
  119. this.lexer.inline(token.text, token.tokens);
  120. return token;
  121. }
  122. }
  123. hr(src) {
  124. const cap = this.rules.block.hr.exec(src);
  125. if (cap) {
  126. return {
  127. type: 'hr',
  128. raw: cap[0]
  129. };
  130. }
  131. }
  132. blockquote(src) {
  133. const cap = this.rules.block.blockquote.exec(src);
  134. if (cap) {
  135. const text = cap[0].replace(/^ *> ?/gm, '');
  136. return {
  137. type: 'blockquote',
  138. raw: cap[0],
  139. tokens: this.lexer.blockTokens(text, []),
  140. text
  141. };
  142. }
  143. }
  144. list(src) {
  145. let cap = this.rules.block.list.exec(src);
  146. if (cap) {
  147. let raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine,
  148. line, nextLine, rawLine, itemContents, endEarly;
  149. let bull = cap[1].trim();
  150. const isordered = bull.length > 1;
  151. const list = {
  152. type: 'list',
  153. raw: '',
  154. ordered: isordered,
  155. start: isordered ? +bull.slice(0, -1) : '',
  156. loose: false,
  157. items: []
  158. };
  159. bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
  160. if (this.options.pedantic) {
  161. bull = isordered ? bull : '[*+-]';
  162. }
  163. // Get next list item
  164. const itemRegex = new RegExp(`^( {0,3}${bull})((?: [^\\n]*)?(?:\\n|$))`);
  165. // Check if current bullet point can start a new List Item
  166. while (src) {
  167. endEarly = false;
  168. if (!(cap = itemRegex.exec(src))) {
  169. break;
  170. }
  171. if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
  172. break;
  173. }
  174. raw = cap[0];
  175. src = src.substring(raw.length);
  176. line = cap[2].split('\n', 1)[0];
  177. nextLine = src.split('\n', 1)[0];
  178. if (this.options.pedantic) {
  179. indent = 2;
  180. itemContents = line.trimLeft();
  181. } else {
  182. indent = cap[2].search(/[^ ]/); // Find first non-space char
  183. indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
  184. itemContents = line.slice(indent);
  185. indent += cap[1].length;
  186. }
  187. blankLine = false;
  188. if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
  189. raw += nextLine + '\n';
  190. src = src.substring(nextLine.length + 1);
  191. endEarly = true;
  192. }
  193. if (!endEarly) {
  194. const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
  195. // Check if following lines should be included in List Item
  196. while (src) {
  197. rawLine = src.split('\n', 1)[0];
  198. line = rawLine;
  199. // Re-align to follow commonmark nesting rules
  200. if (this.options.pedantic) {
  201. line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
  202. }
  203. // End list item if found start of new bullet
  204. if (nextBulletRegex.test(line)) {
  205. break;
  206. }
  207. if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
  208. itemContents += '\n' + line.slice(indent);
  209. } else if (!blankLine) { // Until blank line, item doesn't need indentation
  210. itemContents += '\n' + line;
  211. } else { // Otherwise, improper indentation ends this item
  212. break;
  213. }
  214. if (!blankLine && !line.trim()) { // Check if current line is blank
  215. blankLine = true;
  216. }
  217. raw += rawLine + '\n';
  218. src = src.substring(rawLine.length + 1);
  219. }
  220. }
  221. if (!list.loose) {
  222. // If the previous item ended with a blank line, the list is loose
  223. if (endsWithBlankLine) {
  224. list.loose = true;
  225. } else if (/\n *\n *$/.test(raw)) {
  226. endsWithBlankLine = true;
  227. }
  228. }
  229. // Check for task list items
  230. if (this.options.gfm) {
  231. istask = /^\[[ xX]\] /.exec(itemContents);
  232. if (istask) {
  233. ischecked = istask[0] !== '[ ] ';
  234. itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
  235. }
  236. }
  237. list.items.push({
  238. type: 'list_item',
  239. raw: raw,
  240. task: !!istask,
  241. checked: ischecked,
  242. loose: false,
  243. text: itemContents
  244. });
  245. list.raw += raw;
  246. }
  247. // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
  248. list.items[list.items.length - 1].raw = raw.trimRight();
  249. list.items[list.items.length - 1].text = itemContents.trimRight();
  250. list.raw = list.raw.trimRight();
  251. const l = list.items.length;
  252. // Item child tokens handled here at end because we needed to have the final item to trim it first
  253. for (i = 0; i < l; i++) {
  254. this.lexer.state.top = false;
  255. list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
  256. const spacers = list.items[i].tokens.filter(t => t.type === 'space');
  257. const hasMultipleLineBreaks = spacers.every(t => {
  258. const chars = t.raw.split('');
  259. let lineBreaks = 0;
  260. for (const char of chars) {
  261. if (char === '\n') {
  262. lineBreaks += 1;
  263. }
  264. if (lineBreaks > 1) {
  265. return true;
  266. }
  267. }
  268. return false;
  269. });
  270. if (!list.loose && spacers.length && hasMultipleLineBreaks) {
  271. // Having a single line break doesn't mean a list is loose. A single line break is terminating the last list item
  272. list.loose = true;
  273. list.items[i].loose = true;
  274. }
  275. }
  276. return list;
  277. }
  278. }
  279. html(src) {
  280. const cap = this.rules.block.html.exec(src);
  281. if (cap) {
  282. const token = {
  283. type: 'html',
  284. raw: cap[0],
  285. pre: !this.options.sanitizer
  286. && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
  287. text: cap[0]
  288. };
  289. if (this.options.sanitize) {
  290. token.type = 'paragraph';
  291. token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
  292. token.tokens = [];
  293. this.lexer.inline(token.text, token.tokens);
  294. }
  295. return token;
  296. }
  297. }
  298. def(src) {
  299. const cap = this.rules.block.def.exec(src);
  300. if (cap) {
  301. if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
  302. const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
  303. return {
  304. type: 'def',
  305. tag,
  306. raw: cap[0],
  307. href: cap[2],
  308. title: cap[3]
  309. };
  310. }
  311. }
  312. table(src) {
  313. const cap = this.rules.block.table.exec(src);
  314. if (cap) {
  315. const item = {
  316. type: 'table',
  317. header: splitCells(cap[1]).map(c => { return { text: c }; }),
  318. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  319. rows: cap[3] ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
  320. };
  321. if (item.header.length === item.align.length) {
  322. item.raw = cap[0];
  323. let l = item.align.length;
  324. let i, j, k, row;
  325. for (i = 0; i < l; i++) {
  326. if (/^ *-+: *$/.test(item.align[i])) {
  327. item.align[i] = 'right';
  328. } else if (/^ *:-+: *$/.test(item.align[i])) {
  329. item.align[i] = 'center';
  330. } else if (/^ *:-+ *$/.test(item.align[i])) {
  331. item.align[i] = 'left';
  332. } else {
  333. item.align[i] = null;
  334. }
  335. }
  336. l = item.rows.length;
  337. for (i = 0; i < l; i++) {
  338. item.rows[i] = splitCells(item.rows[i], item.header.length).map(c => { return { text: c }; });
  339. }
  340. // parse child tokens inside headers and cells
  341. // header child tokens
  342. l = item.header.length;
  343. for (j = 0; j < l; j++) {
  344. item.header[j].tokens = [];
  345. this.lexer.inlineTokens(item.header[j].text, item.header[j].tokens);
  346. }
  347. // cell child tokens
  348. l = item.rows.length;
  349. for (j = 0; j < l; j++) {
  350. row = item.rows[j];
  351. for (k = 0; k < row.length; k++) {
  352. row[k].tokens = [];
  353. this.lexer.inlineTokens(row[k].text, row[k].tokens);
  354. }
  355. }
  356. return item;
  357. }
  358. }
  359. }
  360. lheading(src) {
  361. const cap = this.rules.block.lheading.exec(src);
  362. if (cap) {
  363. const token = {
  364. type: 'heading',
  365. raw: cap[0],
  366. depth: cap[2].charAt(0) === '=' ? 1 : 2,
  367. text: cap[1],
  368. tokens: []
  369. };
  370. this.lexer.inline(token.text, token.tokens);
  371. return token;
  372. }
  373. }
  374. paragraph(src) {
  375. const cap = this.rules.block.paragraph.exec(src);
  376. if (cap) {
  377. const token = {
  378. type: 'paragraph',
  379. raw: cap[0],
  380. text: cap[1].charAt(cap[1].length - 1) === '\n'
  381. ? cap[1].slice(0, -1)
  382. : cap[1],
  383. tokens: []
  384. };
  385. this.lexer.inline(token.text, token.tokens);
  386. return token;
  387. }
  388. }
  389. text(src) {
  390. const cap = this.rules.block.text.exec(src);
  391. if (cap) {
  392. const token = {
  393. type: 'text',
  394. raw: cap[0],
  395. text: cap[0],
  396. tokens: []
  397. };
  398. this.lexer.inline(token.text, token.tokens);
  399. return token;
  400. }
  401. }
  402. escape(src) {
  403. const cap = this.rules.inline.escape.exec(src);
  404. if (cap) {
  405. return {
  406. type: 'escape',
  407. raw: cap[0],
  408. text: escape(cap[1])
  409. };
  410. }
  411. }
  412. tag(src) {
  413. const cap = this.rules.inline.tag.exec(src);
  414. if (cap) {
  415. if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
  416. this.lexer.state.inLink = true;
  417. } else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
  418. this.lexer.state.inLink = false;
  419. }
  420. if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  421. this.lexer.state.inRawBlock = true;
  422. } else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  423. this.lexer.state.inRawBlock = false;
  424. }
  425. return {
  426. type: this.options.sanitize
  427. ? 'text'
  428. : 'html',
  429. raw: cap[0],
  430. inLink: this.lexer.state.inLink,
  431. inRawBlock: this.lexer.state.inRawBlock,
  432. text: this.options.sanitize
  433. ? (this.options.sanitizer
  434. ? this.options.sanitizer(cap[0])
  435. : escape(cap[0]))
  436. : cap[0]
  437. };
  438. }
  439. }
  440. link(src) {
  441. const cap = this.rules.inline.link.exec(src);
  442. if (cap) {
  443. const trimmedUrl = cap[2].trim();
  444. if (!this.options.pedantic && /^</.test(trimmedUrl)) {
  445. // commonmark requires matching angle brackets
  446. if (!(/>$/.test(trimmedUrl))) {
  447. return;
  448. }
  449. // ending angle bracket cannot be escaped
  450. const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
  451. if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
  452. return;
  453. }
  454. } else {
  455. // find closing parenthesis
  456. const lastParenIndex = findClosingBracket(cap[2], '()');
  457. if (lastParenIndex > -1) {
  458. const start = cap[0].indexOf('!') === 0 ? 5 : 4;
  459. const linkLen = start + cap[1].length + lastParenIndex;
  460. cap[2] = cap[2].substring(0, lastParenIndex);
  461. cap[0] = cap[0].substring(0, linkLen).trim();
  462. cap[3] = '';
  463. }
  464. }
  465. let href = cap[2];
  466. let title = '';
  467. if (this.options.pedantic) {
  468. // split pedantic href and title
  469. const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
  470. if (link) {
  471. href = link[1];
  472. title = link[3];
  473. }
  474. } else {
  475. title = cap[3] ? cap[3].slice(1, -1) : '';
  476. }
  477. href = href.trim();
  478. if (/^</.test(href)) {
  479. if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
  480. // pedantic allows starting angle bracket without ending angle bracket
  481. href = href.slice(1);
  482. } else {
  483. href = href.slice(1, -1);
  484. }
  485. }
  486. return outputLink(cap, {
  487. href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
  488. title: title ? title.replace(this.rules.inline._escapes, '$1') : title
  489. }, cap[0], this.lexer);
  490. }
  491. }
  492. reflink(src, links) {
  493. let cap;
  494. if ((cap = this.rules.inline.reflink.exec(src))
  495. || (cap = this.rules.inline.nolink.exec(src))) {
  496. let link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  497. link = links[link.toLowerCase()];
  498. if (!link || !link.href) {
  499. const text = cap[0].charAt(0);
  500. return {
  501. type: 'text',
  502. raw: text,
  503. text
  504. };
  505. }
  506. return outputLink(cap, link, cap[0], this.lexer);
  507. }
  508. }
  509. emStrong(src, maskedSrc, prevChar = '') {
  510. let match = this.rules.inline.emStrong.lDelim.exec(src);
  511. if (!match) return;
  512. // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
  513. if (match[3] && prevChar.match(/[\p{L}\p{N}]/u)) return;
  514. const nextChar = match[1] || match[2] || '';
  515. if (!nextChar || (nextChar && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
  516. const lLength = match[0].length - 1;
  517. let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
  518. const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
  519. endReg.lastIndex = 0;
  520. // Clip maskedSrc to same section of string as src (move to lexer?)
  521. maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
  522. while ((match = endReg.exec(maskedSrc)) != null) {
  523. rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
  524. if (!rDelim) continue; // skip single * in __abc*abc__
  525. rLength = rDelim.length;
  526. if (match[3] || match[4]) { // found another Left Delim
  527. delimTotal += rLength;
  528. continue;
  529. } else if (match[5] || match[6]) { // either Left or Right Delim
  530. if (lLength % 3 && !((lLength + rLength) % 3)) {
  531. midDelimTotal += rLength;
  532. continue; // CommonMark Emphasis Rules 9-10
  533. }
  534. }
  535. delimTotal -= rLength;
  536. if (delimTotal > 0) continue; // Haven't found enough closing delimiters
  537. // Remove extra characters. *a*** -> *a*
  538. rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
  539. // Create `em` if smallest delimiter has odd char count. *a***
  540. if (Math.min(lLength, rLength) % 2) {
  541. const text = src.slice(1, lLength + match.index + rLength);
  542. return {
  543. type: 'em',
  544. raw: src.slice(0, lLength + match.index + rLength + 1),
  545. text,
  546. tokens: this.lexer.inlineTokens(text, [])
  547. };
  548. }
  549. // Create 'strong' if smallest delimiter has even char count. **a***
  550. const text = src.slice(2, lLength + match.index + rLength - 1);
  551. return {
  552. type: 'strong',
  553. raw: src.slice(0, lLength + match.index + rLength + 1),
  554. text,
  555. tokens: this.lexer.inlineTokens(text, [])
  556. };
  557. }
  558. }
  559. }
  560. codespan(src) {
  561. const cap = this.rules.inline.code.exec(src);
  562. if (cap) {
  563. let text = cap[2].replace(/\n/g, ' ');
  564. const hasNonSpaceChars = /[^ ]/.test(text);
  565. const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
  566. if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
  567. text = text.substring(1, text.length - 1);
  568. }
  569. text = escape(text, true);
  570. return {
  571. type: 'codespan',
  572. raw: cap[0],
  573. text
  574. };
  575. }
  576. }
  577. br(src) {
  578. const cap = this.rules.inline.br.exec(src);
  579. if (cap) {
  580. return {
  581. type: 'br',
  582. raw: cap[0]
  583. };
  584. }
  585. }
  586. del(src) {
  587. const cap = this.rules.inline.del.exec(src);
  588. if (cap) {
  589. return {
  590. type: 'del',
  591. raw: cap[0],
  592. text: cap[2],
  593. tokens: this.lexer.inlineTokens(cap[2], [])
  594. };
  595. }
  596. }
  597. autolink(src, mangle) {
  598. const cap = this.rules.inline.autolink.exec(src);
  599. if (cap) {
  600. let text, href;
  601. if (cap[2] === '@') {
  602. text = escape(this.options.mangle ? mangle(cap[1]) : cap[1]);
  603. href = 'mailto:' + text;
  604. } else {
  605. text = escape(cap[1]);
  606. href = text;
  607. }
  608. return {
  609. type: 'link',
  610. raw: cap[0],
  611. text,
  612. href,
  613. tokens: [
  614. {
  615. type: 'text',
  616. raw: text,
  617. text
  618. }
  619. ]
  620. };
  621. }
  622. }
  623. url(src, mangle) {
  624. let cap;
  625. if (cap = this.rules.inline.url.exec(src)) {
  626. let text, href;
  627. if (cap[2] === '@') {
  628. text = escape(this.options.mangle ? mangle(cap[0]) : cap[0]);
  629. href = 'mailto:' + text;
  630. } else {
  631. // do extended autolink path validation
  632. let prevCapZero;
  633. do {
  634. prevCapZero = cap[0];
  635. cap[0] = this.rules.inline._backpedal.exec(cap[0])[0];
  636. } while (prevCapZero !== cap[0]);
  637. text = escape(cap[0]);
  638. if (cap[1] === 'www.') {
  639. href = 'http://' + text;
  640. } else {
  641. href = text;
  642. }
  643. }
  644. return {
  645. type: 'link',
  646. raw: cap[0],
  647. text,
  648. href,
  649. tokens: [
  650. {
  651. type: 'text',
  652. raw: text,
  653. text
  654. }
  655. ]
  656. };
  657. }
  658. }
  659. inlineText(src, smartypants) {
  660. const cap = this.rules.inline.text.exec(src);
  661. if (cap) {
  662. let text;
  663. if (this.lexer.state.inRawBlock) {
  664. text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0];
  665. } else {
  666. text = escape(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
  667. }
  668. return {
  669. type: 'text',
  670. raw: cap[0],
  671. text
  672. };
  673. }
  674. }
  675. }