markdownRenderer.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import * as DOM from './dom.js';
  6. import * as dompurify from './dompurify/dompurify.js';
  7. import { DomEmitter } from './event.js';
  8. import { createElement } from './formattedTextRenderer.js';
  9. import { StandardKeyboardEvent } from './keyboardEvent.js';
  10. import { StandardMouseEvent } from './mouseEvent.js';
  11. import { renderLabelWithIcons } from './ui/iconLabel/iconLabels.js';
  12. import { onUnexpectedError } from '../common/errors.js';
  13. import { Event } from '../common/event.js';
  14. import { escapeDoubleQuotes, parseHrefAndDimensions, removeMarkdownEscapes } from '../common/htmlContent.js';
  15. import { markdownEscapeEscapedIcons } from '../common/iconLabels.js';
  16. import { defaultGenerator } from '../common/idGenerator.js';
  17. import { Lazy } from '../common/lazy.js';
  18. import { DisposableStore } from '../common/lifecycle.js';
  19. import { marked } from '../common/marked/marked.js';
  20. import { parse } from '../common/marshalling.js';
  21. import { FileAccess, Schemas } from '../common/network.js';
  22. import { cloneAndChange } from '../common/objects.js';
  23. import { dirname, resolvePath } from '../common/resources.js';
  24. import { escape } from '../common/strings.js';
  25. import { URI } from '../common/uri.js';
  26. const defaultMarkedRenderers = Object.freeze({
  27. image: (href, title, text) => {
  28. let dimensions = [];
  29. let attributes = [];
  30. if (href) {
  31. ({ href, dimensions } = parseHrefAndDimensions(href));
  32. attributes.push(`src="${escapeDoubleQuotes(href)}"`);
  33. }
  34. if (text) {
  35. attributes.push(`alt="${escapeDoubleQuotes(text)}"`);
  36. }
  37. if (title) {
  38. attributes.push(`title="${escapeDoubleQuotes(title)}"`);
  39. }
  40. if (dimensions.length) {
  41. attributes = attributes.concat(dimensions);
  42. }
  43. return '<img ' + attributes.join(' ') + '>';
  44. },
  45. paragraph: (text) => {
  46. return `<p>${text}</p>`;
  47. },
  48. link: (href, title, text) => {
  49. if (typeof href !== 'string') {
  50. return '';
  51. }
  52. // Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
  53. if (href === text) { // raw link case
  54. text = removeMarkdownEscapes(text);
  55. }
  56. title = typeof title === 'string' ? escapeDoubleQuotes(removeMarkdownEscapes(title)) : '';
  57. href = removeMarkdownEscapes(href);
  58. // HTML Encode href
  59. href = href.replace(/&/g, '&amp;')
  60. .replace(/</g, '&lt;')
  61. .replace(/>/g, '&gt;')
  62. .replace(/"/g, '&quot;')
  63. .replace(/'/g, '&#39;');
  64. return `<a href="${href}" title="${title || href}">${text}</a>`;
  65. },
  66. });
  67. /**
  68. * Low-level way create a html element from a markdown string.
  69. *
  70. * **Note** that for most cases you should be using [`MarkdownRenderer`](./src/vs/editor/contrib/markdownRenderer/browser/markdownRenderer.ts)
  71. * which comes with support for pretty code block rendering and which uses the default way of handling links.
  72. */
  73. export function renderMarkdown(markdown, options = {}, markedOptions = {}) {
  74. var _a, _b;
  75. const disposables = new DisposableStore();
  76. let isDisposed = false;
  77. const element = createElement(options);
  78. const _uriMassage = function (part) {
  79. let data;
  80. try {
  81. data = parse(decodeURIComponent(part));
  82. }
  83. catch (e) {
  84. // ignore
  85. }
  86. if (!data) {
  87. return part;
  88. }
  89. data = cloneAndChange(data, value => {
  90. if (markdown.uris && markdown.uris[value]) {
  91. return URI.revive(markdown.uris[value]);
  92. }
  93. else {
  94. return undefined;
  95. }
  96. });
  97. return encodeURIComponent(JSON.stringify(data));
  98. };
  99. const _href = function (href, isDomUri) {
  100. const data = markdown.uris && markdown.uris[href];
  101. let uri = URI.revive(data);
  102. if (isDomUri) {
  103. if (href.startsWith(Schemas.data + ':')) {
  104. return href;
  105. }
  106. if (!uri) {
  107. uri = URI.parse(href);
  108. }
  109. // this URI will end up as "src"-attribute of a dom node
  110. // and because of that special rewriting needs to be done
  111. // so that the URI uses a protocol that's understood by
  112. // browsers (like http or https)
  113. return FileAccess.uriToBrowserUri(uri).toString(true);
  114. }
  115. if (!uri) {
  116. return href;
  117. }
  118. if (URI.parse(href).toString() === uri.toString()) {
  119. return href; // no transformation performed
  120. }
  121. if (uri.query) {
  122. uri = uri.with({ query: _uriMassage(uri.query) });
  123. }
  124. return uri.toString();
  125. };
  126. const renderer = new marked.Renderer();
  127. renderer.image = defaultMarkedRenderers.image;
  128. renderer.link = defaultMarkedRenderers.link;
  129. renderer.paragraph = defaultMarkedRenderers.paragraph;
  130. // Will collect [id, renderedElement] tuples
  131. const codeBlocks = [];
  132. const syncCodeBlocks = [];
  133. if (options.codeBlockRendererSync) {
  134. renderer.code = (code, lang) => {
  135. const id = defaultGenerator.nextId();
  136. const value = options.codeBlockRendererSync(postProcessCodeBlockLanguageId(lang), code);
  137. syncCodeBlocks.push([id, value]);
  138. return `<div class="code" data-code="${id}">${escape(code)}</div>`;
  139. };
  140. }
  141. else if (options.codeBlockRenderer) {
  142. renderer.code = (code, lang) => {
  143. const id = defaultGenerator.nextId();
  144. const value = options.codeBlockRenderer(postProcessCodeBlockLanguageId(lang), code);
  145. codeBlocks.push(value.then(element => [id, element]));
  146. return `<div class="code" data-code="${id}">${escape(code)}</div>`;
  147. };
  148. }
  149. if (options.actionHandler) {
  150. const _activateLink = function (event) {
  151. let target = event.target;
  152. if (target.tagName !== 'A') {
  153. target = target.parentElement;
  154. if (!target || target.tagName !== 'A') {
  155. return;
  156. }
  157. }
  158. try {
  159. let href = target.dataset['href'];
  160. if (href) {
  161. if (markdown.baseUri) {
  162. href = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  163. }
  164. options.actionHandler.callback(href, event);
  165. }
  166. }
  167. catch (err) {
  168. onUnexpectedError(err);
  169. }
  170. finally {
  171. event.preventDefault();
  172. }
  173. };
  174. const onClick = options.actionHandler.disposables.add(new DomEmitter(element, 'click'));
  175. const onAuxClick = options.actionHandler.disposables.add(new DomEmitter(element, 'auxclick'));
  176. options.actionHandler.disposables.add(Event.any(onClick.event, onAuxClick.event)(e => {
  177. const mouseEvent = new StandardMouseEvent(e);
  178. if (!mouseEvent.leftButton && !mouseEvent.middleButton) {
  179. return;
  180. }
  181. _activateLink(mouseEvent);
  182. }));
  183. options.actionHandler.disposables.add(DOM.addDisposableListener(element, 'keydown', (e) => {
  184. const keyboardEvent = new StandardKeyboardEvent(e);
  185. if (!keyboardEvent.equals(10 /* KeyCode.Space */) && !keyboardEvent.equals(3 /* KeyCode.Enter */)) {
  186. return;
  187. }
  188. _activateLink(keyboardEvent);
  189. }));
  190. }
  191. if (!markdown.supportHtml) {
  192. // TODO: Can we deprecated this in favor of 'supportHtml'?
  193. // Use our own sanitizer so that we can let through only spans.
  194. // Otherwise, we'd be letting all html be rendered.
  195. // If we want to allow markdown permitted tags, then we can delete sanitizer and sanitize.
  196. // We always pass the output through dompurify after this so that we don't rely on
  197. // marked for sanitization.
  198. markedOptions.sanitizer = (html) => {
  199. const match = markdown.isTrusted ? html.match(/^(<span[^>]+>)|(<\/\s*span>)$/) : undefined;
  200. return match ? html : '';
  201. };
  202. markedOptions.sanitize = true;
  203. markedOptions.silent = true;
  204. }
  205. markedOptions.renderer = renderer;
  206. // values that are too long will freeze the UI
  207. let value = (_a = markdown.value) !== null && _a !== void 0 ? _a : '';
  208. if (value.length > 100000) {
  209. value = `${value.substr(0, 100000)}…`;
  210. }
  211. // escape theme icons
  212. if (markdown.supportThemeIcons) {
  213. value = markdownEscapeEscapedIcons(value);
  214. }
  215. let renderedMarkdown;
  216. if (options.fillInIncompleteTokens) {
  217. // The defaults are applied by parse but not lexer()/parser(), and they need to be present
  218. const opts = Object.assign(Object.assign({}, marked.defaults), markedOptions);
  219. const tokens = marked.lexer(value, opts);
  220. const newTokens = fillInIncompleteTokens(tokens);
  221. renderedMarkdown = marked.parser(newTokens, opts);
  222. }
  223. else {
  224. renderedMarkdown = marked.parse(value, markedOptions);
  225. }
  226. // Rewrite theme icons
  227. if (markdown.supportThemeIcons) {
  228. const elements = renderLabelWithIcons(renderedMarkdown);
  229. renderedMarkdown = elements.map(e => typeof e === 'string' ? e : e.outerHTML).join('');
  230. }
  231. const htmlParser = new DOMParser();
  232. const markdownHtmlDoc = htmlParser.parseFromString(sanitizeRenderedMarkdown(markdown, renderedMarkdown), 'text/html');
  233. markdownHtmlDoc.body.querySelectorAll('img')
  234. .forEach(img => {
  235. const src = img.getAttribute('src'); // Get the raw 'src' attribute value as text, not the resolved 'src'
  236. if (src) {
  237. let href = src;
  238. try {
  239. if (markdown.baseUri) { // absolute or relative local path, or file: uri
  240. href = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  241. }
  242. }
  243. catch (err) { }
  244. img.src = _href(href, true);
  245. }
  246. });
  247. markdownHtmlDoc.body.querySelectorAll('a')
  248. .forEach(a => {
  249. const href = a.getAttribute('href'); // Get the raw 'href' attribute value as text, not the resolved 'href'
  250. a.setAttribute('href', ''); // Clear out href. We use the `data-href` for handling clicks instead
  251. if (!href
  252. || /^data:|javascript:/i.test(href)
  253. || (/^command:/i.test(href) && !markdown.isTrusted)
  254. || /^command:(\/\/\/)?_workbench\.downloadResource/i.test(href)) {
  255. // drop the link
  256. a.replaceWith(...a.childNodes);
  257. }
  258. else {
  259. let resolvedHref = _href(href, false);
  260. if (markdown.baseUri) {
  261. resolvedHref = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  262. }
  263. a.dataset.href = resolvedHref;
  264. }
  265. });
  266. element.innerHTML = sanitizeRenderedMarkdown(markdown, markdownHtmlDoc.body.innerHTML);
  267. if (codeBlocks.length > 0) {
  268. Promise.all(codeBlocks).then((tuples) => {
  269. var _a, _b;
  270. if (isDisposed) {
  271. return;
  272. }
  273. const renderedElements = new Map(tuples);
  274. const placeholderElements = element.querySelectorAll(`div[data-code]`);
  275. for (const placeholderElement of placeholderElements) {
  276. const renderedElement = renderedElements.get((_a = placeholderElement.dataset['code']) !== null && _a !== void 0 ? _a : '');
  277. if (renderedElement) {
  278. DOM.reset(placeholderElement, renderedElement);
  279. }
  280. }
  281. (_b = options.asyncRenderCallback) === null || _b === void 0 ? void 0 : _b.call(options);
  282. });
  283. }
  284. else if (syncCodeBlocks.length > 0) {
  285. const renderedElements = new Map(syncCodeBlocks);
  286. const placeholderElements = element.querySelectorAll(`div[data-code]`);
  287. for (const placeholderElement of placeholderElements) {
  288. const renderedElement = renderedElements.get((_b = placeholderElement.dataset['code']) !== null && _b !== void 0 ? _b : '');
  289. if (renderedElement) {
  290. DOM.reset(placeholderElement, renderedElement);
  291. }
  292. }
  293. }
  294. // signal size changes for image tags
  295. if (options.asyncRenderCallback) {
  296. for (const img of element.getElementsByTagName('img')) {
  297. const listener = disposables.add(DOM.addDisposableListener(img, 'load', () => {
  298. listener.dispose();
  299. options.asyncRenderCallback();
  300. }));
  301. }
  302. }
  303. return {
  304. element,
  305. dispose: () => {
  306. isDisposed = true;
  307. disposables.dispose();
  308. }
  309. };
  310. }
  311. function postProcessCodeBlockLanguageId(lang) {
  312. if (!lang) {
  313. return '';
  314. }
  315. const parts = lang.split(/[\s+|:|,|\{|\?]/, 1);
  316. if (parts.length) {
  317. return parts[0];
  318. }
  319. return lang;
  320. }
  321. function resolveWithBaseUri(baseUri, href) {
  322. const hasScheme = /^\w[\w\d+.-]*:/.test(href);
  323. if (hasScheme) {
  324. return href;
  325. }
  326. if (baseUri.path.endsWith('/')) {
  327. return resolvePath(baseUri, href).toString();
  328. }
  329. else {
  330. return resolvePath(dirname(baseUri), href).toString();
  331. }
  332. }
  333. function sanitizeRenderedMarkdown(options, renderedMarkdown) {
  334. const { config, allowedSchemes } = getSanitizerOptions(options);
  335. dompurify.addHook('uponSanitizeAttribute', (element, e) => {
  336. if (e.attrName === 'style' || e.attrName === 'class') {
  337. if (element.tagName === 'SPAN') {
  338. if (e.attrName === 'style') {
  339. e.keepAttr = /^(color\:(#[0-9a-fA-F]+|var\(--vscode(-[a-zA-Z]+)+\));)?(background-color\:(#[0-9a-fA-F]+|var\(--vscode(-[a-zA-Z]+)+\));)?$/.test(e.attrValue);
  340. return;
  341. }
  342. else if (e.attrName === 'class') {
  343. e.keepAttr = /^codicon codicon-[a-z\-]+( codicon-modifier-[a-z\-]+)?$/.test(e.attrValue);
  344. return;
  345. }
  346. }
  347. e.keepAttr = false;
  348. return;
  349. }
  350. });
  351. const hook = DOM.hookDomPurifyHrefAndSrcSanitizer(allowedSchemes);
  352. try {
  353. return dompurify.sanitize(renderedMarkdown, Object.assign(Object.assign({}, config), { RETURN_TRUSTED_TYPE: true }));
  354. }
  355. finally {
  356. dompurify.removeHook('uponSanitizeAttribute');
  357. hook.dispose();
  358. }
  359. }
  360. export const allowedMarkdownAttr = [
  361. 'align',
  362. 'autoplay',
  363. 'alt',
  364. 'class',
  365. 'controls',
  366. 'data-code',
  367. 'data-href',
  368. 'height',
  369. 'href',
  370. 'loop',
  371. 'muted',
  372. 'playsinline',
  373. 'poster',
  374. 'src',
  375. 'style',
  376. 'target',
  377. 'title',
  378. 'width',
  379. 'start',
  380. ];
  381. function getSanitizerOptions(options) {
  382. const allowedSchemes = [
  383. Schemas.http,
  384. Schemas.https,
  385. Schemas.mailto,
  386. Schemas.data,
  387. Schemas.file,
  388. Schemas.vscodeFileResource,
  389. Schemas.vscodeRemote,
  390. Schemas.vscodeRemoteResource,
  391. ];
  392. if (options.isTrusted) {
  393. allowedSchemes.push(Schemas.command);
  394. }
  395. return {
  396. config: {
  397. // allowedTags should included everything that markdown renders to.
  398. // Since we have our own sanitize function for marked, it's possible we missed some tag so let dompurify make sure.
  399. // HTML tags that can result from markdown are from reading https://spec.commonmark.org/0.29/
  400. // HTML table tags that can result from markdown are from https://github.github.com/gfm/#tables-extension-
  401. ALLOWED_TAGS: [...DOM.basicMarkupHtmlTags],
  402. ALLOWED_ATTR: allowedMarkdownAttr,
  403. ALLOW_UNKNOWN_PROTOCOLS: true,
  404. },
  405. allowedSchemes
  406. };
  407. }
  408. /**
  409. * Strips all markdown from `string`, if it's an IMarkdownString. For example
  410. * `# Header` would be output as `Header`. If it's not, the string is returned.
  411. */
  412. export function renderStringAsPlaintext(string) {
  413. return typeof string === 'string' ? string : renderMarkdownAsPlaintext(string);
  414. }
  415. /**
  416. * Strips all markdown from `markdown`. For example `# Header` would be output as `Header`.
  417. */
  418. export function renderMarkdownAsPlaintext(markdown) {
  419. var _a;
  420. // values that are too long will freeze the UI
  421. let value = (_a = markdown.value) !== null && _a !== void 0 ? _a : '';
  422. if (value.length > 100000) {
  423. value = `${value.substr(0, 100000)}…`;
  424. }
  425. const html = marked.parse(value, { renderer: plainTextRenderer.value }).replace(/&(#\d+|[a-zA-Z]+);/g, m => { var _a; return (_a = unescapeInfo.get(m)) !== null && _a !== void 0 ? _a : m; });
  426. return sanitizeRenderedMarkdown({ isTrusted: false }, html).toString();
  427. }
  428. const unescapeInfo = new Map([
  429. ['&quot;', '"'],
  430. ['&nbsp;', ' '],
  431. ['&amp;', '&'],
  432. ['&#39;', '\''],
  433. ['&lt;', '<'],
  434. ['&gt;', '>'],
  435. ]);
  436. const plainTextRenderer = new Lazy(() => {
  437. const renderer = new marked.Renderer();
  438. renderer.code = (code) => {
  439. return code;
  440. };
  441. renderer.blockquote = (quote) => {
  442. return quote;
  443. };
  444. renderer.html = (_html) => {
  445. return '';
  446. };
  447. renderer.heading = (text, _level, _raw) => {
  448. return text + '\n';
  449. };
  450. renderer.hr = () => {
  451. return '';
  452. };
  453. renderer.list = (body, _ordered) => {
  454. return body;
  455. };
  456. renderer.listitem = (text) => {
  457. return text + '\n';
  458. };
  459. renderer.paragraph = (text) => {
  460. return text + '\n';
  461. };
  462. renderer.table = (header, body) => {
  463. return header + body + '\n';
  464. };
  465. renderer.tablerow = (content) => {
  466. return content;
  467. };
  468. renderer.tablecell = (content, _flags) => {
  469. return content + ' ';
  470. };
  471. renderer.strong = (text) => {
  472. return text;
  473. };
  474. renderer.em = (text) => {
  475. return text;
  476. };
  477. renderer.codespan = (code) => {
  478. return code;
  479. };
  480. renderer.br = () => {
  481. return '\n';
  482. };
  483. renderer.del = (text) => {
  484. return text;
  485. };
  486. renderer.image = (_href, _title, _text) => {
  487. return '';
  488. };
  489. renderer.text = (text) => {
  490. return text;
  491. };
  492. renderer.link = (_href, _title, text) => {
  493. return text;
  494. };
  495. return renderer;
  496. });
  497. function mergeRawTokenText(tokens) {
  498. let mergedTokenText = '';
  499. tokens.forEach(token => {
  500. mergedTokenText += token.raw;
  501. });
  502. return mergedTokenText;
  503. }
  504. function completeSingleLinePattern(token) {
  505. const subtoken = token.tokens[0];
  506. if (subtoken.type === 'text') {
  507. const lines = subtoken.raw.split('\n');
  508. const lastLine = lines[lines.length - 1];
  509. if (lastLine.includes('`')) {
  510. return completeCodespan(token);
  511. }
  512. else if (lastLine.includes('**')) {
  513. return completeDoublestar(token);
  514. }
  515. else if (lastLine.match(/\*\w/)) {
  516. return completeStar(token);
  517. }
  518. else if (lastLine.match(/(^|\s)__\w/)) {
  519. return completeDoubleUnderscore(token);
  520. }
  521. else if (lastLine.match(/(^|\s)_\w/)) {
  522. return completeUnderscore(token);
  523. }
  524. else if (lastLine.match(/(^|\s)\[.*\]\(\w*/)) {
  525. return completeLinkTarget(token);
  526. }
  527. else if (lastLine.match(/(^|\s)\[\w/)) {
  528. return completeLinkText(token);
  529. }
  530. }
  531. return undefined;
  532. }
  533. // function completeListItemPattern(token: marked.Tokens.List): marked.Tokens.List | undefined {
  534. // // Patch up this one list item
  535. // const lastItem = token.items[token.items.length - 1];
  536. // const newList = completeSingleLinePattern(lastItem);
  537. // if (!newList || newList.type !== 'list') {
  538. // // Nothing to fix, or not a pattern we were expecting
  539. // return;
  540. // }
  541. // // Re-parse the whole list with the last item replaced
  542. // const completeList = marked.lexer(mergeRawTokenText(token.items.slice(0, token.items.length - 1)) + newList.items[0].raw);
  543. // if (completeList.length === 1 && completeList[0].type === 'list') {
  544. // return completeList[0];
  545. // }
  546. // // Not a pattern we were expecting
  547. // return undefined;
  548. // }
  549. export function fillInIncompleteTokens(tokens) {
  550. let i;
  551. let newTokens;
  552. for (i = 0; i < tokens.length; i++) {
  553. const token = tokens[i];
  554. if (token.type === 'paragraph' && token.raw.match(/(\n|^)```/)) {
  555. // If the code block was complete, it would be in a type='code'
  556. newTokens = completeCodeBlock(tokens.slice(i));
  557. break;
  558. }
  559. if (token.type === 'paragraph' && token.raw.match(/(\n|^)\|/)) {
  560. newTokens = completeTable(tokens.slice(i));
  561. break;
  562. }
  563. // if (i === tokens.length - 1 && token.type === 'list') {
  564. // const newListToken = completeListItemPattern(token);
  565. // if (newListToken) {
  566. // newTokens = [newListToken];
  567. // break;
  568. // }
  569. // }
  570. if (i === tokens.length - 1 && token.type === 'paragraph') {
  571. // Only operates on a single token, because any newline that follows this should break these patterns
  572. const newToken = completeSingleLinePattern(token);
  573. if (newToken) {
  574. newTokens = [newToken];
  575. break;
  576. }
  577. }
  578. }
  579. if (newTokens) {
  580. const newTokensList = [
  581. ...tokens.slice(0, i),
  582. ...newTokens
  583. ];
  584. newTokensList.links = tokens.links;
  585. return newTokensList;
  586. }
  587. return tokens;
  588. }
  589. function completeCodeBlock(tokens) {
  590. const mergedRawText = mergeRawTokenText(tokens);
  591. return marked.lexer(mergedRawText + '\n```');
  592. }
  593. function completeCodespan(token) {
  594. return completeWithString(token, '`');
  595. }
  596. function completeStar(tokens) {
  597. return completeWithString(tokens, '*');
  598. }
  599. function completeUnderscore(tokens) {
  600. return completeWithString(tokens, '_');
  601. }
  602. function completeLinkTarget(tokens) {
  603. return completeWithString(tokens, ')');
  604. }
  605. function completeLinkText(tokens) {
  606. return completeWithString(tokens, '](about:blank)');
  607. }
  608. function completeDoublestar(tokens) {
  609. return completeWithString(tokens, '**');
  610. }
  611. function completeDoubleUnderscore(tokens) {
  612. return completeWithString(tokens, '__');
  613. }
  614. function completeWithString(tokens, closingString) {
  615. const mergedRawText = mergeRawTokenText(Array.isArray(tokens) ? tokens : [tokens]);
  616. // If it was completed correctly, this should be a single token.
  617. // Expecting either a Paragraph or a List
  618. return marked.lexer(mergedRawText + closingString)[0];
  619. }
  620. function completeTable(tokens) {
  621. const mergedRawText = mergeRawTokenText(tokens);
  622. const lines = mergedRawText.split('\n');
  623. let numCols; // The number of line1 col headers
  624. let hasSeparatorRow = false;
  625. for (let i = 0; i < lines.length; i++) {
  626. const line = lines[i].trim();
  627. if (typeof numCols === 'undefined' && line.match(/^\s*\|/)) {
  628. const line1Matches = line.match(/(\|[^\|]+)(?=\||$)/g);
  629. if (line1Matches) {
  630. numCols = line1Matches.length;
  631. }
  632. }
  633. else if (typeof numCols === 'number') {
  634. if (line.match(/^\s*\|/)) {
  635. if (i !== lines.length - 1) {
  636. // We got the line1 header row, and the line2 separator row, but there are more lines, and it wasn't parsed as a table!
  637. // That's strange and means that the table is probably malformed in the source, so I won't try to patch it up.
  638. return undefined;
  639. }
  640. // Got a line2 separator row- partial or complete, doesn't matter, we'll replace it with a correct one
  641. hasSeparatorRow = true;
  642. }
  643. else {
  644. // The line after the header row isn't a valid separator row, so the table is malformed, don't fix it up
  645. return undefined;
  646. }
  647. }
  648. }
  649. if (typeof numCols === 'number' && numCols > 0) {
  650. const prefixText = hasSeparatorRow ? lines.slice(0, -1).join('\n') : mergedRawText;
  651. const line1EndsInPipe = !!prefixText.match(/\|\s*$/);
  652. const newRawText = prefixText + (line1EndsInPipe ? '' : '|') + `\n|${' --- |'.repeat(numCols)}`;
  653. return marked.lexer(newRawText);
  654. }
  655. return undefined;
  656. }