Documenter.jl icon indicating copy to clipboard operation
Documenter.jl copied to clipboard

Feature request: stay on page when changing version

Open langestefan opened this issue 1 month ago • 17 comments

Often when I am reading documentation, I want to change the version of the docs I am looking at.

For example, I am currently reading: https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKLanguage/

When I want to read the dev version of this page, I go to the bottom left and change the version to dev.

Instead of forwarding me to https://docs.sciml.ai/ModelingToolkit/dev/basics/MTKLanguage/, this forwards me to the homepage at https://docs.sciml.ai/ModelingToolkit/dev/.

Ofcourse sometimes that particular page is not available in different versions of the documentation, but in that case we can fall back to the homepage. Can we make it so that, if that page is available, changing the version leads to the page I'm currently viewing?

langestefan avatar Nov 01 '25 16:11 langestefan

I'd like this as well, but it's not so easy. Right now we have no good way to determine whether the target page exists. So a naive approach would end up on a 404 error page.

And of course pages built with an older Documenter version won't have this (so you can't switch back) unless we implement some tool that updates old page's version.

But of course all of that could be overcome, I simply wanted to explain why it isn't that easy.

fingolfin avatar Nov 01 '25 17:11 fingolfin

Got it, thanks for responding! This is available in sphinx (the python docs framework), so maybe we can take inspiration from that.

langestefan avatar Nov 01 '25 17:11 langestefan

Sure. If anyone knows how sphinx does it, please leave a comment here explaining it.

fingolfin avatar Nov 03 '25 10:11 fingolfin

Isn't it as basic as this?

  1. Check if page exists. If it returns a 200 OK, forward to it.
  2. If it doesn't exist, forward to home page.

langestefan avatar Nov 03 '25 11:11 langestefan

Yeah, OK, but how do you get the browser to do that? By inserting some JavaScript?

fingolfin avatar Nov 03 '25 14:11 fingolfin

I'm not a frontend expert, but this sounds like something that should be relatively straightforward. Claude tells me this (untested, but looks reasonable, as a starting point):

<!DOCTYPE html>
<html>
<head>
  <title>Smart Links</title>
</head>
<body>

<a href="/primary-page" 
   data-fallback="/backup-page"
   class="smart-link">
   Click me
</a>

<script>
document.querySelectorAll('.smart-link').forEach(link => {
  link.addEventListener('click', async (e) => {
    e.preventDefault();
    
    const primaryUrl = link.href;
    const fallbackUrl = link.dataset.fallback;
    
    try {
      const response = await fetch(primaryUrl, { 
        method: 'HEAD' // Only fetch headers, not the whole page
      });
      
      if (response.status === 200) {
        window.location.href = primaryUrl;
      } else {
        window.location.href = fallbackUrl;
      }
    } catch (error) {
      // Network error or other failure - use fallback
      window.location.href = fallbackUrl;
    }
  });
});
</script>

</body>
</html>

An alternative approach for handling this would be via post-processing, during deployment. Deployment has access to the full gh-pages branch, so it could check which pages exist in other versions. Or, it could draw information from the Inventory file.

The post-processing approach could also be realized by a completely separate script or package.

In any case, I think staying on the current page when changing version would be a very desirable feature.

goerz avatar Nov 03 '25 15:11 goerz

I'm not a frontend expert, but this sounds like something that should be relatively straightforward. Claude tells me this (untested, but looks reasonable, as a starting point):

I gave a shot at implementing this at #2801. Could somebody with more frontend knowledge than me check this out? It works correctly on my local machine.

langestefan avatar Nov 03 '25 16:11 langestefan

X-ref: #2479

mortenpi avatar Nov 03 '25 18:11 mortenpi

Summary of current state of this issue: thanks to PR #2801 we have a basic version of this: when using the version selector popup to switch to different version, then we now stay on the corresponding page in the chose version (if available)

Missing in order to close this are, IMHO:

  1. the code in warner.js (which shows a badge when looking at old or dev versions of manuals, with a link to the stable version) does not yet do this, but really also should
  2. the logic does not take the "hash" / anchor part of the URL into account
  3. support in DocumenterTools to patch older versions of a manual to also use the improved JavaScript, so that one can also "switch back". That said, we could also open three new issues for those and close this one.

fingolfin avatar Nov 23 '25 15:11 fingolfin

That said, we could also open three new issues for those and close this one.

I would be in favour of that. This may be a long shot, but maybe a claude agent or copilot (with some guidance) could tackle these issues separately? It would require a clear issue description and someone would have to test the implementation.

langestefan avatar Nov 24 '25 11:11 langestefan

The first two yes, I would not be sure about that last one though.

asinghvi17 avatar Nov 24 '25 14:11 asinghvi17

I am pretty sure someone could use Claud/Copilot/etc. to assist them to tackle any of the three. But I feel that's a bit besides the point: we care about the issues being resolved, which tool (be it an AI, or an IDE, or something else) they use for that is not part of the issue.

fingolfin avatar Nov 24 '25 15:11 fingolfin

My point was, code agents can work 'independently' on issues if they are simple enough and self-contained. So if nobody is available / willing to tackle this, perhaps an agent can work on the issue.

langestefan avatar Nov 24 '25 16:11 langestefan

OK, but "an agent" would still have to be supervised by someone. We don't want automated AI slop here.

Anyway: if someone opens three new self-contained issues for this, I'll be happy to close this one.

fingolfin avatar Nov 24 '25 17:11 fingolfin

(Maybe my last comment was a bit too implicit, so, to spell out what I meant: If a human guides the AI agent, I don't think it matters whether there is a separate issue or not. They can point at the specific paragraph where I explain the subissue, resp. copy-and-paste that. Conversely, an AI agent that only can do something if there is a hand-taylored issue made just for its sake would be a very poor tool indeed.)

fingolfin avatar Nov 24 '25 17:11 fingolfin

I dont want to turn this into a whole discussion about AI. But there are plenty of examples in the wild now that show AI agents can produce, independently, useful code. Provided you give a clear set of requirements, aka a dedicated issue.

langestefan avatar Nov 24 '25 18:11 langestefan