Skip to content

async transforms between parse and render call #62

Open
@s-light

Description

@s-light

Clear and concise description of the problem

i would like to embed code from files into fenced blocks:

for this i need some async action:
markdown-it/markdown-it#256 (comment)

i need to call my transform function on the parsed tokens array -
and then call the render function.

so would be super nice to be able to just insert async transformative calls between the two tasks..
something like:

const tokens = md.parse(your_source_md, env);
// run async function with only side-effects on tokens
await callAsyncFN(tokens, idx, options, env, sefl);
return md.renderer.render(tokens, md.options, env);

as fare as i can tell this would also mean an addition to the markdown-it-async wrapper.
(as there is only the full render call available at this..)

Suggested solution

add option similar to this
types.ts

// ...
/**
   * Custom tranformations apply before and after the markdown transformation
   */
  transforms?: {
    before?: (code: string, id: string) => string | Promise<string>
    renderingAfterParsingBeforeRender?: (tokens: array, idx: number, options: object, env: object, self:MarkdownIt) => None  | Promise<none>
    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[] | Promise<string[]>
  }
// ...

Alternative

No response

Additional context

i will try and create some sort of minimal example in the next days

for a complex test-setup you can have a look at
https://github.com/s-light/quasar-unplugin-vue-markdown-tests
https://codesandbox.io/p/github/s-light/quasar-unplugin-vue-markdown-tests/

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions