summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Erika <3019731+Princesseuh@users.noreply.github.com> 2023-09-29 23:14:09 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-29 23:14:09 +0200
commitdb83237dd3690051203461b6449e116737aee389 (patch)
tree6ad2fc1e490bf91472c6619377cc879d722bf72d
parent148b5b87690ea595c6febb9bc1ca74d90f594111 (diff)
downloadastro-db83237dd3690051203461b6449e116737aee389.tar.gz
astro-db83237dd3690051203461b6449e116737aee389.tar.zst
astro-db83237dd3690051203461b6449e116737aee389.zip
refactor(markdown): Move `astro:assets`-specific code out of the main Vite plugin (#8704)
Diffstat (limited to '')
-rw-r--r--packages/astro/src/vite-plugin-markdown/images.ts34
-rw-r--r--packages/astro/src/vite-plugin-markdown/index.ts32
2 files changed, 41 insertions, 25 deletions
diff --git a/packages/astro/src/vite-plugin-markdown/images.ts b/packages/astro/src/vite-plugin-markdown/images.ts
new file mode 100644
index 000000000..959d34ec9
--- /dev/null
+++ b/packages/astro/src/vite-plugin-markdown/images.ts
@@ -0,0 +1,34 @@
+export type MarkdownImagePath = { raw: string; resolved: string; safeName: string };
+
+export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: string) {
+ return `
+ import { getImage } from "astro:assets";
+ ${imagePaths
+ .map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`)
+ .join('\n')}
+
+ const images = async function() {
+ return {
+ ${imagePaths
+ .map((entry) => `"${entry.raw}": await getImage({src: Astro__${entry.safeName}})`)
+ .join(',\n')}
+ }
+ }
+
+ async function updateImageReferences(html) {
+ return images().then((images) => {
+ return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) =>
+ spreadAttributes({
+ src: images[imagePath].src,
+ ...images[imagePath].attributes,
+ })
+ );
+ });
+ }
+
+ // NOTE: This causes a top-level await to appear in the user's code, which can break very easily due to a Rollup
+ // bug and certain adapters not supporting it correctly. See: https://github.com/rollup/rollup/issues/4708
+ // Tread carefully!
+ const html = await updateImageReferences(${JSON.stringify(html)});
+ `;
+}
diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts
index 6422f7305..3c8a1af46 100644
--- a/packages/astro/src/vite-plugin-markdown/index.ts
+++ b/packages/astro/src/vite-plugin-markdown/index.ts
@@ -16,6 +16,7 @@ import { isMarkdownFile } from '../core/util.js';
import { shorthash } from '../runtime/server/shorthash.js';
import type { PluginMetadata } from '../vite-plugin-astro/types.js';
import { escapeViteEnvReferences, getFileInfo } from '../vite-plugin-utils/index.js';
+import { getMarkdownCodeForImages, type MarkdownImagePath } from './images.js';
interface AstroPluginOptions {
settings: AstroSettings;
@@ -95,7 +96,7 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug
const { headings, imagePaths: rawImagePaths, frontmatter } = renderResult.metadata;
// Resolve all the extracted images from the content
- const imagePaths: { raw: string; resolved: string; safeName: string }[] = [];
+ const imagePaths: MarkdownImagePath[] = [];
for (const imagePath of rawImagePaths.values()) {
imagePaths.push({
raw: imagePath,
@@ -119,34 +120,15 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug
astroServerRuntimeModulePath
)};
import { AstroError, AstroErrorData } from ${JSON.stringify(astroErrorModulePath)};
-
${layout ? `import Layout from ${JSON.stringify(layout)};` : ''}
- import { getImage } from "astro:assets";
- ${imagePaths
- .map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`)
- .join('\n')}
-
- const images = async function() {
- return {
- ${imagePaths
- .map((entry) => `"${entry.raw}": await getImage({src: Astro__${entry.safeName}})`)
- .join(',\n')}
- }
- }
- async function updateImageReferences(html) {
- return images().then((images) => {
- return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) =>
- spreadAttributes({
- src: images[imagePath].src,
- ...images[imagePath].attributes,
- })
- );
- });
+ ${
+ // Only include the code relevant to `astro:assets` if there's images in the file
+ imagePaths.length > 0
+ ? getMarkdownCodeForImages(imagePaths, html)
+ : `const html = ${JSON.stringify(html)};`
}
- const html = await updateImageReferences(${JSON.stringify(html)});
-
export const frontmatter = ${JSON.stringify(frontmatter)};
export const file = ${JSON.stringify(fileId)};
export const url = ${JSON.stringify(fileUrl)};