Coder Social home page Coder Social logo

Comments (5)

chjj avatar chjj commented on May 16, 2024

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;
      pending++;
      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();
      });
    })(src[i]);
  }

  if (!pending) return done();

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

// usage
marked.setOptions({
  gfm: true,
  highlight: pygments
});

marked(text, function(err, text) {
  console.log(text);
});

from marked.

adamchester avatar adamchester commented on May 16, 2024

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

from marked.

fortes avatar fortes commented on May 16, 2024

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

from marked.

chjj avatar chjj commented on May 16, 2024

@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.

from marked.

fortes avatar fortes commented on May 16, 2024

@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.

from marked.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.