diff --git a/src/marked.js b/src/marked.js index 83ffc00394..bb0007a402 100644 --- a/src/marked.js +++ b/src/marked.js @@ -76,20 +76,22 @@ function marked(src, opt, callback) { marked.walkTokens(tokens, function(token) { if (token.type === 'code') { pending++; - highlight(token.text, token.lang, function(err, code) { - if (err) { - return done(err); - } - if (code != null && code !== token.text) { - token.text = code; - token.escaped = true; - } - - pending--; - if (pending === 0) { - done(); - } - }); + setTimeout(() => { + highlight(token.text, token.lang, function(err, code) { + if (err) { + return done(err); + } + if (code != null && code !== token.text) { + token.text = code; + token.escaped = true; + } + + pending--; + if (pending === 0) { + done(); + } + }); + }, 0); } }); diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index f1253b49bf..53b0d90818 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -310,7 +310,7 @@ text 1 }); it('should call callback for each error in highlight', (done) => { - highlight.and.callFake((lang, text, callback) => { + highlight.and.callFake((text, lang, callback) => { callback(new Error('highlight error')); }); @@ -328,6 +328,29 @@ text 1 } }); }); + + it('should highlight codeblocks when not async', (done) => { + highlight.and.callFake((text, lang, callback) => { + callback(null, `async ${text || ''}`); + }); + + marked(markdown, { highlight }, (err, html) => { + if (err) { + fail(err); + } + + expect(html).toBe(`
async text 1
+
+
async text 2
+
+ +`); + done(); + }); + }); }); describe('walkTokens', () => {