Skip to content

Since the render is async, can it also support async rules? #5

@ronny1020

Description

@ronny1020

Clear and concise description of the problem

Thanks for this excellent tool! I'm currently using it with VitePress 2.0.0-alpha and trying to create my own markdown-it plugin that requires an async render rule. However, it seems that md.renderer.rules does not support async functions.

Would it be possible to allow md.renderer.rules to be async to support this use case?

Thanks in advance!

Suggested solution

We can extend the Renderer class and create the async render method in this repo.

AsyncRenderer.prototype.render = async function (tokens, options, env) {
  let result = '';
  const rules = this.rules;

  const codes = await Promise.all(
    tokens.map(async (token, i) => {
      const type = token.type;

      if (type === 'inline') {
        return this.renderInline(token.children, options, env);
      } else if (typeof rules[type] !== 'undefined') {
        return rules[type](tokens, i, options, env, this);
      } else {
        return this.renderToken(tokens, i, options, env);
      }
    }),
  );

  result = codes.join('');

  return result;
}

Alternative

No response

Additional context

the sample code is adjusted from this
https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.mjs

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