Skip to content

allow custom async *magic* between parse & render #3

@s-light

Description

@s-light

Clear and concise description of the problem

kind of allow async plugins

this is supported if you do the parsing and rendering yourself.
more information can be found in this topic:
markdown-it/markdown-it#256 (comment)

Suggested solution

this would be easily possible if we split the parse and render functions / make the renderer.render someway available:

something in the like of

  async renderAsync(src: string, env?: any): Promise<string> {
    return this.rendererRenderAsync(
      this.parse(src, env),
      this.options,
      env,
    )
  }

  async rendererRenderAsync(tokens: Array, options: any, env: any): Promise<string> {
    this.options.highlight = wrapHightlight(this.options.highlight, this.placeholderMap)
    this.disableWarn = true
    const result = this.renderer.render(tokens, this.options, env)

    this.disableWarn = false
    return replaceAsync(result, placeholderRe, async (match, id) => {
      if (!this.placeholderMap.has(id))
        throw new Error(`Unknown highlight placeholder id: ${id}`)
      const [promise, _str, lang, _attrs] = this.placeholderMap.get(id)!
      const result = await promise || ''
      this.placeholderMap.delete(id)
      if (result.startsWith('<pre'))
        return result
      else
        return `<pre><code class="language-${lang}">${result}</code></pre>`
    })
  }

Alternative

No response

Additional context

this is needed for unplugin/unplugin-vue-markdown#62

i will create a pr for this..

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions