Skip to content

Commit fda7fa5

Browse files
authored
Add memo for processor to 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. Closes GH-909. Reviewed-by: JounQin <admin@1stg.me> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 8545ebd commit fda7fa5

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/index.js

Lines changed: 8 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,12 @@ export async function MarkdownAsync(options) {
212212
* React node.
213213
*/
214214
export function MarkdownHooks(options) {
215-
const processor = createProcessor(options)
215+
const processor = useMemo(
216+
function () {
217+
return createProcessor(options)
218+
},
219+
[options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]
220+
)
216221
const [error, setError] = useState(
217222
/** @type {Error | undefined} */ (undefined)
218223
)
@@ -238,12 +243,7 @@ export function MarkdownHooks(options) {
238243
cancelled = true
239244
}
240245
},
241-
[
242-
options.children,
243-
options.rehypePlugins,
244-
options.remarkPlugins,
245-
options.remarkRehypeOptions
246-
]
246+
[options.children, processor]
247247
)
248248

249249
if (error) throw error

0 commit comments

Comments
 (0)