Skip to content

Commit 6a86656

Browse files
committedNov 30, 2024
feat: switch to markdown-it-async
1 parent f254135 commit 6a86656

8 files changed

+65
-43
lines changed
 

Diff for: ‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"@rollup/pluginutils": "^5.1.3",
8585
"@types/markdown-it": "^14.1.2",
8686
"markdown-it": "^14.1.0",
87+
"markdown-it-async": "^0.1.3",
8788
"unplugin": "^1.16.0"
8889
},
8990
"devDependencies": {

Diff for: ‎pnpm-lock.yaml

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

Diff for: ‎src/core/markdown.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { MarkdownEnv, ResolvedOptions } from '../types'
33
import { toArray, uniq } from '@antfu/utils'
44
import { componentPlugin } from '@mdit-vue/plugin-component'
55
import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter'
6-
import MarkdownIt from 'markdown-it'
6+
import MarkdownIt from 'markdown-it-async'
77
import { preprocessHead } from './head'
88

99
const scriptSetupRE = /<\s*script([^>]*)\bsetup\b([^>]*)>([\s\S]*)<\/script>/g
@@ -61,10 +61,10 @@ function extractCustomBlock(html: string, options: ResolvedOptions) {
6161
return { html, blocks }
6262
}
6363

64-
export async function createMarkdown(options: ResolvedOptions) {
64+
export function createMarkdown(options: ResolvedOptions) {
6565
const isVue2 = options.vueVersion.startsWith('2.')
6666

67-
const markdown = new MarkdownIt({
67+
const markdown = MarkdownIt({
6868
html: true,
6969
linkify: true,
7070
typographer: true,
@@ -91,9 +91,13 @@ export async function createMarkdown(options: ResolvedOptions) {
9191
markdown.use(plugin, options)
9292
})
9393

94-
await options.markdownItSetup(markdown)
94+
const setupPromise = (async () => {
95+
await options.markdownItSetup(markdown)
96+
})()
97+
98+
return async (id: string, raw: string): Promise<TransformResult> => {
99+
await setupPromise
95100

96-
return (id: string, raw: string): TransformResult => {
97101
const {
98102
wrapperClasses,
99103
wrapperComponent,
@@ -106,7 +110,7 @@ export async function createMarkdown(options: ResolvedOptions) {
106110
raw = transforms.before?.(raw, id) ?? raw
107111

108112
const env: MarkdownEnv = { id }
109-
let html = markdown.render(raw, env)
113+
let html = await markdown.renderAsync(raw, env)
110114
const { excerpt = '', frontmatter: data = null } = env
111115

112116
const wrapperClassesResolved = toArray(

Diff for: ‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const unpluginFactory: UnpluginFactory<Options> = (userOptions = {}) => {
2424
},
2525
async transform(raw, id) {
2626
try {
27-
return (await markdownToVue)(id, raw)
27+
return await markdownToVue(id, raw)
2828
}
2929
catch (e: any) {
3030
this.error(e)
@@ -37,7 +37,7 @@ export const unpluginFactory: UnpluginFactory<Options> = (userOptions = {}) => {
3737

3838
const defaultRead = ctx.read
3939
ctx.read = async function () {
40-
return (await markdownToVue)(ctx.file, await defaultRead()).code
40+
return (await markdownToVue(ctx.file, await defaultRead())).code
4141
}
4242
},
4343
},

Diff for: ‎src/types.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { ComponentPluginOptions } from '@mdit-vue/plugin-component'
22
import type { FrontmatterPluginOptions } from '@mdit-vue/plugin-frontmatter'
33
import type { MarkdownItEnv } from '@mdit-vue/types'
44
import type { FilterPattern } from '@rollup/pluginutils'
5-
import type MarkdownIt from 'markdown-it'
5+
import type {
6+
MarkdownItAsync,
7+
MarkdownItAsyncOptions,
8+
PluginSimple as MarkdownItPluginSimple,
9+
PluginWithOptions as MarkdownItPluginWithOptions,
10+
} from 'markdown-it-async'
611
import type { preprocessHead } from './core/head'
712

813
/** a `<meta />` property in HTML is defined with the following name/values */
@@ -147,22 +152,22 @@ export interface Options {
147152
/**
148153
* Options passed to Markdown It
149154
*/
150-
markdownItOptions?: MarkdownIt.Options
155+
markdownItOptions?: MarkdownItAsyncOptions
151156

152157
/**
153158
* Plugins for Markdown It
154159
*/
155160
markdownItUses?: (
156-
| MarkdownIt.PluginSimple
157-
| [MarkdownIt.PluginSimple | MarkdownIt.PluginWithOptions<any>, any]
161+
| MarkdownItPluginSimple
162+
| [MarkdownItPluginSimple | MarkdownItPluginWithOptions<any>, any]
158163
| any
159164
)[]
160165

161166
/**
162167
* A function providing the Markdown It instance gets the ability to apply custom
163168
* settings/plugins
164169
*/
165-
markdownItSetup?: (MarkdownIt: MarkdownIt) => void | Promise<void>
170+
markdownItSetup?: (MarkdownIt: MarkdownItAsync) => void | Promise<void>
166171

167172
/**
168173
* Class names for wrapper div

Diff for: ‎test/excerpt.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('excerpt', () => {
1313
},
1414
},
1515
})
16-
const markdownToVue = await createMarkdown(options)
16+
const markdownToVue = createMarkdown(options)
1717
const md = `---
1818
title: Hey
1919
---
@@ -27,7 +27,7 @@ This is an excerpt which has been rendered to **HTML**.
2727
- A
2828
- B
2929
- C`
30-
expect(markdownToVue('', md).code).toMatchSnapshot()
30+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
3131
})
3232

3333
it('raw excerpt', async () => {
@@ -41,7 +41,7 @@ This is an excerpt which has been rendered to **HTML**.
4141
},
4242
},
4343
})
44-
const markdownToVue = await createMarkdown(options)
44+
const markdownToVue = createMarkdown(options)
4545
const md = `---
4646
title: Hey
4747
---
@@ -55,6 +55,6 @@ This is an excerpt which is kept as **raw Markdown**.
5555
- A
5656
- B
5757
- C`
58-
expect(markdownToVue('', md).code).toMatchSnapshot()
58+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
5959
})
6060
})

Diff for: ‎test/frontmatterPreprocessor.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ const frontmatterPreprocess: ResolvedOptions['frontmatterPreprocess'] = (fm) =>
2727

2828
describe('provide bespoke frontmatter processor', () => {
2929
it('inline markdown is used over default properties', async () => {
30-
const parser = await createMarkdown(resolveOptions({ frontmatterPreprocess }))
31-
const md = parser('', await readFile('test/fixtures/simple.md', 'utf-8')).code
30+
const markdownToVue = createMarkdown(resolveOptions({ frontmatterPreprocess }))
31+
const md = (await markdownToVue('', await readFile('test/fixtures/simple.md', 'utf-8'))).code
32+
3233
// Positive tests
3334
expect(
3435
md.includes('Hello World'),

Diff for: ‎test/transform.test.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { resolveOptions } from '../src/core/options'
44

55
describe('transform', async () => {
66
const options = resolveOptions({})
7-
const markdownToVue = await createMarkdown(options)
7+
const markdownToVue = createMarkdown(options)
88

9-
it('basic', () => {
9+
it('basic', async () => {
1010
const md = `---
1111
title: Hey
1212
---
@@ -17,39 +17,39 @@ title: Hey
1717
- B
1818
- C
1919
`
20-
expect(markdownToVue('', md).code).toMatchSnapshot()
20+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
2121
})
2222

23-
it('style', () => {
23+
it('style', async () => {
2424
const md = `
2525
# Hello
2626
2727
<style>h1 { color: red }</style>
2828
`
29-
expect(markdownToVue('', md).code).toMatchSnapshot()
29+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
3030
})
3131

32-
it('script setup', () => {
32+
it('script setup', async () => {
3333
const md = `
3434
# Hello
3535
3636
<script setup lang="ts">
3737
import Foo from './Foo.vue'
3838
</script>
3939
`
40-
expect(markdownToVue('', md).code).toMatchSnapshot()
40+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
4141
})
4242

43-
it('exposes frontmatter', () => {
43+
it('exposes frontmatter', async () => {
4444
const md = `---
4545
title: Hey
4646
---
4747
4848
# Hello`
49-
expect(markdownToVue('', md).code).toMatchSnapshot()
49+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
5050
})
5151

52-
it('couldn\'t expose frontmatter', () => {
52+
it('couldn\'t expose frontmatter', async () => {
5353
const md = `---
5454
title: Hey
5555
---
@@ -58,21 +58,21 @@ title: Hey
5858
defineExpose({ test: 'test'})
5959
</script>
6060
`
61-
expect(markdownToVue('', md).code).toMatchSnapshot()
61+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
6262
})
6363

64-
it('escapeCodeTagInterpolation', () => {
64+
it('escapeCodeTagInterpolation', async () => {
6565
const md = `
6666
<div>{{hello}}</div>
6767
6868
\`\`\`ts
6969
<div>{{hello}}</div>
7070
\`\`\`
7171
`
72-
expect(markdownToVue('', md).code).toMatchSnapshot()
72+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
7373
})
7474

75-
it('frontmatter interpolation', () => {
75+
it('frontmatter interpolation', async () => {
7676
const md = `
7777
---
7878
name: 'My Cool App'
@@ -82,10 +82,10 @@ name: 'My Cool App'
8282
8383
This is {{frontmatter.name}}
8484
`
85-
expect(markdownToVue('', md).code).toMatchSnapshot()
85+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
8686
})
8787

88-
it('vue directives', () => {
88+
it('vue directives', async () => {
8989
const md = `
9090
---
9191
name: 'My Cool App'
@@ -99,10 +99,10 @@ function onClick() {
9999
100100
<button @click="onClick"></button>
101101
`
102-
expect(markdownToVue('', md).code).toMatchSnapshot()
102+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
103103
})
104104

105-
it('export keyword frontmatters', () => {
105+
it('export keyword frontmatters', async () => {
106106
const md = `
107107
---
108108
class: 'text'
@@ -111,17 +111,17 @@ default: 'foo'
111111
112112
Hello
113113
`
114-
expect(markdownToVue('', md).code).toMatchSnapshot()
114+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
115115
})
116116

117-
it('code escape', () => {
117+
it('code escape', async () => {
118118
const md = `
119119
Hello \`{{ world }}\`
120120
121121
\`\`\`js
122122
console.log(\`{{ world }}\`)
123123
\`\`\`
124124
`
125-
expect(markdownToVue('', md).code).toMatchSnapshot()
125+
expect((await markdownToVue('', md)).code).toMatchSnapshot()
126126
})
127127
})

0 commit comments

Comments
 (0)
Please sign in to comment.