Skip to content

Commit 4f73811

Browse files
authored
fix: render codespan and front matter (#505)
* fix: render codespan * style: update hr label * feat: support front matter
1 parent 13f5aad commit 4f73811

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

package-lock.json

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"csstype": "^3.1.3",
3535
"es-toolkit": "^1.27.0",
3636
"form-data": "4.0.1",
37+
"front-matter": "^4.0.2",
3738
"highlight.js": "^11.10.0",
3839
"juice": "^11.0.0",
3940
"lucide-vue-next": "^0.462.0",

src/config/theme.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,14 @@ const defaultTheme: Theme = {
206206

207207
hr: {
208208
'border-style': `solid`,
209-
'border-width': `1px 0 0`,
209+
'border-width': `2px 0 0`,
210210
'border-color': `rgba(0,0,0,0.1)`,
211211
'-webkit-transform-origin': `0 0`,
212212
'-webkit-transform': `scale(1, 0.5)`,
213213
'transform-origin': `0 0`,
214214
'transform': `scale(1, 0.5)`,
215+
'height': `0.4em`,
216+
'margin': `1.5em 0`,
215217
},
216218
},
217219
inline: {

src/stores/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ export const useStore = defineStore(`store`, () => {
179179
codeThemeChange()
180180
renderer.reset({ citeStatus: isCiteStatus.value, legend: legend.value, isUseIndent: isUseIndent.value })
181181

182-
let outputTemp = marked.parse(editor.value!.getValue()) as string
182+
const { markdownContent } = renderer.parseFrontMatterAndContent(editor.value!.getValue())
183+
let outputTemp = marked.parse(markdownContent) as string
183184

184185
// 去除第一行的 margin-top
185186
outputTemp = outputTemp.replace(/(style=".*?)"/, `$1;margin-top: 0"`)

src/utils/renderer.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import type { ExtendedProperties, IOpts, ThemeStyles } from '@/types'
22
import type { PropertiesHyphen } from 'csstype'
33
import type { Renderer, RendererObject, Tokens } from 'marked'
44
import { cloneDeep, toMerged } from 'es-toolkit'
5-
import hljs from 'highlight.js'
5+
import frontMatter from 'front-matter'
66

7+
import hljs from 'highlight.js'
78
import { marked } from 'marked'
89
import mermaid from 'mermaid'
910
import { getStyleString } from '.'
1011
import markedAlert from './MDAlert'
1112
import { MDKatex } from './MDKatex'
1213

14+
marked.setOptions({
15+
breaks: true,
16+
})
1317
marked.use(MDKatex({ nonStandard: true }))
1418

1519
function buildTheme({ theme: _theme, fonts, size, isUseIndent }: IOpts): ThemeStyles {
@@ -107,6 +111,19 @@ export function initRenderer(opts: IOpts) {
107111
return getStyles(styleMapping, tag, addition)
108112
}
109113

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+
110127
function styledContent(styleLabel: string, content: string, tagName?: string): string {
111128
const tag = tagName ?? styleLabel
112129
return `<${tag} ${styles(styleLabel)}>${content}</${tag}>`
@@ -175,7 +192,7 @@ export function initRenderer(opts: IOpts) {
175192
const language = hljs.getLanguage(langText) ? langText : `plaintext`
176193
let highlighted = hljs.highlight(text, { language }).value
177194
// tab to 4 spaces
178-
highlighted = highlighted.replace(/\t/g, ' ')
195+
highlighted = highlighted.replace(/\t/g, ` `)
179196
highlighted = highlighted
180197
.replace(/\r\n/g, `<br/>`)
181198
.replace(/\n/g, `<br/>`)
@@ -186,7 +203,8 @@ export function initRenderer(opts: IOpts) {
186203
},
187204

188205
codespan({ text }: Tokens.Codespan): string {
189-
return styledContent(`codespan`, text, `code`)
206+
const escapedText = text.replace(/</g, `&lt;`).replace(/>/g, `&gt;`)
207+
return styledContent(`codespan`, escapedText, `code`)
190208
},
191209

192210
listitem(item: Tokens.ListItem): string {
@@ -276,6 +294,7 @@ export function initRenderer(opts: IOpts) {
276294
buildFootnotes,
277295
setOptions,
278296
reset,
297+
parseFrontMatterAndContent,
279298
createContainer(content: string) {
280299
return styledContent(`container`, content, `section`)
281300
},

0 commit comments

Comments
 (0)