(msg: InMessage)
| 56 | } |
| 57 | |
| 58 | async function highlight(msg: InMessage): Promise<OutMessage> { |
| 59 | const highlighter = await getHighlighter(); |
| 60 | const theme = msg.theme ?? "github-dark"; |
| 61 | |
| 62 | let html: string; |
| 63 | try { |
| 64 | html = highlighter.codeToHtml(msg.code, { lang: msg.lang, theme }); |
| 65 | } catch { |
| 66 | // Unknown language — fall back to plain text rendering |
| 67 | html = highlighter.codeToHtml(msg.code, { lang: "text", theme }); |
| 68 | } |
| 69 | |
| 70 | return { id: msg.id, html, plainText: msg.code }; |
| 71 | } |
| 72 | |
| 73 | self.addEventListener("message", async (e: MessageEvent<InMessage>) => { |
| 74 | const result = await highlight(e.data); |
no test coverage detected