|
1 | | -import { marked } from 'marked'; |
| 1 | +const MARKED_CDN_URL = 'https://cdn.jsdelivr.net/npm/marked@16.4.1/lib/marked.esm.js'; |
2 | 2 |
|
3 | | -marked.setOptions({ |
4 | | - breaks: true, |
5 | | - gfm: true, |
6 | | -}); |
| 3 | +let markedPromise = null; |
| 4 | + |
| 5 | +function resolveMarkedInstance() { |
| 6 | + if (markedPromise) { |
| 7 | + return markedPromise; |
| 8 | + } |
| 9 | + |
| 10 | + markedPromise = (async () => { |
| 11 | + const existing = globalThis.marked; |
| 12 | + if (existing && typeof existing.parse === 'function') { |
| 13 | + return existing; |
| 14 | + } |
| 15 | + |
| 16 | + try { |
| 17 | + const module = await import('marked'); |
| 18 | + const candidate = module.marked ?? module.default ?? module; |
| 19 | + if (candidate && typeof candidate.parse === 'function') { |
| 20 | + return candidate; |
| 21 | + } |
| 22 | + } catch (error) { |
| 23 | + console.warn('Falling back to CDN marked import', error); |
| 24 | + } |
| 25 | + |
| 26 | + const fallback = await import(MARKED_CDN_URL); |
| 27 | + const candidate = fallback.marked ?? fallback.default ?? fallback; |
| 28 | + if (!candidate || typeof candidate.parse !== 'function') { |
| 29 | + throw new Error('Unable to load marked parser'); |
| 30 | + } |
| 31 | + |
| 32 | + return candidate; |
| 33 | + })() |
| 34 | + .then((instance) => { |
| 35 | + if (typeof instance.setOptions === 'function') { |
| 36 | + instance.setOptions({ |
| 37 | + breaks: true, |
| 38 | + gfm: true, |
| 39 | + }); |
| 40 | + } |
| 41 | + return instance; |
| 42 | + }) |
| 43 | + .catch((error) => { |
| 44 | + markedPromise = null; |
| 45 | + throw error; |
| 46 | + }); |
| 47 | + |
| 48 | + return markedPromise; |
| 49 | +} |
7 | 50 |
|
8 | 51 | const FOCUSABLE_SELECTOR = [ |
9 | 52 | 'a[href]', |
@@ -42,24 +85,32 @@ function normalizeToText(value) { |
42 | 85 |
|
43 | 86 | function renderMarkdown(target, markdown) { |
44 | 87 | if (!target) { |
45 | | - return false; |
| 88 | + return Promise.resolve(false); |
46 | 89 | } |
47 | 90 |
|
48 | 91 | const text = typeof markdown === 'string' ? markdown.trim() : ''; |
49 | 92 | if (!text) { |
50 | | - return false; |
| 93 | + return Promise.resolve(false); |
51 | 94 | } |
52 | 95 |
|
53 | | - const html = marked.parse(text); |
54 | | - if (!html) { |
55 | | - return false; |
56 | | - } |
| 96 | + return resolveMarkedInstance() |
| 97 | + .then((instance) => { |
| 98 | + const parser = typeof instance.parse === 'function' ? instance.parse.bind(instance) : null; |
| 99 | + const html = parser ? parser(text) : ''; |
| 100 | + if (!html) { |
| 101 | + return false; |
| 102 | + } |
57 | 103 |
|
58 | | - target.innerHTML = html; |
59 | | - target.querySelectorAll('ul').forEach((list) => { |
60 | | - list.classList.add('modal__list'); |
61 | | - }); |
62 | | - return true; |
| 104 | + target.innerHTML = html; |
| 105 | + target.querySelectorAll('ul').forEach((list) => { |
| 106 | + list.classList.add('modal__list'); |
| 107 | + }); |
| 108 | + return true; |
| 109 | + }) |
| 110 | + .catch((error) => { |
| 111 | + console.error('Failed to render markdown content', error); |
| 112 | + return false; |
| 113 | + }); |
63 | 114 | } |
64 | 115 |
|
65 | 116 | function getFocusableElements(container) { |
@@ -223,6 +274,7 @@ export function createModalController(root) { |
223 | 274 | } |
224 | 275 |
|
225 | 276 | content.innerHTML = ''; |
| 277 | + content.hidden = true; |
226 | 278 |
|
227 | 279 | const listItems = Array.isArray(card.details) |
228 | 280 | ? card.details |
@@ -254,10 +306,27 @@ export function createModalController(root) { |
254 | 306 | || ''; |
255 | 307 | } |
256 | 308 |
|
257 | | - const rendered = renderMarkdown(content, markdownSource); |
258 | | - if (!rendered && markdownSource) { |
259 | | - content.textContent = markdownSource; |
260 | | - } |
| 309 | + const fallbackText = markdownSource || detailsTextForFallback || summaryTextCandidate || ''; |
| 310 | + |
| 311 | + renderMarkdown(content, markdownSource) |
| 312 | + .then((rendered) => { |
| 313 | + if (rendered) { |
| 314 | + content.hidden = false; |
| 315 | + return; |
| 316 | + } |
| 317 | + |
| 318 | + if (fallbackText) { |
| 319 | + content.textContent = fallbackText; |
| 320 | + content.hidden = false; |
| 321 | + } |
| 322 | + }) |
| 323 | + .catch(() => { |
| 324 | + if (fallbackText) { |
| 325 | + content.textContent = fallbackText; |
| 326 | + content.hidden = false; |
| 327 | + } |
| 328 | + }); |
| 329 | + |
261 | 330 | if (card.backgroundColor) { |
262 | 331 | dialog.style.background = card.backgroundColor; |
263 | 332 | } else { |
|
0 commit comments