Skip to content

Commit 3d29e94

Browse files
committed
fix(compat): support ansiToThemedTokens
1 parent ba679ea commit 3d29e94

File tree

6 files changed

+13
-31
lines changed

6 files changed

+13
-31
lines changed

packages/shikiji-compat/src/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import fsp from 'node:fs/promises'
33
import type { BuiltinLanguage, BuiltinTheme, CodeToThemedTokensOptions, MaybeGetter, StringLiteralUnion, ThemeInput, ThemeRegistration, ThemedToken } from 'shikiji'
4-
import { bundledLanguages, bundledThemes, getHighlighter as getShikiji, toShikiTheme } from 'shikiji'
4+
import { bundledLanguages, bundledThemes, getHighlighter as getShikiji, toShikiTheme, tokenizeAnsiWithTheme } from 'shikiji'
55
import { transformerCompactLineOptions } from 'shikiji-transformers'
66
import type { AnsiToHtmlOptions, CodeToHtmlOptions, CodeToHtmlOptionsExtra, HighlighterOptions } from './types'
77

@@ -73,8 +73,17 @@ export async function getHighlighter(options: HighlighterOptions = {}) {
7373
return shikiji.codeToHtml(code, options as any)
7474
}
7575

76+
function ansiToThemedTokens(
77+
ansi: string,
78+
options: CodeToThemedTokensOptions = {},
79+
) {
80+
const theme = shikiji.getTheme(options.theme || context.getLoadedThemes()[0])
81+
return tokenizeAnsiWithTheme(theme, ansi)
82+
}
83+
7684
return {
7785
...shikiji,
86+
ansiToThemedTokens,
7887
codeToThemedTokens,
7988
codeToHtml,
8089
ansiToHtml(code: string, options?: AnsiToHtmlOptions) {

packages/shikiji-compat/test/types.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ it('run', async () => {
3636

3737
expect.soft(keysDiff).toMatchInlineSnapshot(`
3838
[
39-
"ansiToThemedTokens",
4039
"getBackgroundColor",
4140
"getForegroundColor",
4241
"setColorReplacements",

packages/shikiji-transformers/src/utils.ts

-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import type { Element } from 'hast'
22
import type { ShikijiTransformer, ShikijiTransformerContext } from 'shikiji'
33

4-
/**
5-
* Check if a node is comment-like,
6-
* e.g. `<!-- comment -->`, `/* comment ..`, `// comment`
7-
*/
8-
export function isCommentLike(node: Element, line: Element) {
9-
if (node.children?.[0].type !== 'text')
10-
return false
11-
const text = node.children[0].value.trim()
12-
if (text.startsWith('<!--') && text.endsWith('-->'))
13-
return true
14-
if (text.startsWith('/*') && text.endsWith('*/'))
15-
return true
16-
if (text.startsWith('//') && line.children.indexOf(node) === line.children.length - 1)
17-
return true
18-
return false
19-
}
20-
214
export function createCommentNotationTransformer(
225
name: string,
236
regex: RegExp,

packages/shikiji/src/core/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ export * from './highlighter'
55
export * from './bundle-factory'
66
export * from './utils'
77

8+
export { codeToThemedTokens } from './tokenizer'
9+
export { tokenizeAnsiWithTheme } from './tokenizer-ansi'
810
export { toShikiTheme } from './normalize'

packages/shikiji/src/core/renderer-ansi.ts

-11
This file was deleted.

vitest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default defineConfig({
2626
coverage: {
2727
provider: 'v8',
2828
include: [
29-
'**/packages/*/src/*.ts',
29+
'**/packages/*/src/**/*.ts',
3030
],
3131
exclude: [
3232
'**/src/oniguruma/**',

0 commit comments

Comments
 (0)