Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyout: keeps the page when switching version/language #242

Merged
merged 10 commits into from
Mar 5, 2024
10 changes: 5 additions & 5 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions src/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ export class FlyoutElement extends LitElement {
`;
}

_getFlyoutLinkWithFilename = (url) => {
// Get the resolver's filename returned by the application (as HTTP header)
// and injected by Cloudflare Worker as a meta HTML tag
const metaFilename = document.querySelector(
"meta[name='readthedocs-resolver-filename']",
);
humitos marked this conversation as resolved.
Show resolved Hide resolved

// Remove trailing slashes from the version's URL and append the
// resolver's filename after removing trailing ``index.html``.
// Examples:
//
// URL: https://docs.readthedocs.io/en/latest/
// Filename: /index.html
// Flyuout URL: https://docs.readthedocs.io/en/latest/
//
// URL: https://docs.readthedocs.io/en/stable/
// Filename: /guides/access/index.html
// Flyuout URL: https://docs.readthedocs.io/en/stable/guides/access/
return (
humitos marked this conversation as resolved.
Show resolved Hide resolved
url.replace(/\/+$/, "") +
metaFilename.content.replace(/\/index.html$/, "")
humitos marked this conversation as resolved.
Show resolved Hide resolved
);
};

renderVersions() {
if (
!this.config.addons.flyout.versions.length ||
Expand All @@ -199,7 +223,8 @@ export class FlyoutElement extends LitElement {
const currentVersion = this.config.versions.current.slug;

const getVersionLink = (version) => {
const link = html`<a href="${version.url}">${version.slug}</a>`;
const url = this._getFlyoutLinkWithFilename(version.url);
const link = html`<a href="${url}">${version.slug}</a>`;
return currentVersion && version.slug === currentVersion
? html`<strong>${link}</strong>`
: link;
Expand All @@ -223,7 +248,8 @@ export class FlyoutElement extends LitElement {
const currentTranslation = this.config.projects.current.language.code;

const getLanguageLink = (translation) => {
const link = html`<a href="${translation.url}">${translation.slug}</a>`;
const url = this._getFlyoutLinkWithFilename(translation.url);
const link = html`<a href="${url}">${translation.slug}</a>`;
return currentTranslation && translation.slug === currentTranslation
? html`<strong>${link}</strong>`
: link;
Expand Down