@@ -2,14 +2,18 @@ import type { ExtendedProperties, IOpts, ThemeStyles } from '@/types'
2
2
import type { PropertiesHyphen } from 'csstype'
3
3
import type { Renderer , RendererObject , Tokens } from 'marked'
4
4
import { cloneDeep , toMerged } from 'es-toolkit'
5
- import hljs from 'highlight.js '
5
+ import frontMatter from 'front-matter '
6
6
7
+ import hljs from 'highlight.js'
7
8
import { marked } from 'marked'
8
9
import mermaid from 'mermaid'
9
10
import { getStyleString } from '.'
10
11
import markedAlert from './MDAlert'
11
12
import { MDKatex } from './MDKatex'
12
13
14
+ marked . setOptions ( {
15
+ breaks : true ,
16
+ } )
13
17
marked . use ( MDKatex ( { nonStandard : true } ) )
14
18
15
19
function buildTheme ( { theme : _theme , fonts, size, isUseIndent } : IOpts ) : ThemeStyles {
@@ -107,6 +111,19 @@ export function initRenderer(opts: IOpts) {
107
111
return getStyles ( styleMapping , tag , addition )
108
112
}
109
113
114
+ function parseFrontMatterAndContent ( markdownText : string ) {
115
+ try {
116
+ const parsed = frontMatter ( markdownText )
117
+ const yamlData = parsed . attributes
118
+ const markdownContent = parsed . body
119
+ return { yamlData, markdownContent }
120
+ }
121
+ catch ( error ) {
122
+ console . error ( `Error parsing front-matter:` , error )
123
+ return { yamlData : { } , markdownContent : markdownText }
124
+ }
125
+ }
126
+
110
127
function styledContent ( styleLabel : string , content : string , tagName ?: string ) : string {
111
128
const tag = tagName ?? styleLabel
112
129
return `<${ tag } ${ styles ( styleLabel ) } >${ content } </${ tag } >`
@@ -175,7 +192,7 @@ export function initRenderer(opts: IOpts) {
175
192
const language = hljs . getLanguage ( langText ) ? langText : `plaintext`
176
193
let highlighted = hljs . highlight ( text , { language } ) . value
177
194
// tab to 4 spaces
178
- highlighted = highlighted . replace ( / \t / g, ' ' )
195
+ highlighted = highlighted . replace ( / \t / g, ` ` )
179
196
highlighted = highlighted
180
197
. replace ( / \r \n / g, `<br/>` )
181
198
. replace ( / \n / g, `<br/>` )
@@ -186,7 +203,8 @@ export function initRenderer(opts: IOpts) {
186
203
} ,
187
204
188
205
codespan ( { text } : Tokens . Codespan ) : string {
189
- return styledContent ( `codespan` , text , `code` )
206
+ const escapedText = text . replace ( / < / g, `<` ) . replace ( / > / g, `>` )
207
+ return styledContent ( `codespan` , escapedText , `code` )
190
208
} ,
191
209
192
210
listitem ( item : Tokens . ListItem ) : string {
@@ -276,6 +294,7 @@ export function initRenderer(opts: IOpts) {
276
294
buildFootnotes,
277
295
setOptions,
278
296
reset,
297
+ parseFrontMatterAndContent,
279
298
createContainer ( content : string ) {
280
299
return styledContent ( `container` , content , `section` )
281
300
} ,
0 commit comments