rn-shiki 0.0.37-23 → 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-23",
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
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,21 +11,33 @@ const monospaceFont = Platform.select({
12
11
  default: 'monospace',
13
12
  })
14
13
 
15
- interface TokenStyle extends Omit<TextStyle, 'color'> {
14
+ interface TokenStyle extends Omit<TextStyle, 'color' | 'fontWeight'> {
16
15
  color?: ColorValue
16
+ fontStyle?: 'normal' | 'italic'
17
+ fontWeight?: FontWeight
17
18
  fontFamily?: string
18
19
  }
19
20
 
20
- interface ThemeStyles {
21
- token: TokenStyle
21
+ interface Styles {
22
22
  [key: string]: TokenStyle
23
23
  }
24
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[]
35
+ }
36
+
25
37
  const baseStyles = StyleSheet.create({
26
38
  scrollView: {
27
39
  flex: 1,
28
40
  minHeight: 20,
29
- backgroundColor: '#24292e', // GitHub Dark default background
30
41
  },
31
42
  lineContainer: {
32
43
  flexDirection: 'row',
@@ -37,21 +48,30 @@ const baseStyles = StyleSheet.create({
37
48
  token: {
38
49
  fontFamily: monospaceFont,
39
50
  fontSize: 14,
40
- color: '#e1e4e8', // GitHub Dark default text color
41
51
  },
42
52
  container: {
43
53
  flex: 1,
44
54
  },
45
55
  })
46
56
 
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,
57
+ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({
58
+ text,
59
+ language,
60
+ languageId,
61
+ theme,
62
+ fontSize = 14,
63
+ }) => {
64
+ const stylesheet = useMemo(() => {
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
+ },
52
71
  token: {
53
72
  ...baseStyles.token,
54
73
  fontSize,
74
+ color: (theme as Theme).colors?.['editor.foreground'] || '#d4d4d4',
55
75
  },
56
76
  })
57
77
  }, [theme, fontSize])
@@ -64,29 +84,33 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
64
84
  })
65
85
 
66
86
  const renderToken = (token: any, index: number, lineIndex: number) => {
67
- // Start with the base token style
68
- const tokenStyles: StyleProp<TokenStyle>[] = [styles.token]
87
+ const styles: StyleProp<TokenStyle>[] = [stylesheet.token]
69
88
 
70
89
  // Add token-specific styles from stylesheet based on scope
71
90
  const tokenScopes = Array.isArray(token.scope) ? token.scope : [token.scope]
72
91
  tokenScopes.forEach((scope: string) => {
73
- if (styles[scope]) {
74
- tokenStyles.push(styles[scope])
92
+ if (stylesheet[scope]) {
93
+ styles.push(stylesheet[scope])
75
94
  }
76
95
  })
77
96
 
78
97
  // Add token-specific styles from the token itself
98
+ const tokenStyles: TokenStyle = {}
79
99
  if (token.color) {
80
- tokenStyles.push({ color: token.color })
100
+ tokenStyles.color = token.color
81
101
  }
82
102
  if (token.fontStyle === 'italic') {
83
- tokenStyles.push({ fontStyle: 'italic' })
103
+ tokenStyles.fontStyle = 'italic'
84
104
  }
85
105
  if (token.fontWeight === 'bold') {
86
- tokenStyles.push({ fontWeight: 'bold' })
106
+ tokenStyles.fontWeight = 'bold'
107
+ }
108
+
109
+ if (Object.keys(tokenStyles).length > 0) {
110
+ styles.push(tokenStyles)
87
111
  }
88
112
 
89
- const finalStyle = StyleSheet.flatten(tokenStyles)
113
+ const finalStyle = StyleSheet.flatten(styles)
90
114
 
91
115
  return (
92
116
  <Text key={`${lineIndex}-${index}`} style={finalStyle}>
@@ -102,7 +126,12 @@ const SyntaxHighlighter: React.FC<SyntaxHighlighterProps> = ({ text, language, l
102
126
  )
103
127
 
104
128
  return (
105
- <ScrollView horizontal showsHorizontalScrollIndicator={Platform.OS !== 'web'} style={baseStyles.scrollView} contentContainerStyle={baseStyles.container}>
129
+ <ScrollView
130
+ horizontal
131
+ showsHorizontalScrollIndicator={Platform.OS !== 'web'}
132
+ style={[baseStyles.scrollView, stylesheet.editor]}
133
+ contentContainerStyle={baseStyles.container}
134
+ >
106
135
  {tokens.map((line, index) => renderLine(line, index))}
107
136
  </ScrollView>
108
137
  )