jupyter / nbconvert

Jupyter Notebook Conversion
https://nbconvert.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.72k stars 564 forks source link

Section links when converting Markdown to RST #1627

Open HealthyPear opened 3 years ago

HealthyPear commented 3 years ago

Not sure if is it a bug or something I didn't understand.

I have a Jupyter notebook in which I made a markdown table of contents like the following,

## Table of contents
- [A](#A)
    - [A1](#A1)
- [B](#B)
- [C](#C)
    - [C1](#C1)
    - [C2](#C2)

and then I call each section like so,

## A
[back to top](#Table-of-contents)

If I use the notebook as it is it works well, but I have the need to convert it to RST in my sphinx-based documentation (using directly HTML seemed more difficult and I didn't want to add to my repo the executed notebooks anymore).

So I installed pandoc and then did something like jupyter nbconvert --to rst Untitled.ipynb, so I have my Untitled.rst which get reproduced in my docs.

Problem is, all the links do not work anymore.

In the RST file I see the links as, e.g.

-  `A <#A>`__

and in the html page,

<li><p><a class="reference external" href="#A">A</a></p>

I am not an HTML expert, but at least the RST link seems to me a web link and not a section (or :ref) link. Am I right? Or am I missing something?

HealthyPear commented 3 years ago

I think I found the problem and it seems a bug:

all the section links are generated in HTML like this,

<h3>A1<a class="headerlink" href="#a1" title="Permalink to this headline">¶</a></h3>

but the correspondent one in the table of contents is,

<li><p><a class="reference external" href="#A1">A1</a></p></li>

which is wrong. In fact if I edit it like

<li><p><a class="reference external" href="#a1">A1</a></p></li>

it works

same with the table of contents, the section link is

<p><a class="reference external" href="#Table-of-contents">back to top</a></p>

while the actual header is

<h2>Table of contents<a class="headerlink" href="#table-of-contents" title="Permalink to this headline">¶</a></h2>

so it seems that the conversion puts all the header in lower-case, killing the links

I would open a proper "bug" issue, since I cannot change the labeling here, but I'll wait a bit to see if anyone has something else to say,

HealthyPear commented 3 years ago

I found this was already addressed on pandoc's side

https://github.com/jgm/pandoc/pull/3616

HealthyPear commented 3 years ago

by the way if the RST output is correct as it is, it could be also a problem in using sphinx

MSeal commented 3 years ago

That does look to be a bug someone needs to fix in the templating. I'm not sure if something changed in sphinx to make it more strict or if we had issues with the output for some time.