Skip to content
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

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

Merged
merged 2 commits into from
Apr 29, 2021
Merged

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

merged 2 commits into from
Apr 29, 2021

Conversation

prcr
Copy link
Contributor

@prcr prcr commented Apr 24, 2021

While deploying version 1.0.2 of the plugin that includes the changes from #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:

  • You're using the default configuration use_directory_urls: true
  • You have a redirect target index.md or README.md

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 pushed a commit to codacy/docs that referenced this pull request Apr 27, 2021
Use the fix from mkdocs/mkdocs-redirects#21
until it's merged upstream.
prcr pushed a commit to codacy/docs that referenced this pull request Apr 27, 2021
@prcr
Copy link
Contributor Author

prcr commented Apr 29, 2021

@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 burkestar merged commit 48e5231 into mkdocs:master Apr 29, 2021
@burkestar
Copy link
Collaborator

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
Copy link
Contributor Author

prcr commented Apr 29, 2021

Many thanks for such a quick reply!

Yes, those things would come in handy before considering introducing more features (for example, this one #16 (comment) 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. 😄 👍

@burkestar
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants