Skip to content

Commit

Permalink
feat: support async transformers (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx authored Jan 18, 2025
1 parent 13160d1 commit cbbf1a2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function createMarkdown(options: ResolvedOptions) {
} = options

raw = raw.trimStart()
raw = transforms.before?.(raw, id) ?? raw
raw = await transforms.before?.(raw, id) ?? raw

const env: MarkdownEnv = { id }
let html = await markdown.renderAsync(raw, env)
Expand Down Expand Up @@ -138,7 +138,7 @@ export function createMarkdown(options: ResolvedOptions) {
html = `<${wrapperComponentName} ${attrs}>${html}</${wrapperComponentName}>`
}

html = transforms.after?.(html, id) ?? html
html = await transforms.after?.(html, id) ?? html

if (options.escapeCodeTagInterpolation) {
// escape curly brackets interpolation in <code>, #14
Expand Down Expand Up @@ -194,7 +194,7 @@ export function createMarkdown(options: ResolvedOptions) {
scriptLines.push('useHead(head)')
}

scriptLines.push(...transforms.extraScripts?.(frontmatter, id) || [])
scriptLines.push(...await transforms.extraScripts?.(frontmatter, id) || [])
}

if (options.excerpt) {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ export interface Options {
* Custom tranformations apply before and after the markdown transformation
*/
transforms?: {
before?: (code: string, id: string) => string
after?: (code: string, id: string) => string
before?: (code: string, id: string) => string | Promise<string>
after?: (code: string, id: string) => string | Promise<string>
/**
* Return extra code to be injected into the `<script>` tag
*/
extraScripts?: (frontmatter: Record<string, any>, id: string) => string[]
extraScripts?: (frontmatter: Record<string, any>, id: string) => string[] | Promise<string[]>
}

include?: FilterPattern
Expand Down

0 comments on commit cbbf1a2

Please sign in to comment.