|
| 1 | +import { createHighlighter } from 'shiki' |
| 2 | +import { expect, it } from 'vitest' |
| 3 | +import { transformerRenderWhitespace } from '../src/transformers/render-whitespace' |
| 4 | + |
| 5 | +it('transformerRenderWhitespace', async () => { |
| 6 | + using shiki = await createHighlighter({ |
| 7 | + themes: ['vitesse-dark', 'vitesse-light', 'nord'], |
| 8 | + langs: ['typescript'], |
| 9 | + }) |
| 10 | + |
| 11 | + const transformer = transformerRenderWhitespace() |
| 12 | + |
| 13 | + const code = ` |
| 14 | + const a = Math.random() > 0.5 ? 1 : \`foo\` |
| 15 | + `.trim() |
| 16 | + |
| 17 | + const result = shiki.codeToHtml(code, { |
| 18 | + lang: 'typescript', |
| 19 | + themes: { |
| 20 | + dark: 'vitesse-dark', |
| 21 | + light: 'vitesse-light', |
| 22 | + nord: 'nord', |
| 23 | + }, |
| 24 | + defaultColor: false, |
| 25 | + transformers: [transformer], |
| 26 | + structure: 'inline', |
| 27 | + }) |
| 28 | + |
| 29 | + expect(result.replace(/<span/g, '\n<span')) |
| 30 | + .toMatchInlineSnapshot(` |
| 31 | + " |
| 32 | + <span style="--shiki-dark:#CB7676;--shiki-light:#AB5959;--shiki-nord:#81A1C1">const</span> |
| 33 | + <span class="space"> </span> |
| 34 | + <span style="--shiki-dark:#BD976A;--shiki-light:#B07D48;--shiki-nord:#D8DEE9">a</span> |
| 35 | + <span class="space"> </span> |
| 36 | + <span style="--shiki-dark:#666666;--shiki-light:#999999;--shiki-nord:#81A1C1">=</span> |
| 37 | + <span class="space"> </span> |
| 38 | + <span style="--shiki-dark:#BD976A;--shiki-light:#B07D48;--shiki-nord:#D8DEE9">Math</span> |
| 39 | + <span style="--shiki-dark:#666666;--shiki-light:#999999;--shiki-nord:#ECEFF4">.</span> |
| 40 | + <span style="--shiki-dark:#80A665;--shiki-light:#59873A;--shiki-nord:#88C0D0">random</span> |
| 41 | + <span style="--shiki-dark:#666666;--shiki-light:#999999;--shiki-nord:#D8DEE9FF">()</span> |
| 42 | + <span class="space"> </span> |
| 43 | + <span style="--shiki-dark:#666666;--shiki-light:#999999;--shiki-nord:#81A1C1">></span> |
| 44 | + <span class="space"> </span> |
| 45 | + <span style="--shiki-dark:#4C9A91;--shiki-light:#2F798A;--shiki-nord:#B48EAD">0.5</span> |
| 46 | + <span class="space"> </span> |
| 47 | + <span style="--shiki-dark:#CB7676;--shiki-light:#AB5959;--shiki-nord:#81A1C1">?</span> |
| 48 | + <span class="space"> </span> |
| 49 | + <span style="--shiki-dark:#4C9A91;--shiki-light:#2F798A;--shiki-nord:#B48EAD">1</span> |
| 50 | + <span class="space"> </span> |
| 51 | + <span style="--shiki-dark:#CB7676;--shiki-light:#AB5959;--shiki-nord:#81A1C1">:</span> |
| 52 | + <span class="space"> </span> |
| 53 | + <span style="--shiki-dark:#C98A7D77;--shiki-light:#B5695977;--shiki-nord:#ECEFF4">\`</span> |
| 54 | + <span style="--shiki-dark:#C98A7D;--shiki-light:#B56959;--shiki-nord:#A3BE8C">foo</span> |
| 55 | + <span style="--shiki-dark:#C98A7D77;--shiki-light:#B5695977;--shiki-nord:#ECEFF4">\`</span>" |
| 56 | + `) |
| 57 | +}) |
0 commit comments