1
1
import React from 'react' ;
2
2
import { Text , ScrollView , Platform } from 'react-native' ;
3
3
import SyntaxHighlighter from 'react-syntax-highlighter' ;
4
- import { createStyleObject } from 'react-syntax-highlighter/dist/create-element' ;
5
- import { defaultStyle } from 'react-syntax-highlighter/dist/styles' ;
6
-
4
+ import SyntaxHighlighterPrism from 'react-syntax-highlighter/prism' ;
5
+ import { createStyleObject } from 'react-syntax-highlighter/create-element' ;
6
+ import { defaultStyle } from 'react-syntax-highlighter/styles/hljs' ;
7
+ import { prism as prismDefaultStyle } from 'react-syntax-highlighter/styles/prism' ;
7
8
8
9
const styleCache = new Map ( ) ;
9
10
10
- function generateNewStylesheet ( stylesheet ) {
11
+ function generateNewStylesheet ( { stylesheet, renderer } ) {
11
12
if ( styleCache . has ( stylesheet ) ) {
12
13
return styleCache . get ( stylesheet ) ;
13
14
}
@@ -33,7 +34,17 @@ function generateNewStylesheet(stylesheet) {
33
34
} , { } ) ;
34
35
return newStylesheet ;
35
36
} , { } ) ;
36
- const defaultColor = transformedStyle . hljs && transformedStyle . hljs . color || '#000' ;
37
+ const defaultColor = (
38
+ renderer === "prism"
39
+ ?
40
+ (
41
+ transformedStyle [ 'pre[class*=\"language-\"]' ] &&
42
+ transformedStyle [ 'pre[class*=\"language-\"]' ] . color ||
43
+ '#000'
44
+ )
45
+ :
46
+ transformedStyle . hljs && transformedStyle . hljs . color || '#000'
47
+ ) ;
37
48
if ( transformedStyle . hljs && transformedStyle . hljs . color ) {
38
49
delete transformedStyle . hljs . color ;
39
50
}
@@ -96,10 +107,24 @@ function nativeRenderer({ defaultColor, fontFamily, fontSize }) {
96
107
}
97
108
98
109
99
- function NativeSyntaxHighlighter ( { fontFamily, fontSize, style, children, ...rest } ) {
110
+ function NativeSyntaxHighlighter ( {
111
+ fontFamily,
112
+ fontSize,
113
+ children,
114
+ highlighter = "highlightjs" ,
115
+ style = highlighter === "prism" ? prismDefaultStyle : defaultStyle ,
116
+ ...rest
117
+ } ) {
100
118
const { transformedStyle, defaultColor } = generateNewStylesheet ( style ) ;
119
+ const Highlighter = (
120
+ highlighter === "prism"
121
+ ?
122
+ SyntaxHighlighterPrism
123
+ :
124
+ SyntaxHighlighter
125
+ ) ;
101
126
return (
102
- < SyntaxHighlighter
127
+ < Highlighter
103
128
{ ...rest }
104
129
style = { transformedStyle }
105
130
horizontal = { true }
@@ -110,14 +135,13 @@ function NativeSyntaxHighlighter({ fontFamily, fontSize, style, children, ...res
110
135
} ) ) }
111
136
>
112
137
{ children }
113
- </ SyntaxHighlighter >
138
+ </ Highlighter >
114
139
) ;
115
140
}
116
141
117
142
NativeSyntaxHighlighter . defaultProps = {
118
143
fontFamily : Platform . OS === 'ios' ? 'Menlo-Regular' : 'monospace' ,
119
144
fontSize : 12 ,
120
- style : defaultStyle ,
121
145
PreTag : ScrollView ,
122
146
CodeTag : ScrollView
123
147
} ;
0 commit comments