Skip to content

Commit 02c1b8a

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 02c1b8a

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"react": "^19.0.0",
7676
"react-dom": "^19.0.0",
7777
"rehype-raw": "^7.0.0",
78-
"rehype-starry-night": "^2.0.0",
7978
"remark-cli": "^12.0.0",
8079
"remark-gfm": "^4.0.0",
8180
"remark-preset-wooorm": "^11.0.0",

0 commit comments

Comments
 (0)