Skip to content

Commit

Permalink
feat: allow markdownItSetup to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 14, 2023
1 parent 2264b1b commit 16ffaf8
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function VitePluginMarkdown(userOptions: Options = {}): Plugin {
return {
name: 'vite-plugin-vue-markdown',
enforce: 'pre',
transform(raw, id) {
async transform(raw, id) {
if (!filter(id))
return
try {
return markdownToVue(id, raw)
return (await markdownToVue)(id, raw)
}
catch (e: any) {
this.error(e)
Expand All @@ -32,7 +32,7 @@ function VitePluginMarkdown(userOptions: Options = {}): Plugin {

const defaultRead = ctx.read
ctx.read = async function () {
return markdownToVue(ctx.file, await defaultRead()).code
return (await markdownToVue)(ctx.file, await defaultRead()).code
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function extractCustomBlock(html: string, options: ResolvedOptions) {
return { html, blocks }
}

export function createMarkdown(options: ResolvedOptions) {
export async function createMarkdown(options: ResolvedOptions) {
const isVue2 = options.vueVersion.startsWith('2.')

const markdown = new MarkdownIt({
Expand Down Expand Up @@ -91,7 +91,7 @@ export function createMarkdown(options: ResolvedOptions) {
markdown.use(plugin, options)
})

options.markdownItSetup(markdown)
await options.markdownItSetup(markdown)

return (id: string, raw: string): TransformResult => {
const {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export interface Options {
* A function providing the Markdown It instance gets the ability to apply custom
* settings/plugins
*/
markdownItSetup?: (MarkdownIt: MarkdownIt) => void
markdownItSetup?: (MarkdownIt: MarkdownIt) => void | Promise<void>

/**
* Class names for wrapper div
Expand Down
8 changes: 4 additions & 4 deletions test/excerpt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createMarkdown } from '../src/markdown'
import { resolveOptions } from '../src/options'

describe('excerpt', () => {
it('rendered excerpt', () => {
it('rendered excerpt', async () => {
const options = resolveOptions({
excerpt: true,
frontmatterOptions: {
Expand All @@ -13,7 +13,7 @@ describe('excerpt', () => {
},
},
})
const markdownToVue = createMarkdown(options)
const markdownToVue = await createMarkdown(options)
const md = `---
title: Hey
---
Expand All @@ -30,7 +30,7 @@ This is an excerpt which has been rendered to **HTML**.
expect(markdownToVue('', md).code).toMatchSnapshot()
})

it('raw excerpt', () => {
it('raw excerpt', async () => {
const options = resolveOptions({
excerpt: true,
frontmatterOptions: {
Expand All @@ -41,7 +41,7 @@ This is an excerpt which has been rendered to **HTML**.
},
},
})
const markdownToVue = createMarkdown(options)
const markdownToVue = await createMarkdown(options)
const md = `---
title: Hey
---
Expand Down
2 changes: 1 addition & 1 deletion test/frontmatterPreprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const frontmatterPreprocess: ResolvedOptions['frontmatterPreprocess'] = (fm) =>

describe('provide bespoke frontmatter processor', () => {
it('inline markdown is used over default properties', async () => {
const parser = createMarkdown(resolveOptions({ frontmatterPreprocess }))
const parser = await createMarkdown(resolveOptions({ frontmatterPreprocess }))
const md = parser('', await readFile('test/fixtures/simple.md', 'utf-8')).code
// Positive tests
expect(
Expand Down
4 changes: 2 additions & 2 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { describe, expect, it } from 'vitest'
import { createMarkdown } from '../src/markdown'
import { resolveOptions } from '../src/options'

describe('transform', () => {
describe('transform', async () => {
const options = resolveOptions({})
const markdownToVue = createMarkdown(options)
const markdownToVue = await createMarkdown(options)

it('basic', () => {
const md = `---
Expand Down

0 comments on commit 16ffaf8

Please sign in to comment.