mkdocs / mkdocs-redirects

Open source plugin for Mkdocs page redirects
MIT License
176 stars 25 forks source link

fix: Avoid empty path for index.md redirect target #21

Closed prcr closed 3 years ago

prcr commented 3 years ago

While deploying version 1.0.2 of the plugin that includes the changes from https://github.com/datarobot/mkdocs-redirects/pull/19 I noticed that in some situations the build of the site would fail:

$ mkdocs build
INFO    -  Cleaning site directory 
INFO    -  Building documentation to directory: /home/prcr/git/docs/site 
Traceback (most recent call last):
  File "/home/prcr/.local/bin/mkdocs", line 11, in <module>
    sys.exit(cli())
  File "/home/prcr/.local/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/prcr/.local/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/prcr/.local/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/prcr/.local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/prcr/.local/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/prcr/.local/lib/python3.6/site-packages/mkdocs/__main__.py", line 152, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "/home/prcr/.local/lib/python3.6/site-packages/mkdocs/commands/build.py", line 295, in build
    config['plugins'].run_event('post_build', config=config)
  File "/home/prcr/.local/lib/python3.6/site-packages/mkdocs/plugins.py", line 96, in run_event
    result = method(**kwargs)
  File "/home/prcr/.local/lib/python3.6/site-packages/mkdocs_redirects/plugin.py", line 131, in on_post_build
    dest_path = get_relative_html_path(page_old, page_new, use_directory_urls)
  File "/home/prcr/.local/lib/python3.6/site-packages/mkdocs_redirects/plugin.py", line 60, in get_relative_html_path
    relative_path = os.path.relpath(new_path, start=os.path.dirname(old_path))
  File "/usr/lib/python3.6/posixpath.py", line 457, in relpath
    raise ValueError("no path specified")
ValueError: no path specified

I found out that this would happen if:

In this particular situation, after removing the index.html from the target path we would obtain an empty string and the call to os.path.relpath would subsequently fail:

https://github.com/datarobot/mkdocs-redirects/blob/b0d564bdc7f2364e4fec0cec6dcf4dc23ca8ea0f/mkdocs_redirects/plugin.py#L56-L60

My fix ensures that in this case the returned string is ./ so that os.path.relpath can correctly calculate the relative path.

prcr commented 3 years ago

@burkestar, perhaps you'll get a chance to review this fix soon? In the meantime, I updated the CHANGES.md and the README.md (as it was still mentioning absolute paths).

burkestar commented 3 years ago

Released to PyPi as 1.0.3

Some day I'll get around to updating this package with better standards (PR tests, drop python 2 support, and auto-releasing logic on merge).

prcr commented 3 years ago

Many thanks for such a quick reply!

Yes, those things would come in handy before considering introducing more features (for example, this one https://github.com/datarobot/mkdocs-redirects/issues/16#issue-663705242 would be helpful for me as well).

But as it is, the plugin is already incredibly useful - I'm maintaining a medium/large documentation site and the redirects are crucial to avoid having broken links on the product UI. This plugin allows me to manage the redirects in a simple and sane way. :smile: :+1:

burkestar commented 3 years ago

That's really cool to hear @prcr! I built this for an internal developer docs site awhile ago and was like "you know, I bet others would find this useful". The power of open source. Thanks for your contributions to making this better.