rn-shiki 0.0.37-3 → 0.0.37-5

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-5",
5
5
  "description": "Shiki syntax highlighter for React Native.",
6
6
  "author": "Ryan Skinner <hello@ryanskinner.com>",
7
7
  "license": "MIT",
@@ -1,5 +1,7 @@
1
+ import type { ThemeRegistrationAny } from 'shiki'
2
+ import type { SyntaxHighlighterProps } from 'src/types/shiki'
1
3
  import React from 'react'
2
- import { Platform, ScrollView, type ScrollViewProps, StyleSheet, Text, type TextStyle, View } from 'react-native'
4
+ import { Platform, ScrollView, StyleSheet, Text, type TextStyle, View } from 'react-native'
3
5
  import { useSyntaxHighlighter } from '../../hooks/useSyntaxHighlighter'
4
6
  import { convertShikiTheme } from '../../utils/style-transformer'
5
7
 
@@ -9,15 +11,6 @@ const monospaceFont = Platform.select({
9
11
  default: 'monospace',
10
12
  })
11
13
 
12
- interface SyntaxHighlighterProps {
13
- text: string
14
- language: any
15
- languageId: string
16
- theme: any
17
- textStyle?: TextStyle
18
- scrollViewProps?: ScrollViewProps
19
- }
20
-
21
14
  const styles = StyleSheet.create({
22
15
  scrollView: {
23
16
  flex: 1,
@@ -32,8 +25,8 @@ const styles = StyleSheet.create({
32
25
  },
33
26
  })
34
27
 
35
- const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme, textStyle, scrollViewProps }) => {
36
- const themeStyles = React.useMemo(() => convertShikiTheme(theme), [theme])
28
+ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme }) => {
29
+ const themeStyles = React.useMemo(() => convertShikiTheme(theme as ThemeRegistrationAny), [theme])
37
30
  const { tokens } = useSyntaxHighlighter({
38
31
  text,
39
32
  language,
@@ -48,7 +41,6 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
48
41
  ...(token.color && { color: token.color }),
49
42
  ...(token.fontStyle === 'italic' && themeStyles.italic),
50
43
  ...(token.fontWeight === 'bold' && themeStyles.bold),
51
- ...textStyle,
52
44
  }
53
45
 
54
46
  return (
@@ -65,13 +57,7 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
65
57
  )
66
58
 
67
59
  return (
68
- <ScrollView
69
- horizontal
70
- showsHorizontalScrollIndicator={Platform.OS !== 'web'}
71
- style={styles.scrollView}
72
- contentContainerStyle={[styles.contentContainer, themeStyles.container, scrollViewProps?.contentContainerStyle]}
73
- {...scrollViewProps}
74
- >
60
+ <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={styles.scrollView}>
75
61
  <View onStartShouldSetResponder={() => true}>{tokens.map((line, index) => renderLine(line, index))}</View>
76
62
  </ScrollView>
77
63
  )
@@ -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) {
package/src/index.ts CHANGED
@@ -1,6 +1,3 @@
1
1
  import SyntaxHighlighter from './components/syntax/SyntaxHighlighter'
2
- import SyntaxLine from './components/syntax/SyntaxLine'
3
2
 
4
- export * from './types/shiki'
5
-
6
- export { SyntaxHighlighter, SyntaxLine }
3
+ export { SyntaxHighlighter }
@@ -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
+ }
@@ -1,81 +0,0 @@
1
- import type { TokenType } from '../../types'
2
- import type { RNTokenStyle } from '../../utils/style-transformer'
3
- import React from 'react'
4
- import { Platform, StyleSheet, Text, View } from 'react-native'
5
-
6
- const monospaceFont = Platform.select({
7
- ios: 'Menlo',
8
- android: 'monospace',
9
- default: 'monospace',
10
- })
11
-
12
- const styles = StyleSheet.create({
13
- lineContainer: {
14
- flexDirection: 'row',
15
- flexWrap: 'wrap',
16
- },
17
- token: {
18
- includeFontPadding: false,
19
- },
20
- })
21
-
22
- interface SyntaxLineProps {
23
- line: TokenType[]
24
- fontSize?: number
25
- isLastLine?: boolean
26
- themeStyles: {
27
- backgroundColor: string
28
- defaultColor: string
29
- styles: Record<string, RNTokenStyle>
30
- }
31
- }
32
-
33
- function SyntaxLine({ line, fontSize = 14, isLastLine = false, themeStyles }: SyntaxLineProps) {
34
- const lineHeight = Math.floor(fontSize * 1.5)
35
-
36
- return (
37
- <View
38
- style={[
39
- styles.lineContainer,
40
- {
41
- minHeight: lineHeight,
42
- paddingBottom: !isLastLine ? 4 : 0,
43
- },
44
- ]}
45
- >
46
- {line.map((token, tokenIndex) => {
47
- const content = token.content.replace(/ /g, '\u00A0')
48
- // Use converted theme styles
49
- const tokenStyle = token.color ? { color: token.color } : { color: themeStyles.defaultColor }
50
-
51
- return (
52
- <Text
53
- key={`${tokenIndex}-${content.slice(0, 8)}`}
54
- style={[
55
- styles.token,
56
- {
57
- ...tokenStyle,
58
- fontFamily: monospaceFont,
59
- fontSize,
60
- fontStyle: token.fontStyle as 'normal' | 'italic',
61
- fontWeight: token.fontWeight as 'normal' | 'bold',
62
- lineHeight,
63
- height: lineHeight,
64
- textAlignVertical: 'center',
65
- ...(Platform.OS === 'ios'
66
- ? {
67
- paddingTop: 2,
68
- }
69
- : {}),
70
- },
71
- ]}
72
- >
73
- {content}
74
- </Text>
75
- )
76
- })}
77
- </View>
78
- )
79
- }
80
-
81
- export default SyntaxLine
@@ -1,2 +0,0 @@
1
- export { default as SyntaxHighlighter } from './SyntaxHighlighter'
2
- export { default as SyntaxLine } from './SyntaxLine'