From 089a5e7b7b41b83d3196ec096ea2fe5d3f1d1019 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Fri, 18 Apr 2025 19:51:16 +0200 Subject: [PATCH 1/2] Memoize the processor in MarkdownHooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6e3fdd2..7600018 100644 --- a/lib/index.js +++ b/lib/index.js @@ -107,7 +107,7 @@ import {unreachable} from 'devlop' import {toJsxRuntime} from 'hast-util-to-jsx-runtime' import {urlAttributes} from 'html-url-attributes' import {Fragment, jsx, jsxs} from 'react/jsx-runtime' -import {useEffect, useState} from 'react' +import {useEffect, useMemo, useState} from 'react' import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' import {unified} from 'unified' @@ -212,7 +212,10 @@ export async function MarkdownAsync(options) { * React node. */ export function MarkdownHooks(options) { - const processor = createProcessor(options) + const processor = useMemo( + () => createProcessor(options), + [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions] + ) const [error, setError] = useState( /** @type {Error | undefined} */ (undefined) ) @@ -238,12 +241,7 @@ export function MarkdownHooks(options) { cancelled = true } }, - [ - options.children, - options.rehypePlugins, - options.remarkPlugins, - options.remarkRehypeOptions - ] + [options.children, processor] ) if (error) throw error From 087408fd819fda187eff42e5257993f69811a5bb Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 19 Apr 2025 17:33:53 +0200 Subject: [PATCH 2/2] Replace arrow function with a function expression --- lib/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 7600018..06f07e1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -213,7 +213,9 @@ export async function MarkdownAsync(options) { */ export function MarkdownHooks(options) { const processor = useMemo( - () => createProcessor(options), + function () { + return createProcessor(options) + }, [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions] ) const [error, setError] = useState(