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

Fix favicon support for query and fragment in URLs #2645

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/neat-deers-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes favicon url parsing of query and fragment
techfg marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions packages/starlight/__tests__/basics/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ describe('FaviconSchema', () => {
expect(favicon.type).toBe('image/jpeg');
});

test('returns the proper href and type attributes when contains query', () => {
const icon = '/custom-icon.gif?v=123456&x=987654';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/gif');
});

test('returns the proper href and type attributes when contains fragment', () => {
const icon = '/custom-icon.png#favicon';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/png');
});

test('returns the proper href and type attributes when contains query and fragment', () => {
const icon = '/custom-icon.ico?v=123456&x=987654#favicon';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/x-icon');
});

test('throws on invalid favicon extensions', () => {
expect(() => FaviconSchema().parse('/favicon.pdf')).toThrow();
});
Expand Down
4 changes: 3 additions & 1 deletion packages/starlight/schemas/favicon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extname } from 'node:path';
import { z } from 'astro/zod';
import { splitQueryAndFragment } from '../utils/path';

const faviconTypeMap = {
'.ico': 'image/x-icon',
Expand All @@ -15,7 +16,8 @@ export const FaviconSchema = () =>
.string()
.default('/favicon.svg')
.transform((favicon, ctx) => {
const ext = extname(favicon).toLowerCase();
const { base } = splitQueryAndFragment(favicon);
const ext = extname(base).toLowerCase();

if (!isFaviconExt(ext)) {
ctx.addIssue({
Expand Down
11 changes: 11 additions & 0 deletions packages/starlight/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ export function ensureHtmlExtension(path: string) {
}
return ensureLeadingSlash(path);
}

export function splitQueryAndFragment(url: string) {
techfg marked this conversation as resolved.
Show resolved Hide resolved
const [urlWithQuery, hash] = url.split('#');
const [urlBase, queryString] = (urlWithQuery || '').split('?');

return {
base: urlBase || '',
query: queryString ? `?${queryString}` : '',
fragment: hash ? `#${hash}` : '',
};
techfg marked this conversation as resolved.
Show resolved Hide resolved
techfg marked this conversation as resolved.
Show resolved Hide resolved
}