Skip to content

Commit 2c39ce2

Browse files
committed
feat: added transforms:renderingInside'
fixes: unplugin#62
1 parent c982f60 commit 2c39ce2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/core/markdown.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ export function createMarkdown(options: ResolvedOptions) {
114114
raw = await transforms.before?.(raw, id) ?? raw
115115

116116
const env: MarkdownEnv = { id }
117-
let html = await md.renderAsync(raw, env)
117+
// let html = await md.renderAsync(raw, env)
118+
const tokens = md.parse(raw, env)
119+
if (transforms.renderingInside) {
120+
// only side-effects on tokens array.
121+
await transforms.renderingInside(tokens, options.markdownItOptions, env, md)
122+
}
123+
let html = await md.rendererRenderAsync(tokens)
124+
118125
const { excerpt = '', frontmatter: data = null } = env
119126

120127
if (wrapperDiv) {

src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
MarkdownItAsyncOptions,
77
PluginSimple as MarkdownItPluginSimple,
88
PluginWithOptions as MarkdownItPluginWithOptions,
9+
Token,
910
} from 'markdown-it-async'
1011
import type { FilterPattern } from 'unplugin-utils'
1112
import type { preprocessHead } from './core/head'
@@ -197,6 +198,10 @@ export interface Options {
197198
*/
198199
transforms?: {
199200
before?: (code: string, id: string) => string | Promise<string>
201+
/**
202+
* run (async) manipulations on tokens array (side-effects only)
203+
*/
204+
renderingInside?: (tokens: Token[], options: MarkdownItAsyncOptions, env: any, self: MarkdownItAsync) => null | Promise<null>
200205
after?: (code: string, id: string) => string | Promise<string>
201206
/**
202207
* Return extra code to be injected into the `<script>` tag

0 commit comments

Comments
 (0)