From 471c6f784e21399676c8b2002665ffdf83a1c59e Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Fri, 5 Aug 2022 14:43:50 -0500 Subject: [PATCH] [markdown] Harder, better, faster, stronger `vite-plugin-markdown` (#4137) * refactor: vite-plugin-md -> vite-plugin-md-legacy * wip: add vite-plugin-md * feat: always apply jsx renderer * fix: markHTMLString on VNode result * feat: apply new vite-plugin-markdown! * fix: add meta export to md * fix: remove needless $$metadata export * fix: toggle to legacy plugin on flag * fix: pass fileId to renderMarkdown * test: raw and compiled content on plain md * fix: escape vite env refs * refactor: astro-md -> legacy-astro-flavored-md, astro-md-mode -> astro-markdown * fix: import.meta.env refs with tests * fix: add pkg.json to clientAddress * fix: prefer JSX integration over Astro runtime * Revert "fix: prefer JSX integration over Astro runtime" This reverts commit 3e5fa49344be9c857393da9af095faab152e92e1. * fix: remove .mdx check on importSource * chore: changeset * chore: remove TODO * fix: add back getHeadings * fix: add pkg.json to astro-head fixture * fix: default to Astro renderer for MDX and MD * feat: add "headings" and "frontmatter" to md layouts * refactor: remove legacy flag conditionals from legacy plugin * fix: add back MDX warning when legacy is off * test: getHeadings() glob * fix: add error on "astro.headings" access * feat: update docs example astro.headings => headings * refactor: readFile as string w/ utf-8 * chore: remove astro metadata TODO * refactor: stringify HTML once * fix: add pkg.json to glob-pages-css --- .changeset/hip-dancers-move.md | 6 + .../components/PageContent/PageContent.astro | 7 +- .../RightSidebar/RightSidebar.astro | 4 +- packages/astro/package.json | 2 + packages/astro/src/core/config.ts | 8 +- packages/astro/src/core/create-vite.ts | 5 +- packages/astro/src/vite-plugin-jsx/index.ts | 32 +- .../src/vite-plugin-markdown-legacy/README.md | 3 + .../src/vite-plugin-markdown-legacy/index.ts | 260 ++++++++++++++++ .../astro/src/vite-plugin-markdown/README.md | 2 +- .../astro/src/vite-plugin-markdown/index.ts | 289 ++++-------------- .../astro/test/astro-markdown-md-mode.test.js | 51 ---- packages/astro/test/astro-markdown.test.js | 231 +++++--------- .../test/fixtures/astro-head/package.json | 8 + .../src/pages/code-in-md.md | 7 - .../fixtures/astro-markdown/astro.config.mjs | 6 +- .../test/fixtures/astro-markdown/package.json | 3 +- .../src/components/Counter.svelte | 0 .../astro-markdown/src/layouts/Base.astro | 28 ++ .../astro-markdown/src/pages/basic.md | 3 + .../astro-markdown/src/pages/code-in-md.md | 9 - .../src/pages/components.md | 0 .../src/pages/headings-glob.json.js | 9 + .../src/pages/raw-content.json.js | 2 +- .../src/pages/vite-env-vars-glob.json.js | 7 + .../astro-markdown/src/pages/vite-env-vars.md | 5 - .../astro-markdown/src/pages/with-layout.md | 8 + .../test/fixtures/client-address/package.json | 8 + .../test/fixtures/glob-pages-css/package.json | 8 + .../astro.config.mjs | 6 +- .../package.json | 3 +- .../src/components/Counter.jsx | 0 .../src/components/Example.jsx | 0 .../src/components/Hello.jsx | 0 .../src/components/SlotComponent.astro | 0 .../src/components/SvelteButton.svelte | 0 .../src/components/TextBlock.jsx | 0 .../src/components/index.js | 0 .../src/content/code-element.md | 0 .../src/imported-md/plain.md | 0 .../src/imported-md/with-components.md | 0 .../src/layouts/content.astro | 0 .../src/layouts/layout-props.astro | 0 .../src/pages/children.md | 0 .../src/pages/code-element.astro | 0 .../src/pages/code-in-md.md | 16 + .../src/pages/comment-with-js.md | 0 .../src/pages/comment.md | 0 .../src/pages/dash.md | 0 .../src/pages/empty-code.md | 0 .../pages/imported-md/with-components.astro | 0 .../src/pages/jsx-expressions.md | 0 .../src/pages/layout-props.md | 0 .../src/pages/namespace.md | 0 .../src/pages/raw-content.json.js | 10 + .../src/pages/script.md | 0 .../src/pages/slots.md | 0 .../src/pages/slug.md | 0 .../src/pages/vite-env-vars.md | 35 +++ .../src/scripts/test.js | 0 .../legacy-astro-flavored-markdown.test.js | 200 ++++++++++++ packages/markdown/remark/src/remark-escape.ts | 2 +- pnpm-lock.yaml | 38 ++- 63 files changed, 831 insertions(+), 490 deletions(-) create mode 100644 .changeset/hip-dancers-move.md create mode 100644 packages/astro/src/vite-plugin-markdown-legacy/README.md create mode 100644 packages/astro/src/vite-plugin-markdown-legacy/index.ts delete mode 100644 packages/astro/test/astro-markdown-md-mode.test.js create mode 100644 packages/astro/test/fixtures/astro-head/package.json delete mode 100644 packages/astro/test/fixtures/astro-markdown-md-mode/src/pages/code-in-md.md rename packages/astro/test/fixtures/{astro-markdown-md-mode => astro-markdown}/src/components/Counter.svelte (100%) create mode 100644 packages/astro/test/fixtures/astro-markdown/src/layouts/Base.astro create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/basic.md rename packages/astro/test/fixtures/{astro-markdown-md-mode => astro-markdown}/src/pages/components.md (100%) create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/headings-glob.json.js create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars-glob.json.js create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/with-layout.md create mode 100644 packages/astro/test/fixtures/client-address/package.json create mode 100644 packages/astro/test/fixtures/glob-pages-css/package.json rename packages/astro/test/fixtures/{astro-markdown-md-mode => legacy-astro-flavored-markdown}/astro.config.mjs (59%) rename packages/astro/test/fixtures/{astro-markdown-md-mode => legacy-astro-flavored-markdown}/package.json (65%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/Counter.jsx (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/Example.jsx (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/Hello.jsx (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/SlotComponent.astro (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/SvelteButton.svelte (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/TextBlock.jsx (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/components/index.js (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/content/code-element.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/imported-md/plain.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/imported-md/with-components.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/layouts/content.astro (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/layouts/layout-props.astro (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/children.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/code-element.astro (100%) create mode 100644 packages/astro/test/fixtures/legacy-astro-flavored-markdown/src/pages/code-in-md.md rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/comment-with-js.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/comment.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/dash.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/empty-code.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/imported-md/with-components.astro (100%) rename packages/astro/test/fixtures/{astro-markdown-md-mode => legacy-astro-flavored-markdown}/src/pages/jsx-expressions.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/layout-props.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/namespace.md (100%) create mode 100644 packages/astro/test/fixtures/legacy-astro-flavored-markdown/src/pages/raw-content.json.js rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/script.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/slots.md (100%) rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/pages/slug.md (100%) create mode 100644 packages/astro/test/fixtures/legacy-astro-flavored-markdown/src/pages/vite-env-vars.md rename packages/astro/test/fixtures/{astro-markdown => legacy-astro-flavored-markdown}/src/scripts/test.js (100%) create mode 100644 packages/astro/test/legacy-astro-flavored-markdown.test.js diff --git a/.changeset/hip-dancers-move.md b/.changeset/hip-dancers-move.md new file mode 100644 index 000000000..bcebddf5c --- /dev/null +++ b/.changeset/hip-dancers-move.md @@ -0,0 +1,6 @@ +--- +'astro': minor +'@astrojs/markdown-remark': patch +--- + +Speed up internal markdown builds with new vite-plugin markdown diff --git a/examples/docs/src/components/PageContent/PageContent.astro b/examples/docs/src/components/PageContent/PageContent.astro index e0d96b57c..9af4056c9 100644 --- a/examples/docs/src/components/PageContent/PageContent.astro +++ b/examples/docs/src/components/PageContent/PageContent.astro @@ -2,9 +2,8 @@ import MoreMenu from "../RightSidebar/MoreMenu.astro"; import TableOfContents from "../RightSidebar/TableOfContents"; -const { content, githubEditUrl } = Astro.props; +const { content, headings, githubEditUrl } = Astro.props; const title = content.title; -const headings = content.astro.headings; ---
@@ -29,9 +28,11 @@ const headings = content.astro.headings; display: flex; flex-direction: column; } - .content > section { + + .content>section { margin-bottom: 4rem; } + .block { display: block; } diff --git a/examples/docs/src/components/RightSidebar/RightSidebar.astro b/examples/docs/src/components/RightSidebar/RightSidebar.astro index c009d2202..71ad5e1c2 100644 --- a/examples/docs/src/components/RightSidebar/RightSidebar.astro +++ b/examples/docs/src/components/RightSidebar/RightSidebar.astro @@ -1,8 +1,7 @@ --- import TableOfContents from "./TableOfContents"; import MoreMenu from "./MoreMenu.astro"; -const { content, githubEditUrl } = Astro.props; -const headings = content.astro.headings; +const { content, headings, githubEditUrl } = Astro.props; ---