Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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/every-breads-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes support for links containing a protocol like `mailto:` in the sidebar
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
8 changes: 8 additions & 0 deletions packages/starlight/__tests__/sidebar/navigation-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
8 changes: 8 additions & 0 deletions packages/starlight/__tests__/sidebar/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ describe('getSidebar', () => {
"label": "API (deprecated)",
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "mailto:me@example.com",
"isCurrent": false,
"label": "E-mail me",
"type": "link",
},
]
`);
});
Expand Down
5 changes: 5 additions & 0 deletions packages/starlight/__tests__/sidebar/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@ export default defineVitestConfig({
label: 'API (deprecated)',
items: [{ autogenerate: { directory: '/Deprecated API/' } }],
},
// A link using the `mailto:` protocol.
{
label: 'E-mail me',
link: 'mailto:me@example.com',
},
],
});
6 changes: 3 additions & 3 deletions packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type {
SidebarAutogenerateRouteData,
} from './routing/types';
import { localeToLang, localizedFilePath, slugToPathname } from './slugs';
import { isAbsoluteUrl } from './url';
import { hasProtocol } from './url';
import type { StarlightConfig } from './user-config';

const DirKey = Symbol('DirKey');
Expand Down Expand Up @@ -120,7 +120,7 @@ function entriesFromAutogenerateConfig(
/** Create a link entry from a manual link item in user config. */
function linkFromSidebarLinkItem(item: SidebarLinkItem, locale: string | undefined) {
let href = item.link;
if (!isAbsoluteUrl(href)) {
if (!hasProtocol(href)) {
href = ensureLeadingSlash(href);
// Inject current locale into link.
if (locale) href = '/' + locale + href;
Expand Down Expand Up @@ -188,7 +188,7 @@ function makeSidebarLink(
opts: MakeLinkOptions & { autogenerate: SidebarAutogenerateRouteData }
): SidebarAutoLink;
function makeSidebarLink({ attrs, badge, href, label, autogenerate }: MakeLinkOptions) {
if (!isAbsoluteUrl(href)) {
if (!hasProtocol(href)) {
href = formatPath(href);
}
return makeLink({ label, href, badge, attrs, autogenerate });
Expand Down
8 changes: 8 additions & 0 deletions packages/starlight/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@ const HTTPProtocolRegEx = /^https?:\/\//;

/** Check if a string starts with one of `http://` or `https://`. */
export const isAbsoluteUrl = (link: string) => HTTPProtocolRegEx.test(link);

/** Check if a string contains a protocol (e.g., `http:`, `https:`, `mailto:`). */
export const hasProtocol = (link: string) => {
const colonIndex = link.indexOf(':');
if (colonIndex === -1) return false;
const slashIndex = link.indexOf('/');
return slashIndex === -1 || colonIndex < slashIndex;
};
Comment thread
delucis marked this conversation as resolved.
Resolved
Loading