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

Keep full url when switching version ? #81

Open
nvuillam opened this issue Nov 11, 2021 · 8 comments
Open

Keep full url when switching version ? #81

nvuillam opened this issue Nov 11, 2021 · 8 comments
Milestone

Comments

@nvuillam
Copy link

Hi,

Thanks for the great tool !

Is there a way to remain on the current page when switching version ?

Example, when I'm on https://megalinter.github.io/v5.0.5/all_linters/ , I would like to land on https://megalinter.github.io/beta/all_linters/ when I switch to beta version, but unfortunately it always brings me back to the root URL

image

image

image

Maybe with a javascript hack ? ( I already do that to rename/reorder versions in the dropdown widget) -> https://github.com/megalinter/megalinter/blob/main/docs/javascripts/version-mike.js

@squidfunk
Copy link

This feature is already implemented in Material for MkDocs, which you're using.

@jimporter
Copy link
Owner

Yeah, this is a goal for 2.0, but there are some subtleties I haven't quite worked out, like what to do when the directory structure has changed between versions. In that case, it'd be nice to redirect to the homepage for the specified version, or at least to have a nicer 404 page than we currently have.

@jimporter jimporter added this to the 2.0 milestone Nov 24, 2021
@nvuillam
Copy link
Author

This feature is already implemented in Material for MkDocs, which you're using.

I saw that but it's only in insiders version for now ^^

@nvuillam
Copy link
Author

Yeah, this is a goal for 2.0, but there are some subtleties I haven't quite worked out, like what to do when the directory structure has changed between versions. In that case, it'd be nice to redirect to the homepage for the specified version, or at least to have a nicer 404 page than we currently have.

I agree,a nice 404 with a message "this page was not existing in version XXX" would be nice :)

@bridgeL
Copy link

bridgeL commented Nov 7, 2022

Maybe with a javascript hack ? ( I already do that to rename/reorder versions in the dropdown widget) -> megalinter/megalinter@main/docs/javascripts/version-mike.js

I have tried your js code, but it seems that it doesn't work, maybe I have got something wrong. I write an another js code and it can work. Just put it here for someone who may need it.

(window.onload = function () {
  setInterval(function () {
    var items = document.querySelectorAll('.md-version__link:not(.changed)');
    if (!items.length) return;

    var item = document.querySelector(".md-version__current");
    if (!item) return;

    var version_now = item.innerHTML;
    console.log(version_now);
    var i = window.location.pathname.indexOf(version_now) + version_now.length + 1;
    var pathname = window.location.pathname.slice(i);

    for (var item of items) {
      item.href += pathname;
      item.className += " changed";
      console.log(item.innerHTML, "change successfully");
    }
  }, 100);
})();

@ilyagr
Copy link

ilyagr commented Aug 27, 2023

The way mkdocs-material seems to do it is to fetch sitemap.xml to figure out which URLs are valid for each version. See

https://github.com/squidfunk/mkdocs-material/blob/f022873c69bb10b419dfbecab2cc0a2d6ac794b1/src/assets/javascripts/integrations/version/index.ts#L132

and

https://github.com/squidfunk/mkdocs-material/blob/f022873c69bb10b419dfbecab2cc0a2d6ac794b1/src/assets/javascripts/integrations/sitemap/index.ts#L91

If the URL isn't valid in the version we're switching to, it just redirects to the default page for that version.

I was briefly considering trying to implement this for readthedocs theme, but I suspect that my javascript knowledge is not up to par, especially if we want the code to work in many browsers.


OTOH, I'm not sure Material's behavior I described above is actually preferable to just redirecting to the same path in the other version and having a reasonable 404 page in case there is no page at that path.

@jimporter
Copy link
Owner

The way mkdocs-material seems to do it is to fetch sitemap.xml to figure out which URLs are valid for each version.

Using sitemap.xml is an interesting idea. I think that would resolve a lot of the issues I was seeing with an implementation here. I'll look into it.

@ilyagr
Copy link

ilyagr commented Aug 28, 2023

Using sitemap.xml is an interesting idea. I think that would resolve a lot of the issues I was seeing with an implementation here. I'll look into it.

This approach also has some downsides (though I don't think they are dealbreakers). Most notably, if the site_url configured in mkdocs.yml, the feature doesn't work. I described this in https://github.com/squidfunk/mkdocs-material/pull/5898/files.

@jimporter jimporter modified the milestones: 2.0, 3.0 Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants