/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as DOM from './dom.js'; import { defaultGenerator } from '../common/idGenerator.js'; import { escape } from '../common/strings.js'; import { removeMarkdownEscapes } from '../common/htmlContent.js'; import * as marked from '../common/marked/marked.js'; import { onUnexpectedError } from '../common/errors.js'; import { URI } from '../common/uri.js'; import { parse } from '../common/marshalling.js'; import { cloneAndChange } from '../common/objects.js'; function createElement(options) { var tagName = options.inline ? 'span' : 'div'; var element = document.createElement(tagName); if (options.className) { element.className = options.className; } return element; } export function renderText(text, options) { if (options === void 0) { options = {}; } var element = createElement(options); element.textContent = text; return element; } export function renderFormattedText(formattedText, options) { if (options === void 0) { options = {}; } var element = createElement(options); _renderFormattedText(element, parseFormattedText(formattedText), options.actionHandler); return element; } /** * Create html nodes for the given content element. */ export function renderMarkdown(markdown, options) { if (options === void 0) { options = {}; } var element = createElement(options); var _uriMassage = function (part) { var data; try { data = parse(decodeURIComponent(part)); } catch (e) { // ignore } if (!data) { return part; } data = cloneAndChange(data, function (value) { if (markdown.uris && markdown.uris[value]) { return URI.revive(markdown.uris[value]); } else { return undefined; } }); return encodeURIComponent(JSON.stringify(data)); }; var _href = function (href) { var data = markdown.uris && markdown.uris[href]; if (!data) { return href; } var uri = URI.revive(data); if (uri.query) { uri = uri.with({ query: _uriMassage(uri.query) }); } if (data) { href = uri.toString(true); } return href; }; // signal to code-block render that the // element has been created var signalInnerHTML; var withInnerHTML = new Promise(function (c) { return signalInnerHTML = c; }); var renderer = new marked.Renderer(); renderer.image = function (href, title, text) { href = _href(href); var dimensions = []; if (href) { var splitted = href.split('|').map(function (s) { return s.trim(); }); href = splitted[0]; var parameters = splitted[1]; if (parameters) { var heightFromParams = /height=(\d+)/.exec(parameters); var widthFromParams = /width=(\d+)/.exec(parameters); var height = heightFromParams ? heightFromParams[1] : ''; var width = widthFromParams ? widthFromParams[1] : ''; var widthIsFinite = isFinite(parseInt(width)); var heightIsFinite = isFinite(parseInt(height)); if (widthIsFinite) { dimensions.push("width=\"" + width + "\""); } if (heightIsFinite) { dimensions.push("height=\"" + height + "\""); } } } var attributes = []; if (href) { attributes.push("src=\"" + href + "\""); } if (text) { attributes.push("alt=\"" + text + "\""); } if (title) { attributes.push("title=\"" + title + "\""); } if (dimensions.length) { attributes = attributes.concat(dimensions); } return ''; }; renderer.link = function (href, title, text) { // Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829 if (href === text) { // raw link case text = removeMarkdownEscapes(text); } href = _href(href); title = removeMarkdownEscapes(title); href = removeMarkdownEscapes(href); if (!href || href.match(/^data:|javascript:/i) || (href.match(/^command:/i) && !markdown.isTrusted) || href.match(/^command:(\/\/\/)?_workbench\.downloadResource/i)) { // drop the link return text; } else { // HTML Encode href href = href.replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); return "" + text + ""; } }; renderer.paragraph = function (text) { return "
" + text + "
"; }; if (options.codeBlockRenderer) { renderer.code = function (code, lang) { var value = options.codeBlockRenderer(lang, code); // when code-block rendering is async we return sync // but update the node with the real result later. var id = defaultGenerator.nextId(); var promise = Promise.all([value, withInnerHTML]).then(function (values) { var strValue = values[0]; var span = element.querySelector("div[data-code=\"" + id + "\"]"); if (span) { span.innerHTML = strValue; } }).catch(function (err) { // ignore }); if (options.codeBlockRenderCallback) { promise.then(options.codeBlockRenderCallback); } return "