Skip to content

Commit 089a5e7

Browse files
committed
Memoize the processor in MarkdownHooks
Some plugins may perform heavy work in preparation to make the transformer light-weight. A good example of this is `rehype-starry-night`. Without this change, an interactive markdown editor which includes `rehype-starry-night` is very sluggish. With this change, performance is as good as if `rehype-starry-night` isn’t even used.
1 parent 8545ebd commit 089a5e7

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ import {unreachable} from 'devlop'
107107
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
108108
import {urlAttributes} from 'html-url-attributes'
109109
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
110-
import {useEffect, useState} from 'react'
110+
import {useEffect, useMemo, useState} from 'react'
111111
import remarkParse from 'remark-parse'
112112
import remarkRehype from 'remark-rehype'
113113
import {unified} from 'unified'
@@ -212,7 +212,10 @@ export async function MarkdownAsync(options) {
212212
* React node.
213213
*/
214214
export function MarkdownHooks(options) {
215-
const processor = createProcessor(options)
215+
const processor = useMemo(
216+
() => createProcessor(options),
217+
[options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]
218+
)
216219
const [error, setError] = useState(
217220
/** @type {Error | undefined} */ (undefined)
218221
)
@@ -238,12 +241,7 @@ export function MarkdownHooks(options) {
238241
cancelled = true
239242
}
240243
},
241-
[
242-
options.children,
243-
options.rehypePlugins,
244-
options.remarkPlugins,
245-
options.remarkRehypeOptions
246-
]
244+
[options.children, processor]
247245
)
248246

249247
if (error) throw error

0 commit comments

Comments
 (0)