rn-shiki 0.0.37-3 → 0.0.37-4
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
import type {
|
1
|
+
import type { ThemedToken } from 'shiki'
|
2
|
+
import type { SyntaxHighlighterProps, TokensResult } from '../types/shiki'
|
2
3
|
import { useEffect, useState } from 'react'
|
3
4
|
import { createHighlighter } from '../syntax'
|
4
5
|
|
@@ -19,14 +20,22 @@ export function useSyntaxHighlighter({ text, language, languageId, theme }: Synt
|
|
19
20
|
}
|
20
21
|
|
21
22
|
try {
|
23
|
+
// First create the highlighter
|
22
24
|
const highlighter = await createHighlighter({
|
23
25
|
langs: [language],
|
24
|
-
themes:
|
26
|
+
themes: [theme],
|
25
27
|
})
|
26
28
|
|
29
|
+
// Get resolved theme name
|
30
|
+
const themeResolved = highlighter.getLoadedThemes()?.[0]
|
31
|
+
if (!themeResolved) {
|
32
|
+
throw new Error('Failed to resolve theme')
|
33
|
+
}
|
34
|
+
|
35
|
+
// Use resolved theme for tokenization
|
27
36
|
const result = highlighter.codeToTokens(text, {
|
28
37
|
lang: languageId,
|
29
|
-
theme:
|
38
|
+
theme: themeResolved,
|
30
39
|
}) as TokensResult
|
31
40
|
|
32
41
|
if (mounted && result.tokens) {
|
@@ -3,17 +3,30 @@ import type { TokenType } from '../../types'
|
|
3
3
|
import { createHighlighterCore } from 'shiki/dist/core.mjs'
|
4
4
|
import { createJavaScriptRegexEngine } from 'shiki/dist/engine-javascript.mjs'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
const engine = createJavaScriptRegexEngine({
|
7
|
+
forgiving: true,
|
8
|
+
simulation: true,
|
9
|
+
cache: new Map(),
|
10
|
+
})
|
11
|
+
|
12
|
+
async function initializeHighlighter(langs: LanguageInput[], themes: ThemeInput[]) {
|
13
|
+
try {
|
14
|
+
// Initialize the core highlighter with our configured engine
|
15
|
+
return await createHighlighterCore({
|
16
|
+
langs,
|
17
|
+
themes,
|
18
|
+
engine, // Pass the configured engine
|
19
|
+
})
|
20
|
+
}
|
21
|
+
catch (err) {
|
22
|
+
console.error('Error initializing highlighter:', err)
|
23
|
+
throw err
|
24
|
+
}
|
12
25
|
}
|
13
26
|
|
14
|
-
export async function createHighlighter({ langs, themes }: { langs: LanguageInput[], themes: ThemeInput[] })
|
15
|
-
if (!langs?.length
|
16
|
-
throw new Error('Please provide
|
27
|
+
export async function createHighlighter({ langs, themes }: { langs: LanguageInput[], themes: ThemeInput[] }) {
|
28
|
+
if (!langs?.length) {
|
29
|
+
throw new Error('Please provide languages when creating a highlighter.')
|
17
30
|
}
|
18
31
|
|
19
32
|
try {
|
package/src/types/shiki.ts
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
-
import type { LanguageInput,
|
1
|
+
import type { LanguageInput, ThemedToken, ThemeInput } from 'shiki'
|
2
2
|
|
3
|
-
export interface
|
3
|
+
export interface TokenType {
|
4
4
|
content: string
|
5
5
|
color?: string
|
6
6
|
fontStyle?: string
|
7
|
-
fontWeight?: string
|
8
|
-
}
|
9
|
-
|
10
|
-
export interface TokensResult {
|
11
|
-
tokens: ThemedToken[][]
|
12
7
|
}
|
13
8
|
|
14
9
|
export interface SyntaxHighlighterProps {
|
15
10
|
text: string
|
16
11
|
language: LanguageInput
|
17
12
|
languageId: string
|
18
|
-
theme
|
13
|
+
theme: ThemeInput
|
19
14
|
fontSize?: number
|
20
15
|
}
|
16
|
+
|
17
|
+
export interface TokensResult {
|
18
|
+
tokens: ThemedToken[][]
|
19
|
+
}
|