Skip to content

Commit

Permalink
fix async highlight sync
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed May 21, 2020
1 parent 19ebe51 commit 0233262
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 16 additions & 14 deletions src/marked.js
Expand Up @@ -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);
}
});

Expand Down
25 changes: 24 additions & 1 deletion test/unit/marked-spec.js
Expand Up @@ -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'));
});

Expand All @@ -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(`<pre><code class="language-lang1">async text 1</code></pre>
<blockquote>
<pre><code class="language-lang2">async text 2</code></pre>
</blockquote>
<ul>
<li><pre><code class="language-lang3">async text 3</code></pre>
</li>
</ul>
`);
done();
});
});
});

describe('walkTokens', () => {
Expand Down

0 comments on commit 0233262

Please sign in to comment.