Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .changeset/fix-css-assetprefix-remote-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'astro': patch
---

Fixes CSS `assetsPrefix` with remote URLs incorrectly prepending a forward slash

When using `build.assetsPrefix` with a remote URL (e.g., `https://cdn.example.com`) for CSS assets, the generated `<link>` elements were incorrectly getting a `/` prepended to the full URL, resulting in invalid URLs like `/https://cdn.example.com/assets/style.css`.

This fix checks if the stylesheet link is a remote URL before prepending the forward slash.
5 changes: 5 additions & 0 deletions .changeset/fix-mdx-error-message-escaping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes greedy regex in error message markdown rendering that caused link syntax examples to capture extra characters
4 changes: 2 additions & 2 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ZodIssueCode, z } from 'zod';
import type { GetImageResult, ImageMetadata } from '../assets/types.js';
import { imageSrcToImportId } from '../assets/utils/resolveImports.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { prependForwardSlash } from '../core/path.js';
import { isRemotePath, prependForwardSlash } from '../core/path.js';
import {
type AstroComponentFactory,
createComponent,
Expand Down Expand Up @@ -821,7 +821,7 @@ async function render({
.map((link: any) => {
return renderUniqueStylesheet(result, {
type: 'external',
src: prependForwardSlash(link),
src: isRemotePath(link) ? link : prependForwardSlash(link),
});
})
.join('');
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/errors/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function getDocsForError(err: ErrorWithMetadata): string | undefined {
}
}

const linkRegex = /\[([^[]+)\]\((.*)\)/g;
const linkRegex = /\[([^[]+)\]\(([^)]*)\)/g;
const boldRegex = /\*\*(.+)\*\*/g;
const urlRegex = / ((?:https?|ftp):\/\/[-\w+&@#\\/%?=~|!:,.;]*[-\w+&@#\\/%=~|])/gi;
const codeRegex = /`([^`]+)`/g;
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ describe('Assets Prefix - Static', () => {
const imgAsset = $('img');
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
});

it('MDX content collection CSS imports should start with assetsPrefix', async () => {
const html = await fixture.readFile('/mdx-blog/index.html');
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
assert.ok(stylesheets.length > 0, 'Expected at least one stylesheet');
stylesheets.each((_i, el) => {
assert.match(el.attribs.href, assetsPrefixRegex);
});
});
});

describe('Assets Prefix - with path prefix', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mdx from '@astrojs/mdx';
import react from '@astrojs/react';
import { defineConfig } from 'astro/config';
import { testImageService } from '../../test-image-service.js';
Expand All @@ -6,9 +7,10 @@ import { testImageService } from '../../test-image-service.js';
export default defineConfig({
// test custom base to make sure things work
base: '/custom-base',
integrations: [react()],
integrations: [react(), mdx()],
build: {
assetsPrefix: 'http://localhost:4321',
inlineStylesheets: 'never',
},
image: {
service: testImageService(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/mdx": "workspace:*",
"@astrojs/react": "workspace:*",
"astro": "workspace:*",
"react": "^18.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: MDX Post with CSS
cover: ../../assets/penguin1.jpg
---

import './styles.css';

# Hello from MDX

This MDX file imports a CSS file to test assetsPrefix with content collections.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
import { getCollection } from "astro:content";
const posts = await getCollection("blog");
const mdxPost = posts.find(p => p.id === 'mdx-with-css.mdx');
const { Content } = await mdxPost.render();
---

<html>
<head>
<title>MDX Blog</title>
</head>
<body>
<Content />
</body>
</html>
107 changes: 107 additions & 0 deletions packages/astro/test/units/errors/dev-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { renderErrorMarkdown } from '../../../dist/core/errors/dev/utils.js';

describe('renderErrorMarkdown', () => {
describe('html target', () => {
it('converts markdown links to HTML anchor tags', () => {
const input = 'Check the [documentation](https://docs.astro.build)';
const result = renderErrorMarkdown(input, 'html');
assert.equal(
result,
'Check the <a href="https://docs.astro.build" target="_blank">documentation</a>',
);
});

it('converts bold text to HTML b tags', () => {
const input = 'This is **important** text';
const result = renderErrorMarkdown(input, 'html');
assert.equal(result, 'This is <b>important</b> text');
});

it('converts inline code to HTML code tags', () => {
const input = 'Use the `console.log` function';
const result = renderErrorMarkdown(input, 'html');
assert.equal(result, 'Use the <code>console.log</code> function');
});

it('converts bare URLs to HTML anchor tags', () => {
const input = 'Visit https://astro.build for more info';
const result = renderErrorMarkdown(input, 'html');
assert.equal(
result,
'Visit <a href="https://astro.build" target="_blank">https://astro.build</a> for more info',
);
});

it('escapes HTML entities in the input', () => {
const input = 'Use <script> tags carefully';
const result = renderErrorMarkdown(input, 'html');
assert.ok(result.includes('&lt;script&gt;'));
});

it('handles multiple markdown elements', () => {
const input = 'Check **bold** and `code` and [link](https://example.com)';
const result = renderErrorMarkdown(input, 'html');
assert.ok(result.includes('<b>bold</b>'));
assert.ok(result.includes('<code>code</code>'));
assert.ok(result.includes('<a href="https://example.com" target="_blank">link</a>'));
});

it('handles link with parentheses followed by more content', () => {
// This is the bug case from issue #15068
// The link [text](url) should not consume content after it
const input = 'use [text](url) for links';
const result = renderErrorMarkdown(input, 'html');
assert.equal(result, 'use <a href="url" target="_blank">text</a> for links');
});

it('handles link followed by closing parenthesis', () => {
// Edge case: link inside parentheses like "(use [text](url))"
const input = '(use [text](url))';
const result = renderErrorMarkdown(input, 'html');
// The link should only capture 'url', not 'url)'
assert.equal(result, '(use <a href="url" target="_blank">text</a>)');
});

it('handles escaped HTML followed by link syntax', () => {
// This simulates the MDX error message case
const input = 'use <code>[text](url)</code>';
const result = renderErrorMarkdown(input, 'html');
// After HTML escaping, <code> becomes &lt;code&gt;
// The link should still be parsed correctly without consuming &gt;)
assert.ok(result.includes('<a href="url" target="_blank">text</a>'));
assert.ok(result.includes('&lt;code&gt;'));
assert.ok(result.includes('&lt;/code&gt;'));
});

it('handles multiple links in the same message', () => {
const input = 'See [docs](https://docs.astro.build) and [guide](https://guide.astro.build)';
const result = renderErrorMarkdown(input, 'html');
assert.ok(result.includes('<a href="https://docs.astro.build" target="_blank">docs</a>'));
assert.ok(result.includes('<a href="https://guide.astro.build" target="_blank">guide</a>'));
});
});

describe('cli target', () => {
it('formats markdown links for CLI output', () => {
const input = 'Check the [documentation](https://docs.astro.build)';
const result = renderErrorMarkdown(input, 'cli');
// CLI output should contain the link text and URL
assert.ok(result.includes('documentation'));
assert.ok(result.includes('https://docs.astro.build'));
});

it('formats bold text for CLI output', () => {
const input = 'This is **important** text';
const result = renderErrorMarkdown(input, 'cli');
assert.ok(result.includes('important'));
});

it('formats bare URLs for CLI output', () => {
const input = 'Visit https://astro.build for more info';
const result = renderErrorMarkdown(input, 'cli');
assert.ok(result.includes('https://astro.build'));
});
});
});
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading