-
Notifications
You must be signed in to change notification settings - Fork 32
Feature request: Heading-based redirects. #69
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
Comments
Thanks for the request @rudolfbyker. That would be a nice addition, similar to #64. I worry that users will expect such redirections to work in existing pages. But the plugin would have to modify the existing page to add the relevant The flat structure would mean the plugin must now iterate on all redirections in a first step, to reconcile page redirects and page+heading redirects. |
Sounds more complicated to implement than I anticipated. |
Sorry, not sure what I was thinking, but surely the plugin wouldn't have to add any heading, just JS code, to redirect depending on the anchor in the URL. So, a bit less complicated :) |
I took a look at implementing this, roughly, and here are a couple thoughts blocking me. I'm not sure the best way to proceed. I am going to use the term "anchor" to refer to the part of the URL beginning with a
Related: squidfunk/mkdocs-material#3764 |
For example: plugins:
- redirects:
redirect_maps:
"old.md#heading1": page1.md
"old.md#heading2": page2.md
"old.md": page3.md --> use page3 plugins:
- redirects:
redirect_maps:
"old.md#heading1": page1.md
"old.md#heading2": page2.md --> use page1 plugins:
- redirects:
redirect_maps:
"existing.md#heading1": page1.md
"existing.md#heading2": page2.md --> no link tag |
Thanks for your thoughts! I think that the first and third cases are sensible. The second case ("use page1") would make the plugin opinionated. This could be dealt with via some interface and options for how to select which page would serve as the canonical redirect, but that might complicate the user experience and the code unnecessarily. I'm interested to give this a try again, maybe later this week or next I'll give it a go. Brainstorming options that spring to mind for case 2 (I'm ignoring how to implement selection, for now):
|
An interesting feature of "first" or "last" is that it allows manual selection thanks to ordering 🙂 |
plugins:
- redirects:
redirect_maps:
"original.md#heading-1": "page1.md" # This heading was moved to a new page.
"original.md#heading-2": "page2.md#new-heading-2" # This heading was moved to an existing page.
# "original.md" continues to exist. In the var anchor=window.location.hash.substr(1);
switch (anchor) {
case 'heading-1':
location.href="page1.html"
break;
case 'heading-2':
location.href="page2.html#new-heading-2"
break;
default:
// Stay here.
} Thanks for considering this :) |
Definitely true! I like to keep them sorted alphabetically in our documentation to make lookup easier, which could be more challenging to cover with only "first" and "last". |
I've attempted an implementation today and ran into a snag. The original plugin assumes the source page will no longer exist. This need not be the case in a world where we can redirect anchors to other anchors. The HTML template for the redirect page does not make sense to use explicitly in the case where the source page continues to exist. We want to be able to reach a visibly-unaltered version of the source page, with only some anchors redirecting. My first thought is to modify the HTML page in the
|
@wwarriner, nice, thank you for taking a stab at this!
I think
I generally do not recommend using BeautifulSoup in MkDocs plugins, as parsing and re-dumping HTML is pretty slow. If a naive solution like appending the script to the page's content works well, I'd suggest doing that.
Yes. I don't see any practical fallback for clients with Javascript disabled. Such users are used to notice when a script is blocked and will likely know to try and enable it to get to the page/section they wanted to go to. |
Anything I can do to help move this along? I really need this feature! |
I use a custom scraper that runs in my CI to detect when I break any URLs that might exist in search engines, or that have been shared by users. Then I use
mkdocs-redirects
to fix those links before deploying the next version of my docs. This works well, until I have to split a page into two separate pages.Since we're doing client-side redirects anyway, could we support heading-based redirects? Heading-based redirects are not possible in the context of SSR, because the anchor (the
#foo
part of the URL) is not sent to the server. But the browser knows what it is, so with a little bit of JS, we can easily detect which heading is desired, and redirect based on that.Currently, the following script is generated:
This could easily be expanded with a simple
switch
statement:The
mkdocs.yaml
config could look something like this:or, if you don't like the flat structure:
or, if you want to keep the door open for other types of redirects (which I can't conceive of now):
The text was updated successfully, but these errors were encountered: