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,7 +1,7 @@
1
1
  {
2
2
  "name": "rn-shiki",
3
3
  "type": "module",
4
- "version": "0.0.37-3",
4
+ "version": "0.0.37-4",
5
5
  "description": "Shiki syntax highlighter for React Native.",
6
6
  "author": "Ryan Skinner <hello@ryanskinner.com>",
7
7
  "license": "MIT",
@@ -1,4 +1,5 @@
1
- import type { SyntaxHighlighterProps, ThemedToken, TokensResult } from '../types/shiki'
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: theme ? [theme] : [],
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: theme || 'none',
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
- async function initializeHighlighter(langs: LanguageInput[], themes: ThemeInput[]): Promise<Awaited<ReturnType<typeof createHighlighterCore>>> {
7
- return createHighlighterCore({
8
- engine: createJavaScriptRegexEngine(),
9
- langs,
10
- themes,
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[] }): Promise<Awaited<ReturnType<typeof createHighlighterCore>>> {
15
- if (!langs?.length || !themes?.length) {
16
- throw new Error('Please provide both `langs` and `themes` when creating a highlighter.')
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 {
@@ -1,20 +1,19 @@
1
- import type { LanguageInput, ThemeRegistrationAny } from 'shiki'
1
+ import type { LanguageInput, ThemedToken, ThemeInput } from 'shiki'
2
2
 
3
- export interface ThemedToken {
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?: ThemeRegistrationAny
13
+ theme: ThemeInput
19
14
  fontSize?: number
20
15
  }
16
+
17
+ export interface TokensResult {
18
+ tokens: ThemedToken[][]
19
+ }