Skip to content

Commit

Permalink
fix: throw error when async function is not marked async (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Aug 8, 2023
1 parent b04280d commit 6d6319f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,16 @@ no need to escape chars
</code></pre>"
`);
});

test('async highlight without async option', async() => {
marked.use(markedHighlight({
highlight(code, lang) {
return new Promise((resolve, reject) => {
resolve(code);
});
}
}));

expect(() => marked(markdown)).toThrow(/set the async option to true/i);
});
});
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function markedHighlight(options) {
}

const code = options.highlight(token.text, lang);
if (code instanceof Promise) {
throw new Error('markedHighlight is not set to async but the highlight function is async. Set the async option to true on markedHighlight to await the async highlight function.');
}
updateToken(token)(code);
},
renderer: {
Expand Down
3 changes: 3 additions & 0 deletions types_test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {expectError} from 'tsd';
// Single function argument
markedHighlight((code: string, language: string) => code+language);

// Invalid asynchronous function argument - missing async: true
expectError(markedHighlight(async (code: string, language: string) => code+language));

// Minimal synchronous options argument
markedHighlight({
highlight(code: string, language: string) {
Expand Down

0 comments on commit 6d6319f

Please sign in to comment.