oprypin / mkdocs-gen-files

MkDocs plugin to programmatically generate documentation pages during the build
https://oprypin.github.io/mkdocs-gen-files
MIT License
107 stars 10 forks source link

Client side redirects from anchors #12

Closed StarfallProjects closed 2 years ago

StarfallProjects commented 2 years ago

Hi! I am trying to use this plugin for client side redirects with '#' in. I've got the basics working:

import mkdocs_gen_files

redirect_map = {
    "reference/about.md": "../glossary/#test-1",
}

redirect_template = """
<script type="text/javascript">
    window.location.href = "{link}";
</script>
<a href="{link}">Redirecting...</a>
"""

for page, link in redirect_map.items():
    with mkdocs_gen_files.open(page, "w") as fd:
        print(redirect_template.format(link=link), file=fd)

However, it doesn't work if the redirect is from an anchor tag:

redirect_map = {
   "reference/glossary/#test-1": "../about/" ,
}

It generates a page: glossary/#test-1 (I guess as intended) Is there a way to handle # in the original link?

Thanks for any help!

StarfallProjects commented 2 years ago

I am also finding that this:

redirect_map = {
     "reference/glossary.md": "../about/",
}

Seems to automatically go to the last anchor on the page (whatever it is)

oprypin commented 2 years ago

Redirecting is not envisioned as a use case for mkdocs-gen-files. I was surprised to see it used as such and assumed to be working well.

I believe you got this example from https://github.com/mkdocstrings/mkdocstrings/blob/e19071b5cab120721ac898da8a87ca97dff23aa3/docs/gen_redirects.py -I don't think this example is advertised anywhere.

In your latter example, this value "reference/glossary/#test-1" is directly passed to open(page, 'w'), so of course it will create a file named as such. I don't think within mkdocs-gen-files it is reasonable to pursue anything that would satisfy this use case.

For more proper redirects I would recommend this plugin https://github.com/datarobot/mkdocs-redirects It doesn't work there either but at least this feature request could be reasonable there.

The reply you got here is also very insightful: https://github.com/squidfunk/mkdocs-material/discussions/3764#discussioncomment-2429473

StarfallProjects commented 2 years ago

Ironically, I got here from that other plugin, due to the recommendation on this issue: https://github.com/datarobot/mkdocs-redirects/issues/16

Hopefully you take over the plugin and add # support. In the mean time . . . well, thanks for answering!