Skip to content

Commit

Permalink
Fix localizeUrl() for .html URLs
Browse files Browse the repository at this point in the history
Closes #1423
  • Loading branch information
delucis committed Jan 30, 2024
1 parent ec4ba9b commit 1ae8cac
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/starlight/utils/localizedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ export function localizedUrl(url: URL, locale: string | undefined): URL {
// Temporarily remove base to simplify
if (hasBase) url.pathname = url.pathname.replace(base, '');
const [_leadingSlash, baseSegment] = url.pathname.split('/');
if (baseSegment && baseSegment in config.locales) {
// Strip .html extension to handle file output builds where URL might be e.g. `/en.html`
const htmlExt = '.html';
const isRootHtml = baseSegment?.endsWith(htmlExt);
const baseSlug = isRootHtml ? baseSegment?.slice(0, -1 * htmlExt.length) : baseSegment;
if (baseSlug && baseSlug in config.locales) {
// We’re in a localized route, substitute the new locale (or strip for root lang).
url.pathname = locale
? url.pathname.replace(baseSegment, locale)
: url.pathname.replace('/' + baseSegment, '');
if (locale) {
url.pathname = url.pathname.replace(baseSlug, locale);
} else if (isRootHtml) {
url.pathname = '/index.html';
} else {
url.pathname = url.pathname.replace('/' + baseSlug, '');
}
} else if (locale) {
// We’re in the root language. Inject the new locale if we have one.
url.pathname = '/' + locale + url.pathname;
if (baseSegment === 'index.html') {
url.pathname = '/' + locale + '.html';
} else {
url.pathname = '/' + locale + url.pathname;
}
}
// Restore base
if (hasBase) url.pathname = base + url.pathname;
Expand Down

0 comments on commit 1ae8cac

Please sign in to comment.