Fix auto links to subheader when TOC is present (#20279)

Fix links to subheader when TOC is present

It was observed (in https://github.com/nim-lang/Nim/pull/20112)
that links to 2nd- (and subsequent) -level headings
fail if TOC is present, e.g.:
```nim
.. contents::

Type relations
==============

Convertible relation
--------------------

Ref. `Convertible relation`_
```

The problem here is that links are resolved in `rst.nim` but later
`rstgen.nim` fixes ("fixes") anchors to make them unique so that
TOC always works (if e.g. there was another sub-section like
"Convertible relation").
The solution implemented in this PR is to move that fix-up of anchors
into `rst.nim`, so that link resolution could know final anchors.

The bug seems to be added in https://github.com/nim-lang/Nim/pull/2332
in 2015, that is it is present in Nim 1.0.
This commit is contained in:
Andrey Makarov 2022-08-28 00:28:26 +03:00 • committed by GitHub
commit de9cbf6af1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 165 additions and 104 deletions

View file

@ -536,6 +536,63 @@ Some chapter
let output1 = input1.toHtml
doAssert output1 == "GC_step"
test "RST anchors/links to headings":
# Currently in TOC mode anchors are modified (for making links from
# the TOC unique)
let inputNoToc = dedent"""
Type relations
==============
Convertible relation
--------------------
Ref. `Convertible relation`_
"""
let outputNoToc = inputNoToc.toHtml
check outputNoToc.count("id=\"type-relations\"") == 1
check outputNoToc.count("id=\"convertible-relation\"") == 1
check outputNoToc.count("href=\"#convertible-relation\"") == 1
let inputTocCases = @[
dedent"""
.. contents::
Type relations
==============
Convertible relation
--------------------
Ref. `Convertible relation`_
Guards and locks
================
""",
dedent"""
Ref. `Convertible relation`_
.. contents::
Type relations
==============
Convertible relation
--------------------
Guards and locks
================
"""
]
for inputToc in inputTocCases:
let outputToc = inputToc.toHtml
check outputToc.count("id=\"type-relations\"") == 1
check outputToc.count("id=\"type-relations-convertible-relation\"") == 1
check outputToc.count("id=\"convertible-relation\">") == 0
# Besides "Ref.", heading also contains link to itself:
check outputToc.count(
"href=\"#type-relations-convertible-relation\">") == 2
check outputToc.count("href=\"#convertible-relation\"") == 0
test "RST links":
let input1 = """
Want to learn about `my favorite programming language`_?