| 64 | }, |
| 65 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 66 | highlight({ code: src, language, themes }: { code: string; language: string; themes: any }, cb?: Callback) { |
| 67 | const lang = SUPPORTED.has(language.trim().toLowerCase()) ? language.trim().toLowerCase() : "text"; |
| 68 | const names: [string, string] = [themeName(themes[0]), themeName(themes[1])]; |
| 69 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 70 | const key = cacheKey(src, lang, names as any); |
| 71 | |
| 72 | if (cache.has(key)) return cache.get(key); |
| 73 | |
| 74 | if (cb) { |
| 75 | if (!pending.has(key)) pending.set(key, new Set<Callback>()); |
| 76 | pending.get(key)!.add(cb); |
| 77 | } |
| 78 | |
| 79 | highlighterPromise.then((h) => { |
| 80 | const tokens = h.codeToTokens(src, { |
| 81 | lang: h.getLoadedLanguages().includes(lang) ? lang : "text", |
| 82 | themes: { light: names[0] as string, dark: names[1] as string }, |
| 83 | }); |
| 84 | cache.set(key, tokens); |
| 85 | const waiters = pending.get(key); |
| 86 | if (waiters) { |
| 87 | for (const fn of waiters) fn(tokens); |
| 88 | pending.delete(key); |
| 89 | } |
| 90 | }).catch((e) => { |
| 91 | console.error("[code-plugin] highlight failed:", e); |
| 92 | pending.delete(key); |
| 93 | }); |
| 94 | |
| 95 | return null; |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |