jimporter / mike

Manage multiple versions of your MkDocs-powered documentation via Git
BSD 3-Clause "New" or "Revised" License
508 stars 45 forks source link

Keep full url when switching version ? #81

Open nvuillam opened 2 years ago

nvuillam commented 2 years ago

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 commented 2 years ago

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

jimporter commented 2 years ago

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.

nvuillam commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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.