Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[markdown] Harder, better, faster, stronger vite-plugin-markdown #4137

Merged
merged 32 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
fcd57c4
refactor: vite-plugin-md -> vite-plugin-md-legacy
bholmesdev Aug 2, 2022
06b48fb
wip: add vite-plugin-md
bholmesdev Aug 2, 2022
884f86b
feat: always apply jsx renderer
bholmesdev Aug 2, 2022
efa8918
fix: markHTMLString on VNode result
bholmesdev Aug 2, 2022
a9f2e04
feat: apply new vite-plugin-markdown!
bholmesdev Aug 2, 2022
3c1dd73
fix: add meta export to md
bholmesdev Aug 2, 2022
fac6712
fix: remove needless $$metadata export
bholmesdev Aug 2, 2022
b1b0a27
fix: toggle to legacy plugin on flag
bholmesdev Aug 2, 2022
0357fd9
fix: pass fileId to renderMarkdown
bholmesdev Aug 2, 2022
6f3a8c2
test: raw and compiled content on plain md
bholmesdev Aug 2, 2022
c25c492
fix: escape vite env refs
bholmesdev Aug 3, 2022
1eb27db
refactor: astro-md -> legacy-astro-flavored-md, astro-md-mode -> astr…
bholmesdev Aug 3, 2022
a540695
fix: import.meta.env refs with tests
bholmesdev Aug 3, 2022
42f8d76
fix: add pkg.json to clientAddress
bholmesdev Aug 3, 2022
9233347
fix: prefer JSX integration over Astro runtime
bholmesdev Aug 3, 2022
1eae86d
Revert "fix: prefer JSX integration over Astro runtime"
bholmesdev Aug 3, 2022
ee49ec5
fix: remove .mdx check on importSource
bholmesdev Aug 3, 2022
75f49da
chore: changeset
bholmesdev Aug 3, 2022
5c1c284
chore: remove TODO
bholmesdev Aug 3, 2022
90dbca5
fix: add back getHeadings
bholmesdev Aug 3, 2022
b7acaa4
fix: add pkg.json to astro-head fixture
bholmesdev Aug 3, 2022
680c220
fix: default to Astro renderer for MDX and MD
bholmesdev Aug 3, 2022
6fb1c59
feat: add "headings" and "frontmatter" to md layouts
bholmesdev Aug 3, 2022
12602d7
refactor: remove legacy flag conditionals from legacy plugin
bholmesdev Aug 3, 2022
a223d39
fix: add back MDX warning when legacy is off
bholmesdev Aug 3, 2022
4849516
test: getHeadings() glob
bholmesdev Aug 3, 2022
9295bae
fix: add error on "astro.headings" access
bholmesdev Aug 3, 2022
37b805b
feat: update docs example astro.headings => headings
bholmesdev Aug 3, 2022
6cb3294
refactor: readFile as string w/ utf-8
bholmesdev Aug 5, 2022
f919a37
chore: remove astro metadata TODO
bholmesdev Aug 5, 2022
353844b
refactor: stringify HTML once
bholmesdev Aug 5, 2022
95e4d8c
fix: add pkg.json to glob-pages-css
bholmesdev Aug 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/hip-dancers-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': minor
'@astrojs/markdown-remark': patch
---

Speed up internal markdown builds with new vite-plugin markdown
7 changes: 4 additions & 3 deletions examples/docs/src/components/PageContent/PageContent.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content.astro.headings now throws! You'll get a nice error recommending Astro.props.headings instead 👍

---

