Skip to content

Commit e59ed40

Browse files
committed
feat: add transforms.extraScripts hook
1 parent ef104fb commit e59ed40

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/core/markdown.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ export async function createMarkdown(options: ResolvedOptions) {
103103
} = options
104104

105105
raw = raw.trimStart()
106-
107-
if (transforms.before)
108-
raw = transforms.before(raw, id)
106+
raw = transforms.before?.(raw, id) ?? raw
109107

110108
const env: MarkdownEnv = { id }
111109
let html = markdown.render(raw, env)
@@ -136,8 +134,7 @@ export async function createMarkdown(options: ResolvedOptions) {
136134
html = `<${wrapperComponentName} ${attrs}>${html}</${wrapperComponentName}>`
137135
}
138136

139-
if (transforms.after)
140-
html = transforms.after(html, id)
137+
html = transforms.after?.(html, id) ?? html
141138

142139
if (options.escapeCodeTagInterpolation) {
143140
// escape curly brackets interpolation in <code>, #14
@@ -190,6 +187,8 @@ export async function createMarkdown(options: ResolvedOptions) {
190187
scriptLines.unshift(`import { useHead } from "${importFrom}"`)
191188
scriptLines.push('useHead(head)')
192189
}
190+
191+
scriptLines.push(...transforms.extraScripts?.(frontmatter, id) || [])
193192
}
194193

195194
if (options.excerpt) {

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ export interface Options {
187187
transforms?: {
188188
before?: (code: string, id: string) => string
189189
after?: (code: string, id: string) => string
190+
/**
191+
* Return extra code to be injected into the `<script>` tag
192+
*/
193+
extraScripts?: (frontmatter: Record<string, any>, id: string) => string[]
190194
}
191195

192196
include?: FilterPattern

0 commit comments

Comments
 (0)