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

Support highlighting codespan using highlight option + or support async function in Renderer #1780

Closed
aminya opened this issue Oct 17, 2020 · 5 comments

Comments

@aminya
Copy link

aminya commented Oct 17, 2020

Describe the feature

  1. Currently, marked only allows specifying a highlight function for code blocks and not inline code (codespan).

  2. Using the custom Renderer is not possible because (new Renderer()).codespan does not accept async functions. Thus, there is no way to use an async highlighter for highlighting codespan unless an exoteric solution like deasync is used.

Why is this feature necessary?
To highlight inline code.

Describe alternatives you've considered
Using a Pandoc renderer. However, lang can be provided externally through other parts of the code, and so this is independent of supporting Pandoc syntax for giving the language through the markdown syntax.

@UziTech
Copy link
Member

UziTech commented Oct 17, 2020

duplicate of #458 PRs are welcome 😁

@UziTech UziTech closed this as completed Oct 17, 2020
@aminya
Copy link
Author

aminya commented Oct 17, 2020

Well, this had two sections. The first one can be simpler I think since the highlight function already exists for code.

@UziTech
Copy link
Member

UziTech commented Oct 17, 2020

The issue with highlighting codespans is that you can't tell marked what language it is because there can't be an infostring like a code block.

You could always do the highlighting after running the markdown through marked.

@aminya
Copy link
Author

aminya commented Oct 17, 2020

I can by using a lambda. For example, here I don't care about "lang" at all. I just supply my own "scopeName" (language)

https://github.com/atom-ide-community/atom-ide-markdown-service/blob/d02dce38781de4d1a71a0e3c84c0c146aff5f49c/src/renderer.ts#L72

@UziTech
Copy link
Member

UziTech commented Oct 17, 2020

You could use a walkTokens function:

const walkTokens = (token) => {
  if (token.type === 'codespan') {
    token.text = highlight(token.text);
  }
};

marked.use({ walkTokens });

For an async walk tokens function you will have to await all of the promises.

const tokens = marked.lexer(md, opts);
const promises = [];
marked.walkTokens(tokens, (token) => {
  if (token.type === 'codespan') {
    promises.push(asyncFunction(token));
  }
});
await Promise.all(promises);
const html = marked.parser(tokens, opts);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants