Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/templates/assets/javascripts/_/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface Config {
search: string /* Search worker URL */
tags?: Record<string, string> /* Tags mapping */
version?: Versioning /* Versioning */
canonical_version?: string /* Optional canonical version set in Mike */
}

/* ----------------------------------------------------------------------------
Expand Down
19 changes: 18 additions & 1 deletion src/templates/assets/javascripts/integrations/version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,25 @@ export function setupVersionSelector(
.pipe(
map(sitemap => {
const location = getLocation()
// checkPath is used to check if the current page is in the sitemap even if it is the canonical version
let checkPath
// path is the actual path of the page we want to redirect to
const path = location.href.replace(config.base, url)
return sitemap.has(path.split("#")[0])

// If the canonical version is set, we replace the current version with the canonical version in the checkPath
if (config.canonical_version) {
const baseUrl = new URL(config.base)
// Make sure the canonical version has a trailing slash if the base URL has one
if (config.base.endsWith("/") && !config.canonical_version.endsWith("/")) {
baseUrl.pathname = `${config.canonical_version}/`
} else {
baseUrl.pathname = config.canonical_version
}
checkPath = location.href.replace(config.base, baseUrl.href)
} else {
checkPath = path
}
return sitemap.has(checkPath.split("#")[0])
? new URL(path)
: new URL(url)
})
Expand Down
5 changes: 4 additions & 1 deletion src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@
{%- if config.extra.version -%}
{%- set mike = config.plugins.get("mike") -%}
{%- if not mike or mike.config.version_selector -%}
{%- set _ = app.update({ "version": config.extra.version }) -%}
{%- set _ = app.update({ "version": config.extra.version}) -%}
{%- endif -%}
{%- if mike.config.canonical_version -%}
{%- set _ = app.update({ "canonical_version": mike.config.canonical_version }) -%}
{%- endif -%}
{%- endif -%}

Expand Down