Skip to content

Commit

Permalink
fix(precompiled): fix traverse algorithm, close #917
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 5, 2025
1 parent 9cd269d commit 3722d2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`run 1`] = `"<pre class="shiki vitesse-dark" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#666666">&#x3C;</span><span style="color:#4D9375">div</span><span style="color:#666666">></span><span style="color:#DBD7CAEE">hello</span><span style="color:#666666">&#x3C;/</span><span style="color:#4D9375">div</span><span style="color:#666666">></span></span></code></pre>"`;
20 changes: 20 additions & 0 deletions packages/langs-precompiled/tests/precompile-run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createJavaScriptRawEngine } from '@shikijs/engine-javascript'
import { createHighlighterCore } from 'shiki'
import { expect, it } from 'vitest'
// eslint-disable-next-line antfu/no-import-dist
import vitesseDark from '../../themes/dist/vitesse-dark.mjs'
// eslint-disable-next-line antfu/no-import-dist
import html from '../dist/html.mjs'

const isNode20andUp = process.version.replace(/^v/, '').split('.').map(Number)[0] >= 20

it.runIf(isNode20andUp)('run', async () => {
const shiki = await createHighlighterCore({
themes: [vitesseDark],
langs: [html],
engine: createJavaScriptRawEngine(),
})

const code = shiki.codeToHtml('<div>hello</div>', { lang: 'html', theme: 'vitesse-dark' })
expect(code).toMatchSnapshot()
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EmulatedRegExp } from 'oniguruma-to-es'
import { format } from 'prettier'
import { expect, it } from 'vitest'
import { toJsLiteral } from './langs'
import { precompileGrammar } from './precompile'
import { toJsLiteral } from '../scripts/langs'
import { precompileGrammar } from '../scripts/precompile'

const isNode20andUp = process.version.replace(/^v/, '').split('.').map(Number)[0] >= 20

Expand Down
4 changes: 4 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function traverseGrammarPatterns(a: any, callback: (pattern: string) => a
}
if (!a || typeof a !== 'object')
return

if (a.foldingStartMarker) {
const pattern = callback(a.foldingStartMarker)
if (pattern != null)
Expand Down Expand Up @@ -54,6 +55,9 @@ export function traverseGrammarPatterns(a: any, callback: (pattern: string) => a
if (a.endCaptures) {
traverseGrammarPatterns(Object.values(a.endCaptures), callback)
}
if (a.injections) {
traverseGrammarPatterns(Object.values(a.injections), callback)
}
Object.values(a.repository || {}).forEach((j: any) => {
traverseGrammarPatterns(j, callback)
})
Expand Down

0 comments on commit 3722d2f

Please sign in to comment.