1
- import React , { memo , useEffect , useState } from "react" ;
2
- import { useSelector } from "react-redux" ;
1
+ import { memo , useEffect } from "react" ;
3
2
import { useRemark } from "react-remark" ;
4
- import rehypeHighlight from "rehype-highlight" ;
3
+ import rehypeHighlight , { Options } from "rehype-highlight" ;
5
4
import rehypeKatex from "rehype-katex" ;
6
5
import remarkMath from "remark-math" ;
7
6
import styled from "styled-components" ;
@@ -12,11 +11,11 @@ import {
12
11
vscEditorBackground ,
13
12
vscForeground ,
14
13
} from ".." ;
15
- import { RootState } from "../../redux/store" ;
16
14
import { getFontSize } from "../../util" ;
17
15
import LinkableCode from "./LinkableCode" ;
18
16
import PreWithToolbar from "./PreWithToolbar" ;
19
17
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre" ;
18
+ import { common } from "lowlight" ;
20
19
import "./katex.css" ;
21
20
import "./markdown.css" ;
22
21
@@ -59,19 +58,9 @@ const StyledMarkdown = styled.div<{
59
58
}
60
59
61
60
background-color: ${ vscBackground } ;
62
- font-family:
63
- var(--vscode-font-family),
64
- system-ui,
65
- -apple-system,
66
- BlinkMacSystemFont,
67
- "Segoe UI",
68
- Roboto,
69
- Oxygen,
70
- Ubuntu,
71
- Cantarell,
72
- "Open Sans",
73
- "Helvetica Neue",
74
- sans-serif;
61
+ font-family: var(--vscode-font-family), system-ui, -apple-system,
62
+ BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell,
63
+ "Open Sans", "Helvetica Neue", sans-serif;
75
64
font-size: ${ ( props ) => props . fontSize || getFontSize ( ) } px;
76
65
padding-left: 8px;
77
66
padding-right: 8px;
@@ -92,41 +81,19 @@ interface StyledMarkdownPreviewProps {
92
81
scrollLocked ?: boolean ;
93
82
}
94
83
95
- const FadeInWords : React . FC = ( props : any ) => {
96
- const { children, ...otherProps } = props ;
84
+ const HLJS_LANGUAGE_CLASSNAME_PREFIX = "language-" ;
97
85
98
- const active = useSelector ( ( store : RootState ) => store . state . active ) ;
86
+ const getLanuageFromClassName = ( className : any ) : string | null => {
87
+ if ( ! className || typeof className !== "string" ) {
88
+ return null ;
89
+ }
99
90
100
- const [ textWhenActiveStarted , setTextWhenActiveStarted ] = useState (
101
- props . children ,
102
- ) ;
91
+ const language = className
92
+ . split ( " " )
93
+ . find ( ( word ) => word . startsWith ( HLJS_LANGUAGE_CLASSNAME_PREFIX ) )
94
+ ?. split ( "-" ) [ 1 ] ;
103
95
104
- useEffect ( ( ) => {
105
- if ( active ) {
106
- setTextWhenActiveStarted ( children ) ;
107
- }
108
- } , [ active ] ) ;
109
-
110
- // Split the text into words
111
- const words = children
112
- . map ( ( child ) => {
113
- if ( typeof child === "string" ) {
114
- return child . split ( " " ) . map ( ( word , index ) => (
115
- < span className = "fade-in-span" key = { index } >
116
- { word } { " " }
117
- </ span >
118
- ) ) ;
119
- } else {
120
- return < span className = "fade-in-span" > { child } </ span > ;
121
- }
122
- } )
123
- . flat ( ) ;
124
-
125
- return active && children !== textWhenActiveStarted ? (
126
- < p { ...otherProps } > { words } </ p >
127
- ) : (
128
- < p > { children } </ p >
129
- ) ;
96
+ return language ;
130
97
} ;
131
98
132
99
const StyledMarkdownPreview = memo ( function StyledMarkdownPreview (
@@ -147,7 +114,21 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
147
114
} ;
148
115
} ,
149
116
] ,
150
- rehypePlugins : [ rehypeHighlight as any , { } , rehypeKatex as any , { } ] ,
117
+ rehypePlugins : [
118
+ rehypeKatex as any ,
119
+ { } ,
120
+ [
121
+ rehypeHighlight as any ,
122
+ {
123
+ // Note: This is the default behavior, but leaving this here for scaffolding to
124
+ // add unsupported languages in the future.
125
+ // https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
126
+ languages : { ...common } ,
127
+ } as Options ,
128
+
129
+ ,
130
+ ] ,
131
+ ] ,
151
132
rehypeReactOptions : {
152
133
components : {
153
134
a : ( { node, ...props } ) => {
@@ -158,12 +139,12 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
158
139
) ;
159
140
} ,
160
141
pre : ( { node, ...preProps } ) => {
161
- const language = preProps ?. children ?. [ 0 ] ?. props ?. className
162
- ?. split ( " " )
163
- . find ( ( word ) => word . startsWith ( "language-" ) )
164
- ?. split ( "-" ) [ 1 ] ;
142
+ const childrenClassName = preProps ?. children ?. [ 0 ] ?. props ?. className ;
143
+
165
144
return props . showCodeBorder ? (
166
- < PreWithToolbar language = { language } >
145
+ < PreWithToolbar
146
+ language = { getLanuageFromClassName ( childrenClassName ) }
147
+ >
167
148
< SyntaxHighlightedPre { ...preProps } > </ SyntaxHighlightedPre >
168
149
</ PreWithToolbar >
169
150
) : (
@@ -181,31 +162,6 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
181
162
< LinkableCode { ...codeProps } > { codeProps . children } </ LinkableCode >
182
163
) ;
183
164
} ,
184
- // pre: ({ node, ...preProps }) => {
185
- // const codeString =
186
- // preProps.children?.[0]?.props?.children?.[0].trim() || "";
187
- // const monacoEditor = (
188
- // <MonacoCodeBlock
189
- // showBorder={props.showCodeBorder}
190
- // language={
191
- // preProps.children?.[0]?.props?.className?.split("-")[1] ||
192
- // "typescript"
193
- // }
194
- // preProps={preProps}
195
- // codeString={codeString}
196
- // />
197
- // );
198
- // return props.showCodeBorder ? (
199
- // <PreWithToolbar copyvalue={codeString}>
200
- // <SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
201
- // </PreWithToolbar>
202
- // ) : (
203
- // <SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
204
- // );
205
- // },
206
- // p: ({ node, ...props }) => {
207
- // return <FadeInWords {...props}></FadeInWords>;
208
- // },
209
165
} ,
210
166
} ,
211
167
} ) ;
0 commit comments