From 839a60622b6c2bffaa4e75b2e249c327bea74f9d Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 3 Feb 2023 22:54:44 -0600 Subject: [PATCH 1/4] fix: always return promise if async --- src/marked.js | 25 ++++++++++++++++--------- test/unit/marked-spec.js | 10 ++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/marked.js b/src/marked.js index 157c8ec2e9..ee06a55fd7 100644 --- a/src/marked.js +++ b/src/marked.js @@ -108,28 +108,35 @@ export function marked(src, opt, callback) { function onError(e) { e.message += '\nPlease report this to https://github.com/markedjs/marked.'; if (opt.silent) { - return '

An error occurred:

'
+      const msg = '

An error occurred:

'
         + escape(e.message + '', true)
         + '
'; + if (opt.async) { + return Promise.reject(msg); + } + return msg; } 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); } } diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index e9bfba1782..4dab6aeaac 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -1112,4 +1112,14 @@ br const html = await promise; expect(html.trim()).toBe('

text walked

'); }); + + 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('

text

'); + }); }); From 736440a446767c293050adf81518500b5375732e Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sat, 4 Feb 2023 00:29:52 -0600 Subject: [PATCH 2/4] docs: await walktokens when async --- docs/USING_PRO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 12a60e172a..9c42f61068 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -255,7 +255,7 @@ smartypants('"this ... string"')

Walk Tokens : walkTokens

-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. The return value of the function is ignored but awaited if running marked in [`async`](#async) mode. `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*. From 2915d5ca909c5ae0d5024afed396ee83056cc277 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sat, 4 Feb 2023 14:12:57 -0600 Subject: [PATCH 3/4] docs: walktokens async Co-authored-by: Steven --- docs/USING_PRO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 9c42f61068..a665122641 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -255,7 +255,7 @@ smartypants('"this ... string"')

Walk Tokens : walkTokens

-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 but awaited if running marked in [`async`](#async) mode. +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*. From 5078a75b4a00eeff203275f3306af5d3194a55a7 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sun, 5 Feb 2023 09:49:38 -0600 Subject: [PATCH 4/4] return rejected promise on error --- src/marked.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/marked.js b/src/marked.js index ee06a55fd7..7d5e23dd26 100644 --- a/src/marked.js +++ b/src/marked.js @@ -112,10 +112,13 @@ export function marked(src, opt, callback) { + escape(e.message + '', true) + '
'; if (opt.async) { - return Promise.reject(msg); + return Promise.resolve(msg); } return msg; } + if (opt.async) { + return Promise.reject(e); + } throw e; }