rn-shiki 0.0.37-21 → 0.0.37-22

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-21",
4
+ "version": "0.0.37-22",
5
5
  "description": "Shiki syntax highlighter for React Native.",
6
6
  "author": "Ryan Skinner <hello@ryanskinner.com>",
7
7
  "license": "MIT",
@@ -1,3 +1,4 @@
1
+ import type { ColorValue, StyleProp, TextStyle, ViewStyle } from 'react-native'
1
2
  import type { SyntaxHighlighterProps } from 'src/types/shiki'
2
3
  import type { ReactStyle } from '../../utils/style-transformer'
3
4
  import React, { useMemo } from 'react'
@@ -11,7 +12,14 @@ const monospaceFont = Platform.select({
11
12
  default: 'monospace',
12
13
  })
13
14
 
14
- const styles = StyleSheet.create({
15
+ interface Styles {
16
+ scrollView: ViewStyle
17
+ lineContainer: ViewStyle
18
+ token: TextStyle
19
+ container: ViewStyle
20
+ }
21
+
22
+ const baseStyles = StyleSheet.create<Styles>({
15
23
  scrollView: {
16
24
  flex: 1,
17
25
  minHeight: 20,
@@ -20,14 +28,27 @@ const styles = StyleSheet.create({
20
28
  flexDirection: 'row',
21
29
  flexWrap: 'wrap',
22
30
  },
31
+ token: {
32
+ fontFamily: monospaceFont,
33
+ },
34
+ container: {
35
+ flex: 1,
36
+ },
23
37
  })
24
38
 
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
+
25
48
  const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme }) => {
26
- console.log('theme:', theme)
27
49
  const stylesheet = useMemo(() => {
28
50
  const styles = getRNStylesFromShikiStyle(theme as ReactStyle)
29
- console.log('Generated stylesheet:', styles)
30
- return styles
51
+ return StyleSheet.create(styles)
31
52
  }, [theme])
32
53
 
33
54
  const { tokens } = useSyntaxHighlighter({
@@ -38,20 +59,35 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
38
59
  })
39
60
 
40
61
  const renderToken = (token: any, index: number, lineIndex: number) => {
41
- // Ensure to collect all styles associated with the token's scopes
62
+ const styles: StyleProp<TokenStyle>[] = [baseStyles.token]
63
+
64
+ if (stylesheet.base) {
65
+ styles.push(stylesheet.base as unknown as TokenStyle)
66
+ }
67
+
42
68
  const tokenScopes = Array.isArray(token.scope) ? token.scope : [token.scope]
43
- const tokenStylesArray = tokenScopes.map((scope: string) => stylesheet[scope] || {})
44
- const combinedStyles = StyleSheet.flatten([
45
- ...tokenStylesArray,
46
- {
47
- fontFamily: monospaceFont,
48
- ...(token.color && { color: token.color }),
49
- ...(token.fontStyle === 'italic' && stylesheet.italic),
50
- ...(token.fontWeight === 'bold' && stylesheet.bold),
51
- },
52
- ])
53
-
54
- console.log(`Rendering token: ${token.content}, styles:`, combinedStyles)
69
+ tokenScopes.forEach((scope: string) => {
70
+ if (stylesheet[scope]) {
71
+ styles.push(stylesheet[scope] as unknown as TokenStyle)
72
+ }
73
+ })
74
+
75
+ const tokenStyles: TokenStyle = {}
76
+ if (token.color) {
77
+ tokenStyles.color = token.color
78
+ }
79
+ if (token.fontStyle === 'italic') {
80
+ tokenStyles.fontStyle = 'italic'
81
+ }
82
+ if (token.fontWeight === 'bold') {
83
+ tokenStyles.fontWeight = 'bold'
84
+ }
85
+
86
+ if (Object.keys(tokenStyles).length > 0) {
87
+ styles.push(tokenStyles)
88
+ }
89
+
90
+ const combinedStyles = StyleSheet.flatten(styles)
55
91
 
56
92
  return (
57
93
  <Text key={`${lineIndex}-${index}`} style={combinedStyles}>
@@ -60,19 +96,17 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
60
96
  )
61
97
  }
62
98
 
63
- // In the renderLine and renderToken flow
64
- const renderLine = (line: any[], lineIndex: number) => {
65
- console.log(`Rendering line ${lineIndex}:`, line)
66
- return (
67
- <View key={lineIndex} style={styles.lineContainer}>
68
- {line.map((token, tokenIndex) => renderToken(token, tokenIndex, lineIndex))}
69
- </View>
70
- )
71
- }
99
+ const renderLine = (line: any[], lineIndex: number) => (
100
+ <View key={lineIndex} style={baseStyles.lineContainer}>
101
+ {line.map((token, tokenIndex) => renderToken(token, tokenIndex, lineIndex))}
102
+ </View>
103
+ )
72
104
 
73
105
  return (
74
- <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={styles.scrollView}>
75
- <View onStartShouldSetResponder={() => true}>{tokens.map((line, index) => renderLine(line, index))}</View>
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>
76
110
  </ScrollView>
77
111
  )
78
112
  }