rn-shiki 0.0.37-21 → 0.0.37-23

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-23",
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 } 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,24 +12,49 @@ const monospaceFont = Platform.select({
11
12
  default: 'monospace',
12
13
  })
13
14
 
14
- const styles = StyleSheet.create({
15
+ interface TokenStyle extends Omit<TextStyle, 'color'> {
16
+ color?: ColorValue
17
+ fontFamily?: string
18
+ }
19
+
20
+ interface ThemeStyles {
21
+ token: TokenStyle
22
+ [key: string]: TokenStyle
23
+ }
24
+
25
+ const baseStyles = StyleSheet.create({
15
26
  scrollView: {
16
27
  flex: 1,
17
28
  minHeight: 20,
29
+ backgroundColor: '#24292e', // GitHub Dark default background
18
30
  },
19
31
  lineContainer: {
20
32
  flexDirection: 'row',
21
33
  flexWrap: 'wrap',
34
+ paddingHorizontal: 16,
35
+ paddingVertical: 8,
36
+ },
37
+ token: {
38
+ fontFamily: monospaceFont,
39
+ fontSize: 14,
40
+ color: '#e1e4e8', // GitHub Dark default text color
41
+ },
42
+ container: {
43
+ flex: 1,
22
44
  },
23
45
  })
24
46
 
25
- const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme }) => {
26
- console.log('theme:', theme)
27
- const stylesheet = useMemo(() => {
28
- const styles = getRNStylesFromShikiStyle(theme as ReactStyle)
29
- console.log('Generated stylesheet:', styles)
30
- return styles
31
- }, [theme])
47
+ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, languageId, theme, fontSize = 14 }) => {
48
+ const styles = useMemo(() => {
49
+ const themeStyles = getRNStylesFromShikiStyle(theme as ReactStyle)
50
+ return StyleSheet.create<ThemeStyles>({
51
+ ...themeStyles,
52
+ token: {
53
+ ...baseStyles.token,
54
+ fontSize,
55
+ },
56
+ })
57
+ }, [theme, fontSize])
32
58
 
33
59
  const { tokens } = useSyntaxHighlighter({
34
60
  text,
@@ -38,41 +64,46 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
38
64
  })
39
65
 
40
66
  const renderToken = (token: any, index: number, lineIndex: number) => {
41
- // Ensure to collect all styles associated with the token's scopes
67
+ // Start with the base token style
68
+ const tokenStyles: StyleProp<TokenStyle>[] = [styles.token]
69
+
70
+ // Add token-specific styles from stylesheet based on scope
42
71
  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
- ])
72
+ tokenScopes.forEach((scope: string) => {
73
+ if (styles[scope]) {
74
+ tokenStyles.push(styles[scope])
75
+ }
76
+ })
53
77
 
54
- console.log(`Rendering token: ${token.content}, styles:`, combinedStyles)
78
+ // Add token-specific styles from the token itself
79
+ if (token.color) {
80
+ tokenStyles.push({ color: token.color })
81
+ }
82
+ if (token.fontStyle === 'italic') {
83
+ tokenStyles.push({ fontStyle: 'italic' })
84
+ }
85
+ if (token.fontWeight === 'bold') {
86
+ tokenStyles.push({ fontWeight: 'bold' })
87
+ }
88
+
89
+ const finalStyle = StyleSheet.flatten(tokenStyles)
55
90
 
56
91
  return (
57
- <Text key={`${lineIndex}-${index}`} style={combinedStyles}>
92
+ <Text key={`${lineIndex}-${index}`} style={finalStyle}>
58
93
  {token.content.replace(/ /g, '\u00A0')}
59
94
  </Text>
60
95
  )
61
96
  }
62
97
 
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
- }
98
+ const renderLine = (line: any[], lineIndex: number) => (
99
+ <View key={lineIndex} style={baseStyles.lineContainer}>
100
+ {line.map((token, tokenIndex) => renderToken(token, tokenIndex, lineIndex))}
101
+ </View>
102
+ )
72
103
 
73
104
  return (
74
- <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={styles.scrollView}>
75
- <View onStartShouldSetResponder={() => true}>{tokens.map((line, index) => renderLine(line, index))}</View>
105
+ <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={baseStyles.scrollView} contentContainerStyle={baseStyles.container}>
106
+ {tokens.map((line, index) => renderLine(line, index))}
76
107
  </ScrollView>
77
108
  )
78
109
  }