From 5c73f614e8f579e04fe61c948b69be7bc6d81d5d Mon Sep 17 00:00:00 2001 From: hippotastic <6137925+hippotastic@users.noreply.github.com> Date: Sat, 11 Jun 2022 00:32:23 +0200 Subject: [PATCH] Fix remarkMdxish performance issue on huge sites (#3572) --- .changeset/proud-forks-hammer.md | 5 +++++ packages/markdown/remark/src/remark-mdxish.ts | 13 +++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 .changeset/proud-forks-hammer.md diff --git a/.changeset/proud-forks-hammer.md b/.changeset/proud-forks-hammer.md new file mode 100644 index 000000000..2706b9b1b --- /dev/null +++ b/.changeset/proud-forks-hammer.md @@ -0,0 +1,5 @@ +--- +'@astrojs/markdown-remark': patch +--- + +Fix remarkMdxish performance issue on huge sites diff --git a/packages/markdown/remark/src/remark-mdxish.ts b/packages/markdown/remark/src/remark-mdxish.ts index 50bf5f103..0f70f87b3 100644 --- a/packages/markdown/remark/src/remark-mdxish.ts +++ b/packages/markdown/remark/src/remark-mdxish.ts @@ -3,12 +3,17 @@ import type { Tag } from 'mdast-util-mdx-jsx'; import { mdxFromMarkdown, mdxToMarkdown } from './mdast-util-mdxish.js'; import { mdxjs } from './mdxjs.js'; -export default function remarkMdxish(this: any, options = {}) { +// Prepare markdown extensions once to prevent performance issues +const extMdxJs = mdxjs({}); +const extMdxFromMarkdown = makeFromMarkdownLessStrict(mdxFromMarkdown()); +const extMdxToMarkdown = mdxToMarkdown(); + +export default function remarkMdxish(this: any) { const data = this.data(); - add('micromarkExtensions', mdxjs(options)); - add('fromMarkdownExtensions', makeFromMarkdownLessStrict(mdxFromMarkdown())); - add('toMarkdownExtensions', mdxToMarkdown()); + add('micromarkExtensions', extMdxJs); + add('fromMarkdownExtensions', extMdxFromMarkdown); + add('toMarkdownExtensions', extMdxToMarkdown); function add(field: string, value: unknown) { const list = data[field] ? data[field] : (data[field] = []);