@@ -8,13 +8,32 @@ import { prism as prismDefaultStyle } from'react-syntax-highlighter/styles/prism
8
8
9
9
const styleCache = new Map ( ) ;
10
10
11
+ const topLevelPropertiesToRemove = [
12
+ "color" ,
13
+ "textShadow" ,
14
+ "textAlign" ,
15
+ "whiteSpace" ,
16
+ "wordSpacing" ,
17
+ "wordBreak" ,
18
+ "wordWrap" ,
19
+ "lineHeight" ,
20
+ "MozTabSize" ,
21
+ "OTabSize" ,
22
+ "tabSize" ,
23
+ "WebkitHyphens" ,
24
+ "MozHyphens" ,
25
+ "msHyphens" ,
26
+ "hyphens" ,
27
+ "fontFamily"
28
+ ] ;
29
+
11
30
function generateNewStylesheet ( { stylesheet, highlighter } ) {
12
31
if ( styleCache . has ( stylesheet ) ) {
13
32
return styleCache . get ( stylesheet ) ;
14
33
}
15
34
const transformedStyle = Object . entries ( stylesheet ) . reduce ( ( newStylesheet , [ className , style ] ) => {
16
35
newStylesheet [ className ] = Object . entries ( style ) . reduce ( ( newStyle , [ key , value ] ) => {
17
- if ( key === 'overflowX' ) {
36
+ if ( key === 'overflowX' || key === "overflow" ) {
18
37
newStyle . overflow = value === 'auto' ? 'scroll' : value ;
19
38
}
20
39
else if ( value . includes ( 'em' ) ) {
@@ -34,20 +53,27 @@ function generateNewStylesheet({ stylesheet, highlighter }) {
34
53
} , { } ) ;
35
54
return newStylesheet ;
36
55
} , { } ) ;
37
- const defaultColor = (
38
- highlighter === "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
- ) ;
48
- if ( transformedStyle . hljs && transformedStyle . hljs . color ) {
49
- delete transformedStyle . hljs . color ;
50
- }
56
+ const topLevel = highlighter === "prism" ? transformedStyle [ 'pre[class*=\"language-\"]' ] : transformedStyle . hljs ;
57
+ const defaultColor = topLevel && topLevel . color || "#000" ;
58
+ topLevelPropertiesToRemove . forEach ( property => {
59
+ if ( topLevel [ property ] ) {
60
+ delete topLevel [ property ] ;
61
+ }
62
+ } ) ;
63
+ if ( topLevel . backgroundColor === "none" ) {
64
+ delete topLevel . backgroundColor ;
65
+ }
66
+ const codeLevel = transformedStyle [ 'code[class*=\"language-\"]' ] ;
67
+ if ( highlighter === "prism" && ! ! codeLevel ) {
68
+ topLevelPropertiesToRemove . forEach ( property => {
69
+ if ( codeLevel [ property ] ) {
70
+ delete codeLevel [ property ] ;
71
+ }
72
+ } ) ;
73
+ if ( codeLevel . backgroundColor === "none" ) {
74
+ delete codeLevel . backgroundColor ;
75
+ }
76
+ }
51
77
styleCache . set ( stylesheet , { transformedStyle, defaultColor } ) ;
52
78
return { transformedStyle, defaultColor } ;
53
79
}
0 commit comments