Skip to content

Commit

Permalink
fix: always return promise if async (#2728)
Browse files Browse the repository at this point in the history
* fix: always return promise if async

* docs: await walktokens when async

* docs: walktokens async

Co-authored-by: Steven <steven@ceriously.com>

* return rejected promise on error

---------

Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
UziTech and styfle authored Mar 22, 2023
1 parent 3acbb7f commit 042dcc5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/USING_PRO.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ smartypants('"this ... string"')

<h2 id="walk-tokens">Walk Tokens : <code>walkTokens</code></h2>

The walkTokens function gets called with every token. Child tokens are called before moving on to sibling tokens. Each token is passed by reference so updates are persisted when passed to the parser. The return value of the function is ignored.
The walkTokens function gets called with every token. Child tokens are called before moving on to sibling tokens. Each token is passed by reference so updates are persisted when passed to the parser. When [`async`](#async) mode is enabled, the return value is awaited. Otherwise the return value is ignored.

`marked.use()` can be called multiple times with different `walkTokens` functions. Each function will be called in order, starting with the function that was assigned *last*.

Expand Down
28 changes: 19 additions & 9 deletions src/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,38 @@ export function marked(src, opt, callback) {
function onError(e) {
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
if (opt.silent) {
return '<p>An error occurred:</p><pre>'
const msg = '<p>An error occurred:</p><pre>'
+ escape(e.message + '', true)
+ '</pre>';
if (opt.async) {
return Promise.resolve(msg);
}
return msg;
}
if (opt.async) {
return Promise.reject(e);
}
throw e;
}

try {
if (opt.async) {
let promise = Promise.resolve(Lexer.lex(src, opt));
if (opt.walkTokens) {
promise = promise.then((tokens) =>
Promise.all(marked.walkTokens(tokens, opt.walkTokens)).then(() => tokens)
);
}
return promise.then((tokens) => Parser.parse(tokens, opt)).catch(onError);
}

const tokens = Lexer.lex(src, opt);
if (opt.walkTokens) {
if (opt.async) {
return Promise.all(marked.walkTokens(tokens, opt.walkTokens))
.then(() => {
return Parser.parse(tokens, opt);
})
.catch(onError);
}
marked.walkTokens(tokens, opt.walkTokens);
}
return Parser.parse(tokens, opt);
} catch (e) {
onError(e);
return onError(e);
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/unit/marked-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,14 @@ br
const html = await promise;
expect(html.trim()).toBe('<p><em>text walked</em></p>');
});

it('should return promise if async', async() => {
marked.use({
async: true
});
const promise = marked('*text*');
expect(promise).toBeInstanceOf(Promise);
const html = await promise;
expect(html.trim()).toBe('<p><em>text</em></p>');
});
});

1 comment on commit 042dcc5

@vercel
Copy link

@vercel vercel bot commented on 042dcc5 Mar 22, 2023

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.