UNPKG

3.95 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 Ericsson and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.MarkdownRendererImpl = exports.MarkdownRendererFactory = exports.MarkdownRenderer = void 0;
28const DOMPurify = require("dompurify");
29const inversify_1 = require("inversify");
30const markdownit = require("markdown-it");
31const label_parser_1 = require("../label-parser");
32const widgets_1 = require("../widgets");
33// #endregion
34/** Use this directly if you aren't worried about circular dependencies in the Shell */
35exports.MarkdownRenderer = Symbol('MarkdownRenderer');
36/** Use this to avoid circular dependencies in the Shell */
37exports.MarkdownRendererFactory = Symbol('MarkdownRendererFactory');
38let MarkdownRendererImpl = class MarkdownRendererImpl {
39 constructor() {
40 this.markdownIt = markdownit();
41 }
42 init() {
43 this.markdownItPlugin();
44 }
45 render(markdown, options) {
46 const host = document.createElement('div');
47 if (markdown) {
48 const html = this.markdownIt.render(markdown.value);
49 host.innerHTML = DOMPurify.sanitize(html, {
50 ALLOW_UNKNOWN_PROTOCOLS: true // DOMPurify usually strips non http(s) links from hrefs
51 });
52 }
53 return { element: host, dispose: () => { } };
54 }
55 markdownItPlugin() {
56 this.markdownIt.renderer.rules.text = (tokens, idx) => {
57 const content = tokens[idx].content;
58 return this.labelParser.parse(content).map(chunk => {
59 if (typeof chunk === 'string') {
60 return chunk;
61 }
62 return `<i class="${(0, widgets_1.codicon)(chunk.name)} ${chunk.animation ? `fa-${chunk.animation}` : ''} icon-inline"></i>`;
63 }).join('');
64 };
65 }
66};
67__decorate([
68 (0, inversify_1.inject)(label_parser_1.LabelParser),
69 __metadata("design:type", label_parser_1.LabelParser)
70], MarkdownRendererImpl.prototype, "labelParser", void 0);
71__decorate([
72 (0, inversify_1.postConstruct)(),
73 __metadata("design:type", Function),
74 __metadata("design:paramtypes", []),
75 __metadata("design:returntype", void 0)
76], MarkdownRendererImpl.prototype, "init", null);
77MarkdownRendererImpl = __decorate([
78 (0, inversify_1.injectable)()
79], MarkdownRendererImpl);
80exports.MarkdownRendererImpl = MarkdownRendererImpl;
81//# sourceMappingURL=markdown-renderer.js.map
\No newline at end of file