|
1 | 1 | import DOMPurify from 'dompurify'; |
2 | | -import { html, type TemplateResult } from 'lit'; |
3 | | -import { Marked, Renderer } from 'marked'; |
| 2 | +import { unsafeHTML } from 'lit/directives/unsafe-html.js'; |
| 3 | +import { Marked } from 'marked'; |
4 | 4 | import markedShiki from 'marked-shiki'; |
5 | 5 | import { getSingletonHighlighter } from 'shiki'; |
6 | 6 | import type { IgcMessage } from '../types.js'; |
@@ -30,121 +30,50 @@ export interface MarkdownRendererOptions { |
30 | 30 | sanitizer?: (html: string) => string; |
31 | 31 | } |
32 | 32 |
|
33 | | -/** |
34 | | - * A renderer that converts markdown chat messages to HTML using `marked`, |
35 | | - * with optional syntax highlighting powered by `shiki`. |
36 | | - * |
37 | | - */ |
38 | | -export class MarkdownMessageRenderer { |
39 | | - private highlighter?: any; |
40 | | - private theme: string; |
41 | | - private langs: string[]; |
42 | | - private _marked: Marked; |
43 | | - private initialized = false; |
44 | | - private _sanitizer: (html: string) => string; |
45 | | - |
46 | | - /** |
47 | | - * Creates a new MarkdownMessageRenderer. |
48 | | - * |
49 | | - * @param {MarkdownRendererOptions} [opts={}] - Configuration options. |
50 | | - */ |
51 | | - constructor(private opts: MarkdownRendererOptions = {}) { |
52 | | - this.theme = opts.theme ?? 'github-light'; |
53 | | - this.langs = opts.languages ?? ['javascript', 'typescript', 'html', 'css']; |
54 | | - |
55 | | - this._marked = new Marked(); |
56 | | - this.initMarked(); |
57 | | - this._sanitizer = this.initSanitizer(); |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Initializes the `marked` instance with custom renderer options. |
62 | | - * Currently modifies link rendering to open in a new tab with safe attributes. |
63 | | - * |
64 | | - * @private |
65 | | - */ |
66 | | - private initMarked() { |
67 | | - const renderer = new Renderer(); |
68 | | - |
69 | | - // Customize link rendering |
70 | | - renderer.link = (href, title, text) => { |
71 | | - return `<a href="${href}" target="_blank" rel="noopener noreferrer" ${title ? `title="${title}"` : ''}>${text}</a>`; |
72 | | - }; |
73 | | - |
74 | | - this._marked.setOptions({ |
75 | | - gfm: true, |
76 | | - breaks: true, |
77 | | - renderer, |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - private initSanitizer() { |
82 | | - return this.opts?.sanitizer ?? DOMPurify.sanitize; |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * Performs async initialization for syntax highlighting. |
87 | | - * Loads Shiki and configures the `marked` instance with `marked-shiki`. |
88 | | - * This is called lazily during rendering unless pre-called explicitly. |
89 | | - * |
90 | | - * @async |
91 | | - * @returns {Promise<void>} A promise that resolves once initialization is complete. |
92 | | - */ |
93 | | - async init(): Promise<void> { |
94 | | - if (this.highlighter || this.opts.noHighlighter) { |
95 | | - this.initialized = true; |
96 | | - return; |
97 | | - } |
98 | | - |
99 | | - this.highlighter = await getSingletonHighlighter({ |
100 | | - themes: [this.theme], |
101 | | - langs: this.langs, |
| 33 | +export async function createMarkdownRenderer( |
| 34 | + options?: MarkdownRendererOptions |
| 35 | +) { |
| 36 | + const sanitizer = options?.sanitizer ?? DOMPurify.sanitize; |
| 37 | + |
| 38 | + const markedInstance = new Marked({ |
| 39 | + breaks: true, |
| 40 | + gfm: true, |
| 41 | + extensions: [ |
| 42 | + { |
| 43 | + name: 'link', |
| 44 | + renderer({ href, title, text }) { |
| 45 | + return `<a href="${href}" target="_blank" rel="noopener noreferrer" ${title ? `title="${title}"` : ''}>${text}</a>`; |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + |
| 51 | + if (!options?.noHighlighter) { |
| 52 | + const themes = [options?.theme ?? 'github-light']; |
| 53 | + const langs = options?.languages |
| 54 | + ? options.languages |
| 55 | + : ['javascript', 'typescript', 'html', 'css']; |
| 56 | + |
| 57 | + const shikiInstance = await getSingletonHighlighter({ |
| 58 | + themes, |
| 59 | + langs, |
102 | 60 | }); |
103 | 61 |
|
104 | | - this._marked.use( |
| 62 | + markedInstance.use( |
105 | 63 | markedShiki({ |
106 | | - highlight: (code: any, lang: string) => { |
107 | | - try { |
108 | | - const safeLang = |
109 | | - lang && this.highlighter.getLoadedLanguages?.().includes(lang) |
110 | | - ? lang |
111 | | - : 'text'; |
112 | | - |
113 | | - return ( |
114 | | - this.highlighter?.codeToHtml(code, { |
115 | | - lang: safeLang, |
116 | | - themes: { light: this.theme }, |
117 | | - }) ?? code |
118 | | - ); |
119 | | - } catch (_err) { |
120 | | - // if Shiki still throws for some reason, just return escaped code |
121 | | - return `<pre><code>${DOMPurify.sanitize(code)}</code></pre>`; |
122 | | - } |
| 64 | + highlight(code, lang, _) { |
| 65 | + return shikiInstance.codeToHtml(code, { |
| 66 | + lang, |
| 67 | + themes: { light: themes[0] }, |
| 68 | + }); |
123 | 69 | }, |
124 | 70 | }) |
125 | 71 | ); |
126 | | - |
127 | | - this.initialized = true; |
128 | 72 | } |
129 | 73 |
|
130 | | - /** |
131 | | - * Renders the given chat message as markdown, with optional syntax highlighting. |
132 | | - * |
133 | | - * @param {IgcMessage} message - The message to render. |
134 | | - * @returns {Promise<TemplateResult>} A lit template containing the rendered markdown content. |
135 | | - */ |
136 | | - async render(message: IgcMessage): Promise<TemplateResult> { |
137 | | - if (!this.initialized) { |
138 | | - await this.init(); |
139 | | - } |
140 | | - |
141 | | - if (!message.text) return html``; |
142 | | - |
143 | | - const rendered = await this._marked?.parse(message.text); |
144 | | - const cleanHtml = this._sanitizer(rendered); |
145 | | - const template = document.createElement('template'); |
146 | | - template.innerHTML = cleanHtml ?? message.text; |
147 | | - |
148 | | - return html`${template.content}`; |
149 | | - } |
| 74 | + return async (message: IgcMessage): Promise<unknown> => { |
| 75 | + return message.text |
| 76 | + ? unsafeHTML(sanitizer(await markedInstance.parse(message.text))) |
| 77 | + : ''; |
| 78 | + }; |
150 | 79 | } |
0 commit comments