Skip to content

Commit 207a8d0

Browse files
committed
feat: 导出 mdParser 和 parseMd 函数,增强 Markdown 解析功能;调整 SlideRenderer 的 getSlidesInfo 方法为公共方法
1 parent 8580e20 commit 207a8d0

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/compiler/md.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { extractTemplateBody } from './utils'
1212
import { compileVue } from './vue'
1313
// import '@slidev/theme-seriph/styles/index'
1414

15-
const mdParser = new MarkdownIt({ quotes: '""\'\'', html: true, xhtmlOut: true, linkify: true })
15+
export const mdParser = new MarkdownIt({ quotes: '""\'\'', html: true, xhtmlOut: true, linkify: true })
1616
mdParser.use(MarkdownItFootnote)
1717
mdParser.use(MarkdownItMdc)
1818

19-
function parseMd(code: string) {
19+
export function parseMd(code: string) {
2020
return `<template><div>${mdParser.render(code)}</div></template>
2121
<script setup>
2222
</script>`

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { compileCss, compileMd, compileVueSFC } from './compiler/md'
1+
export { compileCss, compileMd, compileVueSFC, mdParser, parseMd } from './compiler/md'
22
export { generateUnoCss, UnoGenerator } from './compiler/uno'
33
export { compileVue } from './compiler/vue'
44
export { default as RemoteComp } from './components/RemoteComp.vue'

src/slides.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class SlideRenderer {
4343
this.unoGenerator = new UnoGenerator(options.unoConfig)
4444
}
4545

46-
private getSlidesInfo(slidesSource: SlideSource[]): SlideInfo[] {
46+
getSlidesInfo(slidesSource: SlideSource[]): SlideInfo[] {
4747
return slidesSource.map(
4848
(s, i) => ({
4949
frontmatter: s.frontmatter,

test/index.test.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { generateUnoCss } from '../src/compiler/uno'
3-
import { SlideRenderer } from '../src/index'
2+
import { compileVueSFC, generateUnoCss, SlideRenderer } from '../src/index'
43

54
describe('slide compiler css', async () => {
65
const slidesSource = [
@@ -35,3 +34,33 @@ describe('unocss generate', async () => {
3534
.mt-5{margin-top:1.25rem;}`)
3635
})
3736
})
37+
38+
describe('vue sfc generate', async () => {
39+
const renderer = new SlideRenderer()
40+
const slidesData = renderer.parse(`---
41+
layout: fact
42+
---
43+
44+
# 生成伪代码样式
45+
46+
<div bg-pink:10 border="~ pink/50 rounded-lg" op40>
47+
<div flex="~ items-center gap-2" bg-pink:10 px4 py2 rounded><div i-ph:number-circle-four text-xl /> 代码实时渲染执行</div>
48+
49+
<div ml2 p2 text-pink2>
50+
51+
- 将 MDC 代码转换为 Web 渲染可用的格式
52+
- 静态编译
53+
- 动态渲染
54+
55+
</div>
56+
</div>`)
57+
const output = await compileVueSFC({
58+
slidesInfo: renderer.getSlidesInfo(slidesData),
59+
code: slidesData[0].content,
60+
filename: '__slidev_0.md',
61+
})
62+
// it('exported', () => {
63+
// expect(output).toEqual(`/* layer: default */
64+
// .mt-5{margin-top:1.25rem;}`)
65+
// })
66+
})

0 commit comments

Comments
 (0)