Skip to content

async "highlight" function support? #47

TooTallNate opened this Issue Apr 22, 2012 · 5 comments

4 participants


It would be nice to be able to have the "highlight" callback function return asynchronously, as it's the only way to integrate with something like pygments. I realize that this means that marked itself would have to have a new API that returns asynchronously, but that's probably ok. Thoughts?

chjj commented Apr 24, 2012

I don't know if I'd want to implement this. Any other highlighter you can use from inside node is going to be synchronous. It would really be a lot of changes for one specific case.

You could probably hack it into the exposed function easily enough: (warning, untested)

var marked = function(src, func) {
  return func
    ? marked.async(src, func)
    : parse(block.lexer(src));

marked.async = function(src, func) {
  var src = block.lexer(src)
    , highlight = options.highlight
    , pending = 0
    , l = src.length
    , i = 0;

  if (!highlight || highlight.length < 3) {
    return func(null, parse(src));

  for (; i < l; i++) {
    (function(tok) {
      if (tok.type !== 'code') return;
      highlight(tok.text, tok.lang, function(err, code) {
        if (code == null || code === tok.text) {
          return --pending || done();
        tok.text = code;
        tok.escaped = true;
        --pending || done();

  if (!pending) return done();

  function done() {
    delete options.highlight;
    func(null, parse(src));
    options.highlight = highlight;

// usage
  gfm: true,
  highlight: pygments

marked(text, function(err, text) {

I had the same issue, and your code above appears to work.

I went ahead and got them working together, in case anyone else wants to see, the code is here and test sample usage here

fortes commented Jan 7, 2013

@chjj Would you accept a pull request if I added implement the async API?

chjj commented Jan 8, 2013

@fortes, it depends on how you implement it. I might add it in the way mentioned above, but an entire refactor of the parser is not appealing to me, and I worry about any overhead that the async refactor might add.

fortes commented Jan 8, 2013

@chjj I'll take a crack at it when I get some spare cycles, and you can see what you think. I'm not interested in maintaining a fork, so if it's not something that will go into master, I'll come up with another solution for fortes/enfield.

Great work on marked, btw. Easily the best of the node.js markdown parsers.

@chjj chjj closed this in db4b6d4 Mar 18, 2013
@Fogetti Fogetti pushed a commit to Fogetti/marked that referenced this issue May 13, 2016
@chjj async highlighter support. closes #47. 3fb90e1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.