From b6ee877d58efd31bf408a4f8f360554f3406bc20 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sat, 19 Aug 2023 16:58:32 -0600 Subject: [PATCH] fix: ignore async: false when async: true is set by an extension (#2920) --- src/Instance.ts | 10 ++++++++++ test/unit/marked-spec.js | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Instance.ts b/src/Instance.ts index 4db2b4ef00..111d18c84f 100644 --- a/src/Instance.ts +++ b/src/Instance.ts @@ -244,6 +244,16 @@ export class Marked { const origOpt = { ...optOrCallback }; const opt = { ...this.defaults, ...origOpt }; + + // Show warning if an extension set async to true but the parse was called with async: false + if (this.defaults.async === true && origOpt.async === false) { + if (!opt.silent) { + console.warn('marked(): The async option was set to true by an extension. The async: false option sent to parse will be ignored.'); + } + + opt.async = true; + } + const throwError = this.#onError(!!opt.silent, !!opt.async, callback); // throw error in case of non string input diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index 6f55d4a6a4..47ba609621 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -577,6 +577,20 @@ used extension2 walked

expect(defaults.async).toBeFalse(); }); + it('should return Promise if async', () => { + expect(marked('test', { async: true })).toBeInstanceOf(Promise); + }); + + it('should return string if not async', () => { + expect(typeof marked('test', { async: false })).toBe('string'); + }); + + it('should return Promise if async is set by extension', () => { + use({ async: true }); + + expect(marked('test', { async: false })).toBeInstanceOf(Promise); + }); + it('should allow deleting/editing tokens', () => { const styleTags = { extensions: [{