Skip to content

Commit 21e5514

Browse files
authored
Fix canonicals (finally) (#300)
Requires pre-release versions to be marked as prerelease so Antora doesn't consider them as 'latest'. Pre-release versions will be included in the version selector, unless `page-nav-selectors-hide-prerelease` is set to true. This PR also modifies `page.canonicalUrl`. After it is set by Antora, a helper in the ui-bundle checks the value, and if it contains the version name that is the latest version of the page's component, the url is updated by replacing the version name with 'current'. This ensures that all our canonicals point to current (except where there is no equivalent of a page in the latest release. In that case the canonical is self-referencing).
1 parent 48d92c8 commit 21e5514

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// The canonical URL for a page has already been calculated by Antora
2+
// and is available in the page object a page.canonicalUrl
3+
// if the canonical URL for a page is in the current version of the component
4+
// we want to modify the canonical by replacing that version with 'current'
5+
// we can allow for a page attribute to be set to override 'current'
6+
7+
'use strict'
8+
9+
module.exports = (page) => {
10+
const re = new RegExp(`/${page.latest.version}/`)
11+
const latestVersionPath = `/${(page.attributes['latest-version-path'] || 'current')}/`
12+
return page.canonicalUrl.replace(re, latestVersionPath)
13+
}

src/partials/head-info.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#if page.canonicalUrl}}
2-
{{#with page.canonicalUrl}}
2+
{{#with (neo-canonical-current page)}}
33
<link rel="canonical" href="{{{this}}}">
44
{{/with}}
55
{{else}}

src/partials/nav-selectors.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
<select id="selector-version" data-current="{{@root.page.version}}" class="version-selector dropdown-styles">
1919
{{#each page.versions}}
20-
{{#unless this.prerelease}}
20+
{{#unless (and this.prerelease @root.page.attributes.nav-selectors-hide-prerelease)}}
2121
<option
2222
data-version="{{this.displayVersion}}"
23+
{{#if this.prerelease }}data-prerelease="{{this.prerelease}}"{{/if}}
2324
value="{{{relativize this.url}}}"
2425
{{#if (eq this.version @root.page.version)}} selected{{/if}}
2526
{{~#if this.missing}} disabled{{/if}}

0 commit comments

Comments
 (0)