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

Anchors generated for relative links doesn't exist #30

Closed
xLaszlo opened this issue Nov 12, 2020 · 6 comments
Closed

Anchors generated for relative links doesn't exist #30

xLaszlo opened this issue Nov 12, 2020 · 6 comments
Labels
bug Something isn't working fix_available

Comments

@xLaszlo
Copy link

xLaszlo commented Nov 12, 2020

Relative links are turned into anchors according to their relative paths, but in the print-page class, the section's id which acts as its anchor is generated according to the page's absolute path. This results in these links not working.

We patched this at the end of url.py by generating all relative anchors (this assumes all of them are unique across the entire site and the paths do not contain --s:

    # Finally, insert new anchor for each page
    page_key_parts = page_key.split('-')
    for k in range(len(page_key_parts)):
        partial_page_key = '-'.join(page_key_parts[k:])
        html = f'<section class="print-page" id="{partial_page_key}">{html}</section>'
    return html
@timvink timvink added the bug Something isn't working label Nov 12, 2020
@timvink
Copy link
Owner

timvink commented Nov 12, 2020

Thanks for reporting and supplying a fix!

I will investigate and apply changes, for the next release.

@timvink
Copy link
Owner

timvink commented Dec 18, 2020

Thanks again for pinpointing this one.

I fixed it properly in 4b2defb by changing

url_from_root = os.path.normpath(os.path.join('/',url))

into

url_from_root = os.path.normpath(os.path.join(page_url,url))

And I made sure to add a unit test for this case. Will release the fix soon!

@timvink
Copy link
Owner

timvink commented Dec 21, 2020

Should be addressed in v1.0. Let me know if it's fixed for you.

@EricDuminil
Copy link

When MkDocs use_directory_urls is set to false, the above fix doesn't repair the relative links.

As far as I can tell, this line:

url_from_root = os.path.normpath(os.path.join(os.path.dirname(page_url), url))

with the added dirname, works for both cases (e.g. relative link from example.com/a/b/ or example.com/a/b.html).

It breaks one existing test case, though:

assert fix_href_links(html, "this_page", "/Chapter1/Section2") == result

But in MkDocs, "/Chapter1/Section2" would actually be "/Chapter1/Section2/", and the test would pass again.

I'm not sure if it should be a separate issue. But at least it's related to the above problem.

@timvink
Copy link
Owner

timvink commented Feb 22, 2021

Thanks @EricDuminil for both the report and guidance for the solution! Released in v1.1 https://github.com/timvink/mkdocs-print-site-plugin/releases. Can you check again and let me know if it's solved now?

@EricDuminil
Copy link

@timvink Excellent, it works with mkdocs-print-site-plugin==1.1.0 now. As the other two issues I wrote. Thank you very much!

@timvink timvink closed this as completed Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix_available
Projects
None yet
Development

No branches or pull requests

3 participants