rn-shiki 0.0.37-24 → 0.0.37-25

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-24",
4
+ "version": "0.0.37-25",
5
5
  "description": "Shiki syntax highlighter for React Native.",
6
6
  "author": "Ryan Skinner <hello@ryanskinner.com>",
7
7
  "license": "MIT",
@@ -1,6 +1,5 @@
1
- import type { ColorValue, StyleProp, TextStyle, ViewStyle } from 'react-native'
1
+ import type { ColorValue, StyleProp, TextStyle } from 'react-native'
2
2
  import type { SyntaxHighlighterProps } from 'src/types/shiki'
3
- import type { ReactStyle } from '../../utils/style-transformer'
4
3
  import React, { useMemo } from 'react'
5
4
  import { Platform, ScrollView, StyleSheet, Text, View } from 'react-native'
6
5
  import { useSyntaxHighlighter } from '../../hooks/useSyntaxHighlighter'
@@ -12,14 +11,30 @@ const monospaceFont = Platform.select({
12
11
  default: 'monospace',
13
12
  })
14
13
 
14
+ interface TokenStyle extends Omit<TextStyle, 'color' | 'fontWeight'> {
15
+ color?: ColorValue
16
+ fontStyle?: 'normal' | 'italic'
17
+ fontWeight?: FontWeight
18
+ fontFamily?: string
19
+ }
20
+
15
21
  interface Styles {
16
- scrollView: ViewStyle
17
- lineContainer: ViewStyle
18
- token: TextStyle
19
- container: ViewStyle
22
+ [key: string]: TokenStyle
23
+ }
24
+
25
+ type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'
26
+
27
+ interface ThemeColors {
28
+ 'editor.background'?: string
29
+ 'editor.foreground'?: string
30
+ }
31
+
32
+ interface Theme {
33
+ colors?: ThemeColors
34
+ tokenColors?: any[]
20
35
  }
21
36
 
22
- const baseStyles = StyleSheet.create<Styles>({
37
+ const baseStyles = StyleSheet.create({
23
38
  scrollView: {
24
39
  flex: 1,
25
40
  minHeight: 20,
@@ -27,29 +42,39 @@ const baseStyles = StyleSheet.create<Styles>({
27
42
  lineContainer: {
28
43
  flexDirection: 'row',
29
44
  flexWrap: 'wrap',
45
+ paddingHorizontal: 16,
46
+ paddingVertical: 8,
30
47
  },
31
48
  token: {
32
49
  fontFamily: monospaceFont,
50
+ fontSize: 14,
33
51
  },
34
52
  container: {
35
53
  flex: 1,
36
54
  },
37
55
  })
38
56
 
39
- type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'
40
-
41
- interface TokenStyle extends Omit<TextStyle, 'color' | 'fontWeight'> {
42
- color?: ColorValue
43
- fontStyle?: 'normal' | 'italic'
44
- fontWeight?: FontWeight
45
- fontFamily?: string
46
- }
47
-
48
- const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme }) => {
57
+ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({
58
+ text,
59
+ language,
60
+ languageId,
61
+ theme,
62
+ fontSize = 14,
63
+ }) => {
49
64
  const stylesheet = useMemo(() => {
50
- const styles = getRNStylesFromShikiStyle(theme as ReactStyle)
51
- return StyleSheet.create(styles)
52
- }, [theme])
65
+ const styles = getRNStylesFromShikiStyle(theme as Theme)
66
+ return StyleSheet.create<Styles>({
67
+ ...styles,
68
+ editor: {
69
+ backgroundColor: (theme as Theme).colors?.['editor.background'] || '#1e1e1e',
70
+ },
71
+ token: {
72
+ ...baseStyles.token,
73
+ fontSize,
74
+ color: (theme as Theme).colors?.['editor.foreground'] || '#d4d4d4',
75
+ },
76
+ })
77
+ }, [theme, fontSize])
53
78
 
54
79
  const { tokens } = useSyntaxHighlighter({
55
80
  text,
@@ -59,19 +84,17 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
59
84
  })
60
85
 
61
86
  const renderToken = (token: any, index: number, lineIndex: number) => {
62
- const styles: StyleProp<TokenStyle>[] = [baseStyles.token]
63
-
64
- if (stylesheet.base) {
65
- styles.push(stylesheet.base as unknown as TokenStyle)
66
- }
87
+ const styles: StyleProp<TokenStyle>[] = [stylesheet.token]
67
88
 
89
+ // Add token-specific styles from stylesheet based on scope
68
90
  const tokenScopes = Array.isArray(token.scope) ? token.scope : [token.scope]
69
91
  tokenScopes.forEach((scope: string) => {
70
92
  if (stylesheet[scope]) {
71
- styles.push(stylesheet[scope] as unknown as TokenStyle)
93
+ styles.push(stylesheet[scope])
72
94
  }
73
95
  })
74
96
 
97
+ // Add token-specific styles from the token itself
75
98
  const tokenStyles: TokenStyle = {}
76
99
  if (token.color) {
77
100
  tokenStyles.color = token.color
@@ -87,10 +110,10 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
87
110
  styles.push(tokenStyles)
88
111
  }
89
112
 
90
- const combinedStyles = StyleSheet.flatten(styles)
113
+ const finalStyle = StyleSheet.flatten(styles)
91
114
 
92
115
  return (
93
- <Text key={`${lineIndex}-${index}`} style={combinedStyles}>
116
+ <Text key={`${lineIndex}-${index}`} style={finalStyle}>
94
117
  {token.content.replace(/ /g, '\u00A0')}
95
118
  </Text>
96
119
  )
@@ -103,10 +126,13 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
103
126
  )
104
127
 
105
128
  return (
106
- <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={[baseStyles.scrollView, stylesheet.base as unknown as TokenStyle]}>
107
- <View style={baseStyles.container} onStartShouldSetResponder={() => true}>
108
- {tokens.map((line, index) => renderLine(line, index))}
109
- </View>
129
+ <ScrollView
130
+ horizontal
131
+ showsHorizontalScrollIndicator={Platform.OS !== 'web'}
132
+ style={[baseStyles.scrollView, stylesheet.editor]}
133
+ contentContainerStyle={baseStyles.container}
134
+ >
135
+ {tokens.map((line, index) => renderLine(line, index))}
110
136
  </ScrollView>
111
137
  )
112
138
  }