markdownRenderer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 { StandardMouseEvent } from './mouseEvent.js';
  10. import { renderLabelWithIcons } from './ui/iconLabel/iconLabels.js';
  11. import { onUnexpectedError } from '../common/errors.js';
  12. import { Event } from '../common/event.js';
  13. import { escapeDoubleQuotes, parseHrefAndDimensions, removeMarkdownEscapes } from '../common/htmlContent.js';
  14. import { markdownEscapeEscapedIcons } from '../common/iconLabels.js';
  15. import { defaultGenerator } from '../common/idGenerator.js';
  16. import { DisposableStore } from '../common/lifecycle.js';
  17. import { marked } from '../common/marked/marked.js';
  18. import { parse } from '../common/marshalling.js';
  19. import { FileAccess, Schemas } from '../common/network.js';
  20. import { cloneAndChange } from '../common/objects.js';
  21. import { dirname, resolvePath } from '../common/resources.js';
  22. import { escape } from '../common/strings.js';
  23. import { URI } from '../common/uri.js';
  24. /**
  25. * Low-level way create a html element from a markdown string.
  26. *
  27. * **Note** that for most cases you should be using [`MarkdownRenderer`](./src/vs/editor/contrib/markdownRenderer/browser/markdownRenderer.ts)
  28. * which comes with support for pretty code block rendering and which uses the default way of handling links.
  29. */
  30. export function renderMarkdown(markdown, options = {}, markedOptions = {}) {
  31. var _a;
  32. const disposables = new DisposableStore();
  33. let isDisposed = false;
  34. const element = createElement(options);
  35. const _uriMassage = function (part) {
  36. let data;
  37. try {
  38. data = parse(decodeURIComponent(part));
  39. }
  40. catch (e) {
  41. // ignore
  42. }
  43. if (!data) {
  44. return part;
  45. }
  46. data = cloneAndChange(data, value => {
  47. if (markdown.uris && markdown.uris[value]) {
  48. return URI.revive(markdown.uris[value]);
  49. }
  50. else {
  51. return undefined;
  52. }
  53. });
  54. return encodeURIComponent(JSON.stringify(data));
  55. };
  56. const _href = function (href, isDomUri) {
  57. const data = markdown.uris && markdown.uris[href];
  58. let uri = URI.revive(data);
  59. if (isDomUri) {
  60. if (href.startsWith(Schemas.data + ':')) {
  61. return href;
  62. }
  63. if (!uri) {
  64. uri = URI.parse(href);
  65. }
  66. // this URI will end up as "src"-attribute of a dom node
  67. // and because of that special rewriting needs to be done
  68. // so that the URI uses a protocol that's understood by
  69. // browsers (like http or https)
  70. return FileAccess.asBrowserUri(uri).toString(true);
  71. }
  72. if (!uri) {
  73. return href;
  74. }
  75. if (URI.parse(href).toString() === uri.toString()) {
  76. return href; // no transformation performed
  77. }
  78. if (uri.query) {
  79. uri = uri.with({ query: _uriMassage(uri.query) });
  80. }
  81. return uri.toString();
  82. };
  83. const renderer = new marked.Renderer();
  84. renderer.image = (href, title, text) => {
  85. let dimensions = [];
  86. let attributes = [];
  87. if (href) {
  88. ({ href, dimensions } = parseHrefAndDimensions(href));
  89. attributes.push(`src="${escapeDoubleQuotes(href)}"`);
  90. }
  91. if (text) {
  92. attributes.push(`alt="${escapeDoubleQuotes(text)}"`);
  93. }
  94. if (title) {
  95. attributes.push(`title="${escapeDoubleQuotes(title)}"`);
  96. }
  97. if (dimensions.length) {
  98. attributes = attributes.concat(dimensions);
  99. }
  100. return '<img ' + attributes.join(' ') + '>';
  101. };
  102. renderer.link = (href, title, text) => {
  103. if (typeof href !== 'string') {
  104. return '';
  105. }
  106. // Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
  107. if (href === text) { // raw link case
  108. text = removeMarkdownEscapes(text);
  109. }
  110. title = typeof title === 'string' ? escapeDoubleQuotes(removeMarkdownEscapes(title)) : '';
  111. href = removeMarkdownEscapes(href);
  112. // HTML Encode href
  113. href = href.replace(/&/g, '&amp;')
  114. .replace(/</g, '&lt;')
  115. .replace(/>/g, '&gt;')
  116. .replace(/"/g, '&quot;')
  117. .replace(/'/g, '&#39;');
  118. return `<a href="${href}" title="${title || href}">${text}</a>`;
  119. };
  120. renderer.paragraph = (text) => {
  121. return `<p>${text}</p>`;
  122. };
  123. // Will collect [id, renderedElement] tuples
  124. const codeBlocks = [];
  125. if (options.codeBlockRenderer) {
  126. renderer.code = (code, lang) => {
  127. const id = defaultGenerator.nextId();
  128. const value = options.codeBlockRenderer(lang !== null && lang !== void 0 ? lang : '', code);
  129. codeBlocks.push(value.then(element => [id, element]));
  130. return `<div class="code" data-code="${id}">${escape(code)}</div>`;
  131. };
  132. }
  133. if (options.actionHandler) {
  134. const onClick = options.actionHandler.disposables.add(new DomEmitter(element, 'click'));
  135. const onAuxClick = options.actionHandler.disposables.add(new DomEmitter(element, 'auxclick'));
  136. options.actionHandler.disposables.add(Event.any(onClick.event, onAuxClick.event)(e => {
  137. const mouseEvent = new StandardMouseEvent(e);
  138. if (!mouseEvent.leftButton && !mouseEvent.middleButton) {
  139. return;
  140. }
  141. let target = mouseEvent.target;
  142. if (target.tagName !== 'A') {
  143. target = target.parentElement;
  144. if (!target || target.tagName !== 'A') {
  145. return;
  146. }
  147. }
  148. try {
  149. let href = target.dataset['href'];
  150. if (href) {
  151. if (markdown.baseUri) {
  152. href = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  153. }
  154. options.actionHandler.callback(href, mouseEvent);
  155. }
  156. }
  157. catch (err) {
  158. onUnexpectedError(err);
  159. }
  160. finally {
  161. mouseEvent.preventDefault();
  162. }
  163. }));
  164. }
  165. if (!markdown.supportHtml) {
  166. // TODO: Can we deprecated this in favor of 'supportHtml'?
  167. // Use our own sanitizer so that we can let through only spans.
  168. // Otherwise, we'd be letting all html be rendered.
  169. // If we want to allow markdown permitted tags, then we can delete sanitizer and sanitize.
  170. // We always pass the output through dompurify after this so that we don't rely on
  171. // marked for sanitization.
  172. markedOptions.sanitizer = (html) => {
  173. const match = markdown.isTrusted ? html.match(/^(<span[^>]+>)|(<\/\s*span>)$/) : undefined;
  174. return match ? html : '';
  175. };
  176. markedOptions.sanitize = true;
  177. markedOptions.silent = true;
  178. }
  179. markedOptions.renderer = renderer;
  180. // values that are too long will freeze the UI
  181. let value = (_a = markdown.value) !== null && _a !== void 0 ? _a : '';
  182. if (value.length > 100000) {
  183. value = `${value.substr(0, 100000)}…`;
  184. }
  185. // escape theme icons
  186. if (markdown.supportThemeIcons) {
  187. value = markdownEscapeEscapedIcons(value);
  188. }
  189. let renderedMarkdown = marked.parse(value, markedOptions);
  190. // Rewrite theme icons
  191. if (markdown.supportThemeIcons) {
  192. const elements = renderLabelWithIcons(renderedMarkdown);
  193. renderedMarkdown = elements.map(e => typeof e === 'string' ? e : e.outerHTML).join('');
  194. }
  195. const htmlParser = new DOMParser();
  196. const markdownHtmlDoc = htmlParser.parseFromString(sanitizeRenderedMarkdown(markdown, renderedMarkdown), 'text/html');
  197. markdownHtmlDoc.body.querySelectorAll('img')
  198. .forEach(img => {
  199. const src = img.getAttribute('src'); // Get the raw 'src' attribute value as text, not the resolved 'src'
  200. if (src) {
  201. let href = src;
  202. try {
  203. if (markdown.baseUri) { // absolute or relative local path, or file: uri
  204. href = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  205. }
  206. }
  207. catch (err) { }
  208. img.src = _href(href, true);
  209. }
  210. });
  211. markdownHtmlDoc.body.querySelectorAll('a')
  212. .forEach(a => {
  213. const href = a.getAttribute('href'); // Get the raw 'href' attribute value as text, not the resolved 'href'
  214. a.setAttribute('href', ''); // Clear out href. We use the `data-href` for handling clicks instead
  215. if (!href
  216. || /^data:|javascript:/i.test(href)
  217. || (/^command:/i.test(href) && !markdown.isTrusted)
  218. || /^command:(\/\/\/)?_workbench\.downloadResource/i.test(href)) {
  219. // drop the link
  220. a.replaceWith(...a.childNodes);
  221. }
  222. else {
  223. let resolvedHref = _href(href, false);
  224. if (markdown.baseUri) {
  225. resolvedHref = resolveWithBaseUri(URI.from(markdown.baseUri), href);
  226. }
  227. a.dataset.href = resolvedHref;
  228. }
  229. });
  230. element.innerHTML = sanitizeRenderedMarkdown(markdown, markdownHtmlDoc.body.innerHTML);
  231. if (codeBlocks.length > 0) {
  232. Promise.all(codeBlocks).then((tuples) => {
  233. var _a, _b;
  234. if (isDisposed) {
  235. return;
  236. }
  237. const renderedElements = new Map(tuples);
  238. const placeholderElements = element.querySelectorAll(`div[data-code]`);
  239. for (const placeholderElement of placeholderElements) {
  240. const renderedElement = renderedElements.get((_a = placeholderElement.dataset['code']) !== null && _a !== void 0 ? _a : '');
  241. if (renderedElement) {
  242. DOM.reset(placeholderElement, renderedElement);
  243. }
  244. }
  245. (_b = options.asyncRenderCallback) === null || _b === void 0 ? void 0 : _b.call(options);
  246. });
  247. }
  248. // signal size changes for image tags
  249. if (options.asyncRenderCallback) {
  250. for (const img of element.getElementsByTagName('img')) {
  251. const listener = disposables.add(DOM.addDisposableListener(img, 'load', () => {
  252. listener.dispose();
  253. options.asyncRenderCallback();
  254. }));
  255. }
  256. }
  257. return {
  258. element,
  259. dispose: () => {
  260. isDisposed = true;
  261. disposables.dispose();
  262. }
  263. };
  264. }
  265. function resolveWithBaseUri(baseUri, href) {
  266. const hasScheme = /^\w[\w\d+.-]*:/.test(href);
  267. if (hasScheme) {
  268. return href;
  269. }
  270. if (baseUri.path.endsWith('/')) {
  271. return resolvePath(baseUri, href).toString();
  272. }
  273. else {
  274. return resolvePath(dirname(baseUri), href).toString();
  275. }
  276. }
  277. function sanitizeRenderedMarkdown(options, renderedMarkdown) {
  278. const { config, allowedSchemes } = getSanitizerOptions(options);
  279. dompurify.addHook('uponSanitizeAttribute', (element, e) => {
  280. if (e.attrName === 'style' || e.attrName === 'class') {
  281. if (element.tagName === 'SPAN') {
  282. if (e.attrName === 'style') {
  283. e.keepAttr = /^(color\:#[0-9a-fA-F]+;)?(background-color\:#[0-9a-fA-F]+;)?$/.test(e.attrValue);
  284. return;
  285. }
  286. else if (e.attrName === 'class') {
  287. e.keepAttr = /^codicon codicon-[a-z\-]+( codicon-modifier-[a-z\-]+)?$/.test(e.attrValue);
  288. return;
  289. }
  290. }
  291. e.keepAttr = false;
  292. return;
  293. }
  294. });
  295. const hook = DOM.hookDomPurifyHrefAndSrcSanitizer(allowedSchemes);
  296. try {
  297. return dompurify.sanitize(renderedMarkdown, Object.assign(Object.assign({}, config), { RETURN_TRUSTED_TYPE: true }));
  298. }
  299. finally {
  300. dompurify.removeHook('uponSanitizeAttribute');
  301. hook.dispose();
  302. }
  303. }
  304. function getSanitizerOptions(options) {
  305. const allowedSchemes = [
  306. Schemas.http,
  307. Schemas.https,
  308. Schemas.mailto,
  309. Schemas.data,
  310. Schemas.file,
  311. Schemas.vscodeFileResource,
  312. Schemas.vscodeRemote,
  313. Schemas.vscodeRemoteResource,
  314. ];
  315. if (options.isTrusted) {
  316. allowedSchemes.push(Schemas.command);
  317. }
  318. return {
  319. config: {
  320. // allowedTags should included everything that markdown renders to.
  321. // Since we have our own sanitize function for marked, it's possible we missed some tag so let dompurify make sure.
  322. // HTML tags that can result from markdown are from reading https://spec.commonmark.org/0.29/
  323. // HTML table tags that can result from markdown are from https://github.github.com/gfm/#tables-extension-
  324. ALLOWED_TAGS: ['ul', 'li', 'p', 'b', 'i', 'code', 'blockquote', 'ol', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'em', 'pre', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'div', 'del', 'a', 'strong', 'br', 'img', 'span'],
  325. ALLOWED_ATTR: ['href', 'data-href', 'target', 'title', 'src', 'alt', 'class', 'style', 'data-code', 'width', 'height', 'align'],
  326. ALLOW_UNKNOWN_PROTOCOLS: true,
  327. },
  328. allowedSchemes
  329. };
  330. }