Skip to content

Commit

Permalink
Merge pull request #1685 from UziTech/fix-highlight-asyncs
Browse files Browse the repository at this point in the history
fix async highlight not async
  • Loading branch information
styfle committed May 22, 2020
2 parents 19ebe51 + 0233262 commit ced3ff1
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

1 comment on commit ced3ff1

@vercel
Copy link

@vercel vercel bot commented on ced3ff1 May 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.