Skip to content

Commit

Permalink
Notifications: stay on the same page when switching version
Browse files Browse the repository at this point in the history
This commit implements the same behavior we have in the flyout. If you are
looking at the page `mypage.html` and you click on another version, we keep the
user on `mypage.html` for that "another version".

Now, we implemented the same for the notifications.

Closes #387
  • Loading branch information
humitos committed Oct 1, 2024
1 parent 2c19271 commit 0340c50
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
14 changes: 7 additions & 7 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.

39 changes: 3 additions & 36 deletions src/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { classMap } from "lit/directives/class-map.js";
import { default as objectPath } from "object-path";

import styleSheet from "./flyout.css";
import { AddonBase, addUtmParameters } from "./utils";
import { AddonBase, addUtmParameters, getLinkWithFilename } from "./utils";
import {
EVENT_READTHEDOCS_SEARCH_SHOW,
EVENT_READTHEDOCS_FLYOUT_HIDE,
Expand Down Expand Up @@ -221,37 +221,6 @@ 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']",
);

// 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/

// Keep only one trailing slash
const base = url.replace(/\/+$/, "/");

// 1. remove initial slash to make it relative to the base
// 2. remove the trailing "index.html"
const filename = metaFilename.content
.replace(/\/index.html$/, "/")
.replace(/^\//, "");

return new URL(filename, base);
};

renderVersions() {
if (
!this.config.versions.active.length ||
Expand All @@ -262,7 +231,7 @@ export class FlyoutElement extends LitElement {
}

const getVersionLink = (version) => {
const url = this._getFlyoutLinkWithFilename(version.urls.documentation);
const url = getLinkWithFilename(version.urls.documentation);
const link = html`<a href="${url}">${version.slug}</a>`;
return this.config.versions.current.slug == version.slug
? html`<strong>${link}</strong>`
Expand All @@ -285,9 +254,7 @@ export class FlyoutElement extends LitElement {
}

const getLanguageLink = (translation) => {
const url = this._getFlyoutLinkWithFilename(
translation.urls.documentation,
);
const url = getLinkWithFilename(translation.urls.documentation);
const link = html`<a href="${url}">${translation.language.code}</a>`;
return this.config.projects.current.slug === translation.slug
? html`<strong>${link}</strong>`
Expand Down
4 changes: 2 additions & 2 deletions src/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { html, nothing, render, LitElement } from "lit";

import styleSheet from "./notification.css";
import { AddonBase, addUtmParameters } from "./utils";
import { AddonBase, addUtmParameters, getLinkWithFilename } from "./utils";

export class NotificationElement extends LitElement {
/** @static @property {string} - registered HTML element tag name */
Expand Down Expand Up @@ -243,7 +243,7 @@ export class NotificationElement extends LitElement {

if (stableVersion !== undefined) {
this.stableVersionAvailable = true;
this.urls.stable = stableVersion.urls.documentation;
this.urls.stable = getLinkWithFilename(stableVersion.urls.documentation);
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,35 @@ export function getMetadataValue(name) {
}
return undefined;
}

/**
* Append "resolver filename" to the URL passed.
*
* Examples:
*
* URL: https://docs.readthedocs.io/en/stable/
* Filename: /guides/access/index.html
* Resulting URL: https://docs.readthedocs.io/en/stable/guides/access/
*
* URL: https://docs.readthedocs.io/en/latest/
* Filename: /index.html
* Resulting URL: https://docs.readthedocs.io/en/latest/
*
*/
export function getLinkWithFilename(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 = getMetadataValue("readthedocs-resolver-filename");

// Keep only one trailing slash
const base = url.replace(/\/+$/, "/");

// 1. remove initial slash to make it relative to the base
// 2. remove the trailing "index.html"
const filename = metaFilename
.replace(/\/index.html$/, "/")
.replace(/^\//, "");

return new URL(filename, base);
}

0 comments on commit 0340c50

Please sign in to comment.