spatialaudio / nbsphinx

:ledger: Sphinx source parser for Jupyter notebooks
https://nbsphinx.readthedocs.io/
MIT License
449 stars 128 forks source link

Non working internal links for manually added anchors in 0.9.2 #748

Open jenshnielsen opened 1 year ago

jenshnielsen commented 1 year ago

In 0.9.2 I am seeing a warning about undefined labels for internal links starting with # when building the html version of my docs and the links are not working.

A minimal notebook that triggers this behavior is something like the following

# LinkExample

## Table of Contents <a class="anchor" id="toc"></a>

 * [LinkInSameCell](#link)

## Section <a class="anchor" id="link"></a>
([back to ToC](#toc))

This triggers warnings like the ones below and does not generate working links.

...LinkExample.ipynb:14: WARNING: undefined label: '/examples/linkexample.ipynb#link'
...LinkExample.ipynb:19: WARNING: undefined label: '/examples/linkexample.ipynb#toc'

This used to work with 0.9.1 and I have bisected this to break with #725

It seems that if I remove the manual anchors from my notebook and use the automatically generated ones this works fine. However due to the way my notebook is structured (with an introduction to each term and a longer section) these are not always unique causing the link to be to the wrong location

mgeier commented 1 year ago

This is just a warning, so you are getting the correct HTML output, right?

If you want to silence the warning, I think you can put that into your conf.py:

suppress_warnings = ['ref.ref']

The reason for this warning is that Sphinx is not aware of those manual anchors. I think it would be possible to parse those and make them known to Sphinx, but I don't think it is trivial, and I'm not planning to do that. But if someone wants to implement this, I'm open for PRs!

BTW, if possible, I would just try to change the section titles slightly to avoid ambiguities.

jenshnielsen commented 1 year ago

No it’s not just a warning. With 0.9.1 this produces working links with this does not produce working links

mgeier commented 1 year ago

No it’s not just a warning.

Sorry, I haven't read your original report thoroughly enough, I have now seen that you have mentioned that already.

I have tried it with nbsphinx 0.9.1, and it doesn't work either!

This may be related to the pandoc version, though. I have 2.17.1.1 and it seems to strip the anchors from the headers. And even if the links are created as expected, it doesn't help if the anchors are gone.

Your header looks like this in the generated HTML:

<h2>Table of Contents<a class="headerlink" href="#Table-of-Contents" title="Permalink to this heading">#</a></h2>

If your pandoc version doesn't do that, you can try this work-around:

[LinkInSameCell](linkexample.html#link)

Note the .html! I normally wouldn't recommend using that, but in your case it might work.

jenshnielsen commented 1 year ago

@mgeier Thanks I will try that when I have a sec (or it may simply be a better option to rework my notebooks to avoid duplicated headings)