From 0b381d53f23c31492cf415057d960f9a5eaa2f3d Mon Sep 17 00:00:00 2001
From: mktbsh
Date: Sun, 28 Jul 2024 02:18:20 +0900
Subject: Merge tags, quick fixes (#2153) (#2154)
Co-authored-by: Chris Swithinbank
---
.changeset/long-baboons-jam.md | 5 +++++
packages/starlight/__tests__/basics/head.test.ts | 21 +++++++++++++++++++++
packages/starlight/utils/head.ts | 4 +++-
3 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 .changeset/long-baboons-jam.md
diff --git a/.changeset/long-baboons-jam.md b/.changeset/long-baboons-jam.md
new file mode 100644
index 00000000..48c560db
--- /dev/null
+++ b/.changeset/long-baboons-jam.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/starlight": patch
+---
+
+Updates `` logic to deduplicate `` tags. This means that custom canonicals set via frontmatter now override the default canonical generated by Starlight.
diff --git a/packages/starlight/__tests__/basics/head.test.ts b/packages/starlight/__tests__/basics/head.test.ts
index db5a454b..999ccce2 100644
--- a/packages/starlight/__tests__/basics/head.test.ts
+++ b/packages/starlight/__tests__/basics/head.test.ts
@@ -11,6 +11,27 @@ describe('createHead', () => {
).toEqual([{ tag: 'title', content: 'Override', attrs: {} }]);
});
+ test('merges two tags', () => {
+ expect(
+ createHead(
+ [{ tag: 'link', attrs: { rel: 'canonical', href: "https://example.com" }, }],
+ [{ tag: 'link', attrs: { rel: 'canonical', href: "https://astro.build" }, content: '' }],
+ )
+ ).toEqual([{ tag: 'link', attrs: { rel: 'canonical', href: "https://astro.build" }, content: '' }]);
+ });
+
+ test('does not merge same link tags', () => {
+ expect(
+ createHead(
+ [{ tag: 'link', attrs: { rel: 'stylesheet', href: "primary.css" }, content: '' }],
+ [{ tag: 'link', attrs: { rel: 'stylesheet', href: "secondary.css" }, content: '' }],
+ )
+ ).toEqual([
+ { tag: 'link', attrs: { rel: 'stylesheet', href: "primary.css" }, content: '' },
+ { tag: 'link', attrs: { rel: 'stylesheet', href: "secondary.css" }, content: '' }
+ ]);
+ });
+
for (const prop of ['name', 'property', 'http-equiv']) {
test(`merges two tags with same ${prop} value`, () => {
expect(
diff --git a/packages/starlight/utils/head.ts b/packages/starlight/utils/head.ts
index 25a20917..c84537c6 100644
--- a/packages/starlight/utils/head.ts
+++ b/packages/starlight/utils/head.ts
@@ -12,7 +12,7 @@ export function createHead(defaults: HeadUserConfig, ...heads: HeadConfig[]) {
}
/**
- * Test if a head config object contains a matching `` or `` tag.
+ * Test if a head config object contains a matching `` or `` or `` tag.
*
* For example, will return true if `head` already contains
* `` and the passed `tag`
@@ -25,6 +25,8 @@ function hasTag(head: HeadConfig, entry: HeadConfig[number]): boolean {
return head.some(({ tag }) => tag === 'title');
case 'meta':
return hasOneOf(head, entry, ['name', 'property', 'http-equiv']);
+ case 'link':
+ return head.some(({ attrs }) => attrs.rel === 'canonical')
default:
return false;
}
--
cgit