Skip to content

Commit 705cd04

Browse files
committed
fix: allow to init the highlighter without themes and langs
1 parent dfc1d25 commit 705cd04

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

packages/shikiji/src/core/core.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { renderToHtmlDualThemes } from './renderer-html-dual-themes'
99

1010
export type HighlighterCore = HighlighterGeneric<never, never>
1111

12-
export async function getHighlighterCore(options: HighlighterCoreOptions): Promise<HighlighterCore> {
12+
export async function getHighlighterCore(options: HighlighterCoreOptions = {}): Promise<HighlighterCore> {
1313
async function resolveLangs(langs: LanguageInput[]) {
1414
return Array.from(new Set((await Promise.all(
1515
langs.map(async lang => await normalizeGetter(lang).then(r => Array.isArray(r) ? r : [r])),
@@ -19,8 +19,8 @@ export async function getHighlighterCore(options: HighlighterCoreOptions): Promi
1919
const [
2020
themes, langs,
2121
] = await Promise.all([
22-
Promise.all(options.themes.map(normalizeGetter)),
23-
resolveLangs(options.langs),
22+
Promise.all((options.themes || []).map(normalizeGetter)),
23+
resolveLangs(options.langs || []),
2424
typeof options.loadWasm === 'function'
2525
? Promise.resolve(options.loadWasm()).then(r => loadWasm(r))
2626
: options.loadWasm
@@ -40,7 +40,7 @@ export async function getHighlighterCore(options: HighlighterCoreOptions): Promi
4040
const _registry = new Registry(resolver, themes, langs)
4141
await _registry.init()
4242

43-
const defaultTheme = themes[0].name
43+
const defaultTheme = themes[0]?.name
4444

4545
function codeToThemedTokens(
4646
code: string,

packages/shikiji/src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export interface HighlighterGeneric<BundledLangKeys extends string, BundledTheme
4949
}
5050

5151
export interface HighlighterCoreOptions {
52-
themes: ThemeInput[]
53-
langs: LanguageInput[]
52+
themes?: ThemeInput[]
53+
langs?: LanguageInput[]
5454
loadWasm?: OnigurumaLoadOptions | (() => Promise<OnigurumaLoadOptions>)
5555
}
5656

packages/shikiji/test/core.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ describe('should', () => {
7676
]
7777
`)
7878
})
79+
80+
it('works without no initial langs and themes', async () => {
81+
const shiki = await getHighlighterCore()
82+
83+
await shiki.loadLanguage(js)
84+
await shiki.loadTheme(nord)
85+
86+
const code = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' })
87+
88+
expect(code).toMatchInlineSnapshot('"<pre class=\\"shiki nord\\" style=\\"background-color:#2e3440ff;color:#2e3440ff\\" tabindex=\\"0\\"><code><span class=\\"line\\"><span style=\\"color:#D8DEE9\\">console</span><span style=\\"color:#ECEFF4\\">.</span><span style=\\"color:#88C0D0\\">log</span><span style=\\"color:#D8DEE9FF\\">(</span><span style=\\"color:#ECEFF4\\">&quot;</span><span style=\\"color:#A3BE8C\\">Hi</span><span style=\\"color:#ECEFF4\\">&quot;</span><span style=\\"color:#D8DEE9FF\\">)</span></span></code></pre>"')
89+
})
7990
})
8091

8192
describe('errors', () => {

0 commit comments

Comments
 (0)