full site update

This commit is contained in:
2025-07-24 18:46:24 +02:00
parent bfe2b90d8d
commit 37a6e0ab31
6912 changed files with 540482 additions and 361712 deletions

View File

@@ -1,2 +1,2 @@
import type { ContentEntryType } from '../@types/astro.js';
import type { ContentEntryType } from '../types/public/content.js';
export declare const markdownContentEntryType: ContentEntryType;

View File

@@ -6,23 +6,21 @@ const markdownContentEntryType = {
async getEntryInfo({ contents, fileUrl }) {
const parsed = safeParseFrontmatter(contents, fileURLToPath(fileUrl));
return {
data: parsed.data,
body: parsed.content,
slug: parsed.data.slug,
rawData: parsed.matter
data: parsed.frontmatter,
body: parsed.content.trim(),
slug: parsed.frontmatter.slug,
rawData: parsed.rawFrontmatter
};
},
// We need to handle propagation for Markdown because they support layouts which will bring in styles.
handlePropagation: true,
async getRenderFunction(config) {
const processor = await createMarkdownProcessor(config.markdown);
const processor = await createMarkdownProcessor({
image: config.image,
...config.markdown
});
return async function renderToString(entry) {
if (!entry.body) {
return {
html: ""
};
}
const result = await processor.render(entry.body, {
const result = await processor.render(entry.body ?? "", {
frontmatter: entry.data,
// @ts-expect-error Internal API
fileURL: entry.filePath ? pathToFileURL(entry.filePath) : void 0
@@ -31,7 +29,7 @@ const markdownContentEntryType = {
html: result.code,
metadata: {
...result.metadata,
imagePaths: Array.from(result.metadata.imagePaths)
imagePaths: result.metadata.localImagePaths.concat(result.metadata.remoteImagePaths)
}
};
};

View File

@@ -2,4 +2,4 @@ export type MarkdownImagePath = {
raw: string;
safeName: string;
};
export declare function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: string): string;
export declare function getMarkdownCodeForImages(localImagePaths: MarkdownImagePath[], remoteImagePaths: string[], html: string): string;

View File

@@ -1,12 +1,12 @@
function getMarkdownCodeForImages(imagePaths, html) {
function getMarkdownCodeForImages(localImagePaths, remoteImagePaths, html) {
return `
import { getImage } from "astro:assets";
${imagePaths.map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`).join("\n")}
${localImagePaths.map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`).join("\n")}
const images = async function(html) {
const imageSources = {};
${imagePaths.map((entry) => {
const rawUrl = JSON.stringify(entry.raw);
${localImagePaths.map((entry) => {
const rawUrl = JSON.stringify(entry.raw).replace(/'/g, "'");
return `{
const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(
/[.*+?^${}()|[\]\\]/g,
@@ -16,44 +16,56 @@ function getMarkdownCodeForImages(imagePaths, html) {
let occurrenceCounter = 0;
while ((match = regex.exec(html)) !== null) {
const matchKey = ${rawUrl} + '_' + occurrenceCounter;
const imageProps = JSON.parse(match[1].replace(/"/g, '"'));
const imageProps = JSON.parse(match[1].replace(/"/g, '"').replace(/'/g, "'"));
const { src, ...props } = imageProps;
imageSources[matchKey] = await getImage({src: Astro__${entry.safeName}, ...props});
occurrenceCounter++;
}
}`;
}).join("\n")}
${remoteImagePaths.map((raw) => {
const rawUrl = JSON.stringify(raw).replace(/'/g, "'");
return `{
const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(
/[.*+?^${}()|[\]\\]/g,
"\\\\$&"
)} + '[^"]*)"', 'g');
let match;
let occurrenceCounter = 0;
while ((match = regex.exec(html)) !== null) {
const matchKey = ${rawUrl} + '_' + occurrenceCounter;
const props = JSON.parse(match[1].replace(/"/g, '"').replace(/'/g, "'"));
imageSources[matchKey] = await getImage(props);
occurrenceCounter++;
}
}`;
}).join("\n")}
return imageSources;
};
async function updateImageReferences(html) {
return images(html).then((imageSources) => {
return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) => {
const decodedImagePath = JSON.parse(imagePath.replace(/"/g, '"'));
// Use the 'index' property for each image occurrence
const srcKey = decodedImagePath.src + '_' + decodedImagePath.index;
if (imageSources[srcKey].srcSet && imageSources[srcKey].srcSet.values.length > 0) {
imageSources[srcKey].attributes.srcset = imageSources[srcKey].srcSet.attribute;
}
const { index, ...attributesWithoutIndex } = imageSources[srcKey].attributes;
return spreadAttributes({
src: imageSources[srcKey].src,
...attributesWithoutIndex,
});
});
});
}
async function updateImageReferences(html) {
const imageSources = await images(html);
// 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)});
return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) => {
const decodedImagePath = JSON.parse(imagePath.replace(/"/g, '"'));
// Use the 'index' property for each image occurrence
const srcKey = decodedImagePath.src + '_' + decodedImagePath.index;
if (imageSources[srcKey].srcSet && imageSources[srcKey].srcSet.values.length > 0) {
imageSources[srcKey].attributes.srcset = imageSources[srcKey].srcSet.attribute;
}
const { index, ...attributesWithoutIndex } = imageSources[srcKey].attributes;
return spreadAttributes({
src: imageSources[srcKey].src,
...attributesWithoutIndex,
});
});
}
const html = async () => await updateImageReferences(${JSON.stringify(html)});
`;
}
export {

View File

@@ -1,6 +1,6 @@
import type { Plugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import type { Logger } from '../core/logger/core.js';
import type { AstroSettings } from '../types/astro.js';
interface AstroPluginOptions {
settings: AstroSettings;
logger: Logger;

View File

@@ -1,13 +1,13 @@
import fs from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
InvalidAstroDataError,
createMarkdownProcessor
createMarkdownProcessor,
isFrontmatterValid
} from "@astrojs/markdown-remark";
import { normalizePath } from "vite";
import { safeParseFrontmatter } from "../content/utils.js";
import { AstroError, AstroErrorData } from "../core/errors/index.js";
import { isMarkdownFile } from "../core/util.js";
import { isMarkdownFile, isPage } from "../core/util.js";
import { normalizePath } from "../core/viteUtils.js";
import { shorthash } from "../runtime/server/shorthash.js";
import { createDefaultAstroMetadata } from "../vite-plugin-astro/metadata.js";
import { getFileInfo } from "../vite-plugin-utils/index.js";
@@ -44,23 +44,32 @@ function markdown({ settings, logger }) {
const raw = safeParseFrontmatter(rawFile, id);
const fileURL = pathToFileURL(fileId);
if (!processor) {
processor = createMarkdownProcessor(settings.config.markdown);
processor = createMarkdownProcessor({
image: settings.config.image,
experimentalHeadingIdCompat: settings.config.experimental.headingIdCompat,
...settings.config.markdown
});
}
const renderResult = await (await processor).render(raw.content, {
// @ts-expect-error passing internal prop
fileURL,
frontmatter: raw.data
}).catch((err) => {
if (err instanceof InvalidAstroDataError) {
throw new AstroError(AstroErrorData.InvalidFrontmatterInjectionError);
}
throw err;
frontmatter: raw.frontmatter
});
if (!isFrontmatterValid(renderResult.metadata.frontmatter)) {
throw new AstroError(AstroErrorData.InvalidFrontmatterInjectionError);
}
let html = renderResult.code;
const { headings, imagePaths: rawImagePaths, frontmatter } = renderResult.metadata;
const imagePaths = [];
for (const imagePath of rawImagePaths.values()) {
imagePaths.push({
const {
headings,
localImagePaths: rawLocalImagePaths,
remoteImagePaths,
frontmatter
} = renderResult.metadata;
const isMarkdownPage = isPage(fileURL, settings);
const charset = isMarkdownPage ? '<meta charset="utf-8">' : "";
const localImagePaths = [];
for (const imagePath of rawLocalImagePaths) {
localImagePaths.push({
raw: imagePath,
safeName: shorthash(imagePath)
});
@@ -80,7 +89,7 @@ function markdown({ settings, logger }) {
${layout ? `import Layout from ${JSON.stringify(layout)};` : ""}
${// 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)};`}
localImagePaths.length > 0 || remoteImagePaths.length > 0 ? getMarkdownCodeForImages(localImagePaths, remoteImagePaths, html) : `const html = () => ${JSON.stringify(html)};`}
export const frontmatter = ${JSON.stringify(frontmatter)};
export const file = ${JSON.stringify(fileId)};
@@ -88,8 +97,8 @@ function markdown({ settings, logger }) {
export function rawContent() {
return ${JSON.stringify(raw.content)};
}
export function compiledContent() {
return html;
export async function compiledContent() {
return await html();
}
export function getHeadings() {
return ${JSON.stringify(headings)};
@@ -110,8 +119,8 @@ function markdown({ settings, logger }) {
compiledContent,
'server:root': true,
}, {
'default': () => render\`\${unescapeHTML(html)}\`
})}\`;` : `render\`\${maybeRenderHead(result)}\${unescapeHTML(html)}\`;`}
'default': () => render\`\${unescapeHTML(html())}\`
})}\`;` : `render\`${charset}\${maybeRenderHead(result)}\${unescapeHTML(html())}\`;`}
});
export default Content;
`;