rn-shiki 0.0.35 → 0.0.36
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { SyntaxHighlighterProps } from 'src/types/shiki'
|
2
2
|
import React from 'react'
|
3
|
-
import { Platform, Text, View } from 'react-native'
|
3
|
+
import { Platform, ScrollView, StyleSheet, Text, View } from 'react-native'
|
4
4
|
import { useSyntaxHighlighter } from '../../hooks/useSyntaxHighlighter'
|
5
5
|
import SyntaxLine from './SyntaxLine'
|
6
6
|
|
@@ -10,6 +10,28 @@ const monospaceFont = Platform.select({
|
|
10
10
|
default: 'monospace',
|
11
11
|
})
|
12
12
|
|
13
|
+
const styles = StyleSheet.create({
|
14
|
+
container: {
|
15
|
+
flex: 1,
|
16
|
+
minHeight: 20,
|
17
|
+
},
|
18
|
+
scrollContent: {
|
19
|
+
flexGrow: 1,
|
20
|
+
padding: 8,
|
21
|
+
},
|
22
|
+
contentContainer: {
|
23
|
+
minWidth: '100%',
|
24
|
+
},
|
25
|
+
errorText: {
|
26
|
+
padding: 8,
|
27
|
+
color: '#ff6b6b',
|
28
|
+
},
|
29
|
+
loadingText: {
|
30
|
+
padding: 8,
|
31
|
+
color: '#666666',
|
32
|
+
},
|
33
|
+
})
|
34
|
+
|
13
35
|
function SyntaxHighlighter({ text, language, languageId, theme, fontSize = 14 }: SyntaxHighlighterProps & { fontSize?: number }) {
|
14
36
|
const { tokens, error, isLoading } = useSyntaxHighlighter({
|
15
37
|
text,
|
@@ -21,12 +43,13 @@ function SyntaxHighlighter({ text, language, languageId, theme, fontSize = 14 }:
|
|
21
43
|
if (error) {
|
22
44
|
return (
|
23
45
|
<Text
|
24
|
-
style={
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
46
|
+
style={[
|
47
|
+
styles.errorText,
|
48
|
+
{
|
49
|
+
fontFamily: monospaceFont,
|
50
|
+
fontSize,
|
51
|
+
},
|
52
|
+
]}
|
30
53
|
>
|
31
54
|
Error:
|
32
55
|
{' '}
|
@@ -38,12 +61,13 @@ function SyntaxHighlighter({ text, language, languageId, theme, fontSize = 14 }:
|
|
38
61
|
if (isLoading) {
|
39
62
|
return (
|
40
63
|
<Text
|
41
|
-
style={
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
64
|
+
style={[
|
65
|
+
styles.loadingText,
|
66
|
+
{
|
67
|
+
fontFamily: monospaceFont,
|
68
|
+
fontSize,
|
69
|
+
},
|
70
|
+
]}
|
47
71
|
>
|
48
72
|
Loading...
|
49
73
|
</Text>
|
@@ -51,19 +75,13 @@ function SyntaxHighlighter({ text, language, languageId, theme, fontSize = 14 }:
|
|
51
75
|
}
|
52
76
|
|
53
77
|
return (
|
54
|
-
<
|
55
|
-
{
|
56
|
-
|
57
|
-
key={`line-${lineIndex}`}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
}}
|
62
|
-
>
|
63
|
-
<SyntaxLine line={line} fontSize={fontSize} />
|
64
|
-
</View>
|
65
|
-
))}
|
66
|
-
</View>
|
78
|
+
<ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={styles.container} contentContainerStyle={styles.scrollContent}>
|
79
|
+
<View style={styles.contentContainer} onStartShouldSetResponder={() => true}>
|
80
|
+
{tokens.map((line, lineIndex) => (
|
81
|
+
<SyntaxLine key={`line-${lineIndex}`} line={line} fontSize={fontSize} />
|
82
|
+
))}
|
83
|
+
</View>
|
84
|
+
</ScrollView>
|
67
85
|
)
|
68
86
|
}
|
69
87
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { TokenType } from '../../types'
|
2
2
|
import React from 'react'
|
3
|
-
import { Platform, Text, View } from 'react-native'
|
3
|
+
import { Platform, StyleSheet, Text, View } from 'react-native'
|
4
4
|
|
5
5
|
const monospaceFont = Platform.select({
|
6
6
|
ios: 'Menlo',
|
@@ -8,56 +8,59 @@ const monospaceFont = Platform.select({
|
|
8
8
|
default: 'monospace',
|
9
9
|
})
|
10
10
|
|
11
|
+
const styles = StyleSheet.create({
|
12
|
+
lineContainer: {
|
13
|
+
flexDirection: 'row',
|
14
|
+
flexWrap: 'wrap',
|
15
|
+
},
|
16
|
+
token: {
|
17
|
+
includeFontPadding: false,
|
18
|
+
},
|
19
|
+
})
|
20
|
+
|
11
21
|
interface SyntaxLineProps {
|
12
22
|
line: TokenType[]
|
13
23
|
fontSize?: number
|
14
24
|
}
|
15
25
|
|
16
26
|
function SyntaxLine({ line, fontSize = 14 }: SyntaxLineProps) {
|
27
|
+
const lineHeight = fontSize * 1.5
|
28
|
+
|
17
29
|
return (
|
18
30
|
<View
|
19
|
-
style={
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
31
|
+
style={[
|
32
|
+
styles.lineContainer,
|
33
|
+
{
|
34
|
+
minHeight: lineHeight,
|
35
|
+
},
|
36
|
+
]}
|
24
37
|
>
|
25
38
|
{line.map((token, tokenIndex) => {
|
26
39
|
const content = token.content.replace(/ /g, '\u00A0')
|
27
40
|
return (
|
28
41
|
<Text
|
29
42
|
key={`${tokenIndex}-${content.slice(0, 8)}`}
|
30
|
-
style={
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
style={[
|
44
|
+
styles.token,
|
45
|
+
{
|
46
|
+
color: token.color || '#FFFFFF',
|
47
|
+
fontFamily: monospaceFont,
|
48
|
+
fontSize,
|
49
|
+
fontStyle: token.fontStyle as 'normal' | 'italic',
|
50
|
+
fontWeight: token.fontWeight as 'normal' | 'bold',
|
51
|
+
lineHeight,
|
52
|
+
...(Platform.OS === 'ios'
|
53
|
+
? {
|
54
|
+
paddingTop: 1,
|
55
|
+
}
|
56
|
+
: {}),
|
57
|
+
},
|
58
|
+
]}
|
45
59
|
>
|
46
60
|
{content}
|
47
61
|
</Text>
|
48
62
|
)
|
49
63
|
})}
|
50
|
-
<Text
|
51
|
-
style={{
|
52
|
-
fontFamily: monospaceFont,
|
53
|
-
fontSize,
|
54
|
-
color: 'transparent',
|
55
|
-
lineHeight: fontSize * 1.5,
|
56
|
-
height: fontSize * 1.5,
|
57
|
-
}}
|
58
|
-
>
|
59
|
-
{'\n'}
|
60
|
-
</Text>
|
61
64
|
</View>
|
62
65
|
)
|
63
66
|
}
|