Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepare transform_insideRendering #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@mdit-vue/types": "^2.1.0",
"@types/markdown-it": "^14.1.2",
"markdown-it": "^14.1.0",
"markdown-it-async": "^2.0.0",
"markdown-it-async": "^2.1.0",
"unplugin": "^2.2.0",
"unplugin-utils": "^0.2.4"
},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ export function createMarkdown(options: ResolvedOptions) {
raw = await transforms.before?.(raw, id) ?? raw

const env: MarkdownEnv = { id }
let html = await md.renderAsync(raw, env)

let tokens = md.parse(raw, env)

tokens = await transforms.tokens?.(tokens, options.markdownItOptions, env, md) ?? tokens

let html = await md.rendererRenderAsync(tokens, md.options as any, env)

const { excerpt = '', frontmatter: data = null } = env

if (wrapperDiv) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UnpluginFactory } from 'unplugin'
import type { Options } from './types'
import { createFilter } from 'unplugin-utils'
import { createUnplugin } from 'unplugin'
import { createFilter } from 'unplugin-utils'
import { createMarkdown } from './core/markdown'
import { resolveOptions } from './core/options'

Expand Down
19 changes: 16 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
MarkdownItAsyncOptions,
PluginSimple as MarkdownItPluginSimple,
PluginWithOptions as MarkdownItPluginWithOptions,
Token,
} from 'markdown-it-async'
import type { FilterPattern } from 'unplugin-utils'
import type { preprocessHead } from './core/head'
Expand Down Expand Up @@ -196,12 +197,22 @@ export interface Options {
* Custom tranformations apply before and after the markdown transformation
*/
transforms?: {
before?: (code: string, id: string) => string | Promise<string>
after?: (code: string, id: string) => string | Promise<string>
/**
* Pre-transformations
*/
before?: (code: string, id: string) => Awaitable<string | void>
/**
* Transform internal MarkdownIt tokens
*/
tokens?: (tokens: Token[], options: MarkdownItAsyncOptions, env: any, self: MarkdownItAsync) => Awaitable<Token[] | void>
/**
* Post-transformations
*/
after?: (code: string, id: string) => Awaitable<string | void>
/**
* Return extra code to be injected into the `<script>` tag
*/
extraScripts?: (frontmatter: Record<string, any>, id: string) => string[] | Promise<string[]>
extraScripts?: (frontmatter: Record<string, any>, id: string) => Awaitable<string[] | void>
}

include?: FilterPattern
Expand All @@ -213,3 +224,5 @@ export interface ResolvedOptions extends Required<Options> { }
export interface MarkdownEnv extends MarkdownItEnv {
id: string
}

export type Awaitable<T> = T | Promise<T>
Loading