mike
mike copied to clipboard
Keep full url when switching version ?
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
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
This feature is already implemented in Material for MkDocs, which you're using.
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.
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 ^^
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 :)
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);
})();
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.
The way
mkdocs-material
seems to do it is to fetchsitemap.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.
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.