Skip to content

Commit d3893cb

Browse files
committed
feat: add warnOnSyncRender option
1 parent 1572b5c commit d3893cb

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ const md = MarkdownItAsync({
2626
const html = await md.renderAsync(markdown)
2727
```
2828

29+
## Opt-in Warning
30+
31+
If you integrate this package into your project, and want to make sure you have every usage of `md.render` migrated to `md.renderAsync`, you can enable the `warnOnSyncRender` option.
32+
33+
```ts
34+
const md = MarkdownItAsync({
35+
warnOnSyncRender: true
36+
})
37+
38+
md.render('Hello') // This will throw a conole warning
39+
```
40+
2941
## How it works?
3042

3143
This package is a thin wrapper around `markdown-it` to support async highlight function. It uses [the approach suggested in `markdown-it`'s docs](https://github.com/markdown-it/markdown-it/blob/master/docs/development.md#i-need-async-rule-how-to-do-it), by putting placeholders in sync mode and then replace them with async results.

src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export interface MarkdownItAsyncOptions extends Omit<Options, 'highlight'> {
2020
* @default null
2121
*/
2222
highlight?: ((str: string, lang: string, attrs: string) => string | Promise<string>) | null | undefined
23+
24+
/**
25+
* Emit warning when calling `md.render` instead of `md.renderAsync`.
26+
*
27+
* @default false
28+
*/
29+
warnOnSyncRender?: boolean
2330
}
2431

2532
export type { MarkdownItAsyncOptions as Options }
@@ -58,9 +65,16 @@ export class MarkdownItAsync extends MarkdownIt {
5865
return super.use(plugin, ...params)
5966
}
6067

68+
render(src: string, env?: any): string {
69+
if ((this.options as MarkdownItAsyncOptions).warnOnSyncRender) {
70+
console.warn('[markdown-it-async] Please use `md.renderAsync` instead of `md.render`')
71+
}
72+
return super.render(src, env)
73+
}
74+
6175
async renderAsync(src: string, env?: any): Promise<string> {
6276
this.options.highlight = wrapHightlight(this.options.highlight, this.placeholderMap)
63-
const result = this.render(src, env)
77+
const result = super.render(src, env)
6478
return replaceAsync(result, placeholderRe, async (match, id) => {
6579
if (!this.placeholderMap.has(id))
6680
throw new Error(`Unknown highlight placeholder id: ${id}`)

test/index.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import createMarkdownIt from 'markdown-it'
22
import { codeToHtml, createHighlighter } from 'shiki'
3-
import { describe, expect, it } from 'vitest'
3+
import { describe, expect, it, vi } from 'vitest'
44
import createMarkdownItAsync from '../src/'
55

66
const fixture = `
@@ -55,4 +55,21 @@ describe('markdown-it-async', async () => {
5555

5656
expect(expectedResult).toEqual(await mda.renderAsync(fixture))
5757
})
58+
59+
it('warn', () => {
60+
const mda = createMarkdownItAsync({
61+
warnOnSyncRender: true,
62+
})
63+
64+
const spy = vi
65+
.spyOn(console, 'warn')
66+
.mockImplementation(() => {})
67+
68+
mda.render(fixture)
69+
70+
expect.soft(spy)
71+
.toHaveBeenCalledWith('[markdown-it-async] Please use `md.renderAsync` instead of `md.render`')
72+
73+
spy.mockRestore()
74+
})
5875
})

0 commit comments

Comments
 (0)