Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix async highlight not async #1685

Merged
merged 1 commit into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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