rn-shiki 0.0.37-22 → 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,4 +1,4 @@
|
|
1
|
-
import type { ColorValue, StyleProp, TextStyle
|
1
|
+
import type { ColorValue, StyleProp, TextStyle } from 'react-native'
|
2
2
|
import type { SyntaxHighlighterProps } from 'src/types/shiki'
|
3
3
|
import type { ReactStyle } from '../../utils/style-transformer'
|
4
4
|
import React, { useMemo } from 'react'
|
@@ -12,44 +12,49 @@ const monospaceFont = Platform.select({
|
|
12
12
|
default: 'monospace',
|
13
13
|
})
|
14
14
|
|
15
|
-
interface
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
20
23
|
}
|
21
24
|
|
22
|
-
const baseStyles = StyleSheet.create
|
25
|
+
const baseStyles = StyleSheet.create({
|
23
26
|
scrollView: {
|
24
27
|
flex: 1,
|
25
28
|
minHeight: 20,
|
29
|
+
backgroundColor: '#24292e', // GitHub Dark default background
|
26
30
|
},
|
27
31
|
lineContainer: {
|
28
32
|
flexDirection: 'row',
|
29
33
|
flexWrap: 'wrap',
|
34
|
+
paddingHorizontal: 16,
|
35
|
+
paddingVertical: 8,
|
30
36
|
},
|
31
37
|
token: {
|
32
38
|
fontFamily: monospaceFont,
|
39
|
+
fontSize: 14,
|
40
|
+
color: '#e1e4e8', // GitHub Dark default text color
|
33
41
|
},
|
34
42
|
container: {
|
35
43
|
flex: 1,
|
36
44
|
},
|
37
45
|
})
|
38
46
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
const styles = getRNStylesFromShikiStyle(theme as ReactStyle)
|
51
|
-
return StyleSheet.create(styles)
|
52
|
-
}, [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])
|
53
58
|
|
54
59
|
const { tokens } = useSyntaxHighlighter({
|
55
60
|
text,
|
@@ -59,38 +64,32 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
|
|
59
64
|
})
|
60
65
|
|
61
66
|
const renderToken = (token: any, index: number, lineIndex: number) => {
|
62
|
-
|
63
|
-
|
64
|
-
if (stylesheet.base) {
|
65
|
-
styles.push(stylesheet.base as unknown as TokenStyle)
|
66
|
-
}
|
67
|
+
// Start with the base token style
|
68
|
+
const tokenStyles: StyleProp<TokenStyle>[] = [styles.token]
|
67
69
|
|
70
|
+
// Add token-specific styles from stylesheet based on scope
|
68
71
|
const tokenScopes = Array.isArray(token.scope) ? token.scope : [token.scope]
|
69
72
|
tokenScopes.forEach((scope: string) => {
|
70
|
-
if (
|
71
|
-
|
73
|
+
if (styles[scope]) {
|
74
|
+
tokenStyles.push(styles[scope])
|
72
75
|
}
|
73
76
|
})
|
74
77
|
|
75
|
-
|
78
|
+
// Add token-specific styles from the token itself
|
76
79
|
if (token.color) {
|
77
|
-
tokenStyles.color
|
80
|
+
tokenStyles.push({ color: token.color })
|
78
81
|
}
|
79
82
|
if (token.fontStyle === 'italic') {
|
80
|
-
tokenStyles.fontStyle
|
83
|
+
tokenStyles.push({ fontStyle: 'italic' })
|
81
84
|
}
|
82
85
|
if (token.fontWeight === 'bold') {
|
83
|
-
tokenStyles.fontWeight
|
84
|
-
}
|
85
|
-
|
86
|
-
if (Object.keys(tokenStyles).length > 0) {
|
87
|
-
styles.push(tokenStyles)
|
86
|
+
tokenStyles.push({ fontWeight: 'bold' })
|
88
87
|
}
|
89
88
|
|
90
|
-
const
|
89
|
+
const finalStyle = StyleSheet.flatten(tokenStyles)
|
91
90
|
|
92
91
|
return (
|
93
|
-
<Text key={`${lineIndex}-${index}`} style={
|
92
|
+
<Text key={`${lineIndex}-${index}`} style={finalStyle}>
|
94
93
|
{token.content.replace(/ /g, '\u00A0')}
|
95
94
|
</Text>
|
96
95
|
)
|
@@ -103,10 +102,8 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
|
|
103
102
|
)
|
104
103
|
|
105
104
|
return (
|
106
|
-
<ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={
|
107
|
-
|
108
|
-
{tokens.map((line, index) => renderLine(line, index))}
|
109
|
-
</View>
|
105
|
+
<ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={baseStyles.scrollView} contentContainerStyle={baseStyles.container}>
|
106
|
+
{tokens.map((line, index) => renderLine(line, index))}
|
110
107
|
</ScrollView>
|
111
108
|
)
|
112
109
|
}
|