Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bench/bundle-test/index-lite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { codeToHtml, createShikiInternal } from '@shikijs/core'
import { codeToHtml, createShikiPrimitiveAsync } from '@shikijs/core'
import { createJavaScriptRawEngine } from '@shikijs/engine-javascript/raw'

const shiki = createShikiInternal(
const shiki = createShikiPrimitiveAsync(
{
langs: [
import('@shikijs/langs-precompiled/ts'),
Expand Down
4 changes: 2 additions & 2 deletions bench/bundle-test/index-wasm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { codeToHtml, createShikiInternal } from '@shikijs/core'
import { codeToHtml, createShikiPrimitiveAsync } from '@shikijs/core'
import { createOnigurumaEngine } from '@shikijs/engine-oniguruma'

const shiki = createShikiInternal(
const shiki = createShikiPrimitiveAsync(
{
langs: [
import('@shikijs/langs/ts'),
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test": "vitest"
},
"dependencies": {
"@shikijs/primitive": "workspace:*",
"@shikijs/types": "workspace:*",
"@shikijs/vscode-textmate": "catalog:prod",
"@types/hast": "catalog:types",
Expand Down
25 changes: 11 additions & 14 deletions packages/core/src/constructors/highlighter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { HighlighterCore, HighlighterCoreOptions } from '@shikijs/types'

import { codeToTokensWithThemes, createShikiPrimitive, createShikiPrimitiveAsync } from '@shikijs/primitive'
import { codeToHast } from '../highlight/code-to-hast'
import { codeToHtml } from '../highlight/code-to-html'
import { codeToTokens } from '../highlight/code-to-tokens'
import { codeToTokensBase, getLastGrammarState } from '../highlight/code-to-tokens-base'
import { codeToTokensWithThemes } from '../highlight/code-to-tokens-themes'

import { createShikiInternal } from './internal'
import { createShikiInternalSync } from './internal-sync'

/**
* Create a Shiki core highlighter instance, with no languages or themes bundled.
Expand All @@ -16,19 +13,19 @@ import { createShikiInternalSync } from './internal-sync'
* @see http://shiki.style/guide/bundles#fine-grained-bundle
*/
export async function createHighlighterCore(options: HighlighterCoreOptions<false>): Promise<HighlighterCore> {
const internal = await createShikiInternal(options)
const primitive = await createShikiPrimitiveAsync(options)

return {
getLastGrammarState: (...args: any[]) => getLastGrammarState(internal, ...args as [any])!,
codeToTokensBase: (code, options) => codeToTokensBase(internal, code, options),
codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options),
codeToTokens: (code, options) => codeToTokens(internal, code, options),
codeToHast: (code, options) => codeToHast(internal, code, options),
codeToHtml: (code, options) => codeToHtml(internal, code, options),
getLastGrammarState: (...args: any[]) => getLastGrammarState(primitive, ...args as [any])!,
codeToTokensBase: (code, options) => codeToTokensBase(primitive, code, options),
codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(primitive, code, options),
codeToTokens: (code, options) => codeToTokens(primitive, code, options),
codeToHast: (code, options) => codeToHast(primitive, code, options),
codeToHtml: (code, options) => codeToHtml(primitive, code, options),
getBundledLanguages: () => ({}),
getBundledThemes: () => ({}),
...internal,
getInternalContext: () => internal,
...primitive,
getInternalContext: () => primitive,
}
}

Expand All @@ -41,7 +38,7 @@ export async function createHighlighterCore(options: HighlighterCoreOptions<fals
* @see http://shiki.style/guide/bundles#fine-grained-bundle
*/
export function createHighlighterCoreSync(options: HighlighterCoreOptions<true>): HighlighterCore {
const internal = createShikiInternalSync(options)
const internal = createShikiPrimitive(options)

return {
getLastGrammarState: (...args: any[]) => getLastGrammarState(internal, ...args as [any, any]),
Expand Down
30 changes: 0 additions & 30 deletions packages/core/src/constructors/internal.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/core/src/highlight/code-to-hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
CodeToHastOptions,
CodeToHastRenderOptions,
GrammarState,
ShikiInternal,
ShikiPrimitive,
ShikiTransformerContext,
ShikiTransformerContextCommon,
ShikiTransformerContextSource,
Expand All @@ -14,21 +14,21 @@ import type {
Text,
} from 'hast'

import { getLastGrammarStateFromMap, setLastGrammarStateToMap } from '@shikijs/primitive'
import { FontStyle } from '@shikijs/vscode-textmate'
import { getLastGrammarStateFromMap, setLastGrammarStateToMap } from '../textmate/grammar-state'
import { addClassToHast, getTokenStyleObject, stringifyTokenStyle } from '../utils'
import { getTransformers } from './_get-transformers'
import { codeToTokens } from './code-to-tokens'

export function codeToHast(
internal: ShikiInternal,
primitive: ShikiPrimitive,
code: string,
options: CodeToHastOptions,
transformerContext: ShikiTransformerContextCommon = {
meta: {},
options,
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
codeToTokens: (_code, _options) => codeToTokens(internal, _code, _options),
codeToHast: (_code, _options) => codeToHast(primitive, _code, _options),
codeToTokens: (_code, _options) => codeToTokens(primitive, _code, _options),
},
): Root {
let input = code
Expand All @@ -43,7 +43,7 @@ export function codeToHast(
themeName,
rootStyle,
grammarState,
} = codeToTokens(internal, input, options)
} = codeToTokens(primitive, input, options)

const {
mergeWhitespaces = true,
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/highlight/code-to-html.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
CodeToHastOptions,
ShikiInternal,
ShikiPrimitive,
ShikiTransformerContextCommon,
} from '@shikijs/types'

Expand All @@ -16,18 +16,18 @@ export const hastToHtml = toHtml
* Get highlighted code in HTML.
*/
export function codeToHtml(
internal: ShikiInternal,
primitive: ShikiPrimitive,
code: string,
options: CodeToHastOptions,
): string {
const context: ShikiTransformerContextCommon = {
meta: {},
options,
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
codeToTokens: (_code, _options) => codeToTokens(internal, _code, _options),
codeToHast: (_code, _options) => codeToHast(primitive, _code, _options),
codeToTokens: (_code, _options) => codeToTokens(primitive, _code, _options),
}

let result = hastToHtml(codeToHast(internal, code, options, context))
let result = hastToHtml(codeToHast(primitive, code, options, context))

for (const transformer of getTransformers(options))
result = transformer.postprocess?.call(context, result, options) || result
Expand Down
Loading