Skip to content

Commit

Permalink
test: snapshot for HAST
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 18, 2024
1 parent 26bf0e0 commit 023ae42
Show file tree
Hide file tree
Showing 38 changed files with 44,510 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/shikiji-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export { getShikiInternal, setDefaultWasmLoader } from './internal'
export { codeToThemedTokens } from './tokenizer'
export { tokenizeAnsiWithTheme } from './tokenizer-ansi'
export { codeToHast } from './renderer-hast'
export { codeToHtml } from './renderer-html'
export { codeToHtml, hastToHtml } from './renderer-html'
export { codeToTokensWithThemes } from './renderer-html-themes'
export { normalizeTheme } from './normalize'
2 changes: 2 additions & 0 deletions packages/shikiji-core/src/renderer-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { toHtml as hastToHtml } from 'hast-util-to-html'
import type { CodeToHastOptions, ShikiInternal, ShikijiTransformerContextCommon } from './types'
import { codeToHast } from './renderer-hast'

export { hastToHtml }

/**
* Get highlighted code in HTML.
*/
Expand Down
16 changes: 12 additions & 4 deletions packages/shikiji-twoslash/test/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { codeToHtml } from 'shikiji'
import { basename } from 'node:path'
import { codeToHast, hastToHtml } from 'shikiji'
import { rendererRich, transformerTwoslash } from 'shikiji-twoslash'
import { describe, expect, it } from 'vitest'

const files = import.meta.glob('./fixtures/*.*', { as: 'raw', eager: true })

describe('fixtures', () => {
for (const file in files) {
it('should works', async () => {
const name = basename(file)
it(name, async () => {
const code = files[file]
const ext = file.split('.').pop()!
const html = await codeToHtml(code, {
const hast = await codeToHast(code, {
lang: ext,
theme: 'vitesse-dark',
transformers: [
Expand All @@ -19,8 +21,14 @@ describe('fixtures', () => {
],
})

const html = hastToHtml(hast)

expect.soft(JSON.stringify(hast, null, 2))
.toMatchFileSnapshot(`./out/${name}.json`)

const style = '<link rel="stylesheet" href="../../../style-rich.css" />'
expect(style + html).toMatchFileSnapshot(`./out/${file}.html`)
expect.soft(style + html)
.toMatchFileSnapshot(`./out/${name}.html`)
})
}
})
5 changes: 5 additions & 0 deletions packages/shikiji-twoslash/test/fixtures/import-vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @errors: 2322
import { ref } from "vue"

const a = ref(10)
a.value = 'hi'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<link rel="stylesheet" href="../../../style-rich.css" /><pre class="shiki vitesse-dark twoslash lsp" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#4D9375">import</span><span style="color:#DBD7CAEE"> </span><span style="color:#666666">{</span><span style="color:#DBD7CAEE"> </span><span style="color:#BD976A"><span class="twoslash-hover"><span class="twoslash-popup-container"><code class="twoslash-popup-code"><span class="line"><span style="color:#CB7676">const</span><span> </span><span style="color:#BD976A">foo</span><span style="color:#666666">:</span><span> </span><span style="color:#C98A7D99">"</span><span style="color:#C98A7D">foo</span><span style="color:#C98A7D99">"</span></span></code></span>foo</span></span><span style="color:#DBD7CAEE"> </span><span style="color:#666666">}</span><span style="color:#DBD7CAEE"> </span><span style="color:#4D9375">from</span><span style="color:#DBD7CAEE"> </span><span style="color:#C98A7D99">'</span><span style="color:#C98A7D">./foo</span><span style="color:#C98A7D99">'</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BD976A"><span class="twoslash-hover"><span class="twoslash-popup-container"><code class="twoslash-popup-code"><span class="line"><span style="color:#CB7676">var</span><span> </span><span style="color:#BD976A">console</span><span style="color:#666666">:</span><span> </span><span style="color:#5DA994">Console</span></span></code></span>console</span></span><span style="color:#666666">.</span><span style="color:#BD976A"><span>e<span class="twoslash-completion-cursor"><ul class="twoslash-completion-list"><li><span class="twoslash-completions-icon completions-method"><svg viewBox="0 0 32 32"><path fill="currentColor" d="m19.626 29.526l-.516-1.933a12.004 12.004 0 0 0 6.121-19.26l1.538-1.28a14.003 14.003 0 0 1-7.143 22.473"></path><path fill="currentColor" d="M10 29H8v-3.82l.804-.16C10.262 24.727 12 23.62 12 20v-1.382l-4-2v-2.236l4-2V12c0-5.467 3.925-9 10-9h2v3.82l-.804.16C21.738 7.273 20 8.38 20 12v.382l4 2v2.236l-4 2V20c0 5.467-3.925 9-10 9m0-2c4.935 0 8-2.682 8-7v-2.618l3.764-1.882L18 13.618V12c0-4.578 2.385-6.192 4-6.76V5c-4.935 0-8 2.682-8 7v1.618L10.236 15.5L14 17.382V20c0 4.578-2.385 6.192-4 6.76Z"></path><path fill="currentColor" d="M5.231 24.947a14.003 14.003 0 0 1 7.147-22.474l.516 1.932a12.004 12.004 0 0 0-6.125 19.263Z"></path></svg></span><span><span class="twoslash-completions-matched">e</span><span class="twoslash-completions-unmatched">rror</span></span></li></ul></span></span></span><span style="color:#DBD7CAEE"> </span></span>
<span class="line"></span></code></pre>
Loading

0 comments on commit 023ae42

Please sign in to comment.