|
| 1 | +/* global document */ |
| 2 | + |
| 3 | +import createHighlighter from '@node-core/rehype-shiki'; |
| 4 | +import { createTransformerFactory, rendererRich } from '@shikijs/twoslash'; |
| 5 | +import shikiNordTheme from 'shiki/themes/nord.mjs'; |
| 6 | +import { createTwoslashFromCDN } from 'twoslash-cdn'; |
| 7 | +import { createStorage } from 'unstorage'; |
| 8 | +import indexedDbDriver from 'unstorage/drivers/indexedb'; |
| 9 | + |
| 10 | +// An example using unstorage with IndexedDB to cache the virtual file system |
| 11 | +const storage = createStorage({ |
| 12 | + driver: indexedDbDriver({ base: 'twoslash-cdn' }), |
| 13 | +}); |
| 14 | + |
| 15 | +const twoslash = createTwoslashFromCDN({ |
| 16 | + storage, |
| 17 | + compilerOptions: { |
| 18 | + lib: ['dom', 'dom.iterable', 'esnext'], |
| 19 | + module: 'nodenext', |
| 20 | + types: ['node'], |
| 21 | + }, |
| 22 | +}); |
| 23 | + |
| 24 | +const transformerTwoslash = createTransformerFactory(twoslash.runSync)({ |
| 25 | + renderer: rendererRich({ jsdoc: true }), |
| 26 | + langs: ['ts', 'js', 'cjs', 'mjs'], |
| 27 | + throws: false, |
| 28 | +}); |
| 29 | + |
| 30 | +const highlighterPromise = createHighlighter({ |
| 31 | + wasm: false, |
| 32 | +}); |
| 33 | + |
| 34 | +/** |
| 35 | + * Extracts the raw code content from a <pre><code> element |
| 36 | + * @param {HTMLPreElement} preElement - The pre element |
| 37 | + * @returns {string} The raw code content |
| 38 | + */ |
| 39 | +function extractRawCode(preElement) { |
| 40 | + const codeElement = preElement.querySelector('code'); |
| 41 | + if (!codeElement) { |
| 42 | + return ''; |
| 43 | + } |
| 44 | + |
| 45 | + return codeElement.textContent || ''; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Process a single code block with Twoslash |
| 50 | + * @param {HTMLPreElement} preElement - The pre element |
| 51 | + * @returns {Promise<void>} |
| 52 | + */ |
| 53 | +async function processTwoslashBlock(preElement) { |
| 54 | + try { |
| 55 | + const rawCode = extractRawCode(preElement); |
| 56 | + |
| 57 | + if (!rawCode) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const highlighter = await highlighterPromise; |
| 62 | + |
| 63 | + await twoslash.prepareTypes(rawCode); |
| 64 | + |
| 65 | + const html = highlighter.shiki.codeToHtml(rawCode, { |
| 66 | + lang: 'mjs', |
| 67 | + theme: { |
| 68 | + // We are updating this color because the background color and comment text color |
| 69 | + // in the Codebox component do not comply with accessibility standards. |
| 70 | + // See: https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html |
| 71 | + colorReplacements: { '#616e88': '#707e99' }, |
| 72 | + ...shikiNordTheme, |
| 73 | + }, |
| 74 | + transformers: [transformerTwoslash], |
| 75 | + }); |
| 76 | + |
| 77 | + const temp = document.createElement('div'); |
| 78 | + |
| 79 | + temp.innerHTML = html; |
| 80 | + |
| 81 | + const newPre = temp.querySelector('pre'); |
| 82 | + |
| 83 | + if (newPre) { |
| 84 | + newPre.className = `${preElement.className} twoslash`; |
| 85 | + newPre.style = ''; |
| 86 | + |
| 87 | + preElement.parentNode?.replaceChild(newPre, preElement); |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + console.error('Error processing Twoslash block:', error); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Initialize Twoslash processing on page load |
| 96 | + */ |
| 97 | +async function initTwoslash() { |
| 98 | + const codeBlocks = document.querySelectorAll('pre'); |
| 99 | + |
| 100 | + const twoslashBlocks = Array.from(codeBlocks).filter(preElement => |
| 101 | + preElement.querySelector('code') |
| 102 | + ); |
| 103 | + |
| 104 | + await Promise.all(twoslashBlocks.map(block => processTwoslashBlock(block))); |
| 105 | +} |
| 106 | + |
| 107 | +initTwoslash(); |
0 commit comments