<article id="article" class="content">
Expand All @@ -29,9 +28,11 @@ const headings = content.astro.headings;
display: flex;
flex-direction: column;
}
.content > section {

.content>section {
margin-bottom: 4rem;
}

.block {
display: block;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/docs/src/components/RightSidebar/RightSidebar.astro
Original file line number Diff line number Diff line change
@@ -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;
---

<nav class="sidebar-nav" aria-labelledby="grid-right">
Expand All @@ -18,6 +17,7 @@ const headings = content.astro.headings;
position: sticky;
top: 0;
}

.sidebar-nav-inner {
height: 100%;
padding: 0;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"./vite-plugin-astro-postprocess/*": "./dist/vite-plugin-astro-postprocess/*",
"./vite-plugin-jsx/*": "./dist/vite-plugin-jsx/*",
"./vite-plugin-jsx": "./dist/vite-plugin-jsx/index.js",
"./vite-plugin-markdown-legacy": "./dist/vite-plugin-markdown-legacy/index.js",
"./vite-plugin-markdown-legacy/*": "./dist/vite-plugin-markdown-legacy/*",
"./vite-plugin-markdown": "./dist/vite-plugin-markdown/index.js",
"./vite-plugin-markdown/*": "./dist/vite-plugin-markdown/*",
"./dist/jsx/*": "./dist/jsx/*"
Expand Down
8 changes: 2 additions & 6 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { z } from 'zod';
import { LogOptions } from './logger/core.js';
import { appendForwardSlash, prependForwardSlash, trimSlashes } from './path.js';
import { arraify, isObject } from './util.js';
import jsxRenderer from '../jsx/renderer.js';

load.use([loadTypeScript]);

Expand Down Expand Up @@ -343,16 +344,11 @@ export async function validateConfig(
_ctx: {
pageExtensions: ['.astro', '.md', '.html'],
scripts: [],
renderers: [],
renderers: [jsxRenderer],
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
injectedRoutes: [],
adapter: undefined,
},
};
if (result.integrations.find((integration) => integration.name === '@astrojs/mdx')) {
// Enable default JSX integration. It needs to come first, so unshift rather than push!
const { default: jsxRenderer } = await import('../jsx/renderer.js');
(result._ctx.renderers as any[]).unshift(jsxRenderer);
}

// If successful, return the result as a verified AstroConfig object.
return result;
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import envVitePlugin from '../vite-plugin-env/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js';
import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js';
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import legacyMarkdownVitePlugin from '../vite-plugin-markdown-legacy/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js';
Expand Down Expand Up @@ -76,7 +77,9 @@ export async function createVite(
// the build to run very slow as the filewatcher is triggered often.
mode === 'dev' && astroViteServerPlugin({ config: astroConfig, logging }),
envVitePlugin({ config: astroConfig }),
markdownVitePlugin({ config: astroConfig, logging }),
astroConfig.legacy.astroFlavoredMarkdown
? legacyMarkdownVitePlugin({ config: astroConfig, logging })
: markdownVitePlugin({ config: astroConfig, logging }),
htmlVitePlugin(),
jsxVitePlugin({ config: astroConfig, logging }),
astroPostprocessVitePlugin({ config: astroConfig }),
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function renderJSX(result: SSRResult, vnode: any): Promise<any> {
props[key] = value;
}
}
return await renderToString(result, vnode.type as any, props, slots);
return markHTMLString(await renderToString(result, vnode.type as any, props, slots));
}
case !vnode.type && (vnode.type as any) !== 0:
return '';
Expand Down
32 changes: 25 additions & 7 deletions packages/astro/src/vite-plugin-jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,31 @@ export default function jsx({ config, logging }: AstroPluginJSXOptions): Plugin
JSX_RENDERER_CACHE.set(config, jsxRenderers);
}

// Attempt: Single JSX renderer
const astroRenderer = jsxRenderers.get('astro');

// Shortcut: only use Astro renderer for MD and MDX files
if ((id.includes('.mdx') || id.includes('.md')) && astroRenderer) {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
const { code: jsxCode } = await esbuild.transform(code, {
loader: getEsbuildLoader(path.extname(id)) as esbuild.Loader,
jsx: 'preserve',
sourcefile: id,
sourcemap: 'inline',
});
return transformJSX({
code: jsxCode,
id,
renderer: astroRenderer,
mode,
ssr,
});
}

// Attempt: Single JSX integration
// If we only have one renderer, we can skip a bunch of work!
if (jsxRenderers.size === 1) {
const nonAstroJsxRenderers = new Map(
[...jsxRenderers.entries()].filter(([key]) => key !== 'astro')
);
if (nonAstroJsxRenderers.size === 1) {
// downlevel any non-standard syntax, but preserve JSX
const { code: jsxCode } = await esbuild.transform(code, {
loader: getEsbuildLoader(path.extname(id)) as esbuild.Loader,
Expand All @@ -150,7 +172,7 @@ export default function jsx({ config, logging }: AstroPluginJSXOptions): Plugin
return transformJSX({
code: jsxCode,
id,
renderer: [...jsxRenderers.values()][0],
renderer: [...nonAstroJsxRenderers.values()][0],
mode,
ssr,
});
Expand Down Expand Up @@ -196,10 +218,6 @@ export default function jsx({ config, logging }: AstroPluginJSXOptions): Plugin
}
}

if (!importSource && jsxRenderers.has('astro') && id.includes('.mdx')) {
importSource = 'astro';
}

// if JSX renderer found, then use that
if (importSource) {
const jsxRenderer = jsxRenderers.get(importSource);
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/vite-plugin-markdown-legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# vite-plugin-markdown-legacy

Adds Markdown support to Vite, both at the top level as well as within `.astro` files.
Loading