markdownRenderer.js 13 KB

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