Skip to content

Commit 2902e07

Browse files
authored
🐛 FIX encoded URL ID references (#715)
For example `<#name with spaces>` will be encoded as `#name%20with%20spaces`, so we need to decode before resolving the reference.
1 parent 4cac732 commit 2902e07

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

myst_parser/mdit_to_docutils/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ def render_id_link(self, token: SyntaxTreeNode) -> None:
969969
ref_node = nodes.reference()
970970
self.add_line_and_source_path(ref_node, token)
971971
ref_node["id_link"] = True
972-
ref_node["refuri"] = token.attrGet("href") or ""
972+
ref_node["refuri"] = self.md.normalizeLinkText(str(token.attrGet("href") or ""))
973973
self.copy_attributes(
974974
token, ref_node, ("class", "id", "reftitle"), aliases={"title": "reftitle"}
975975
)

myst_parser/mdit_to_docutils/transforms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from docutils import nodes
77
from docutils.transforms import Transform
8+
from markdown_it.common.normalize_url import normalizeLink
89

910
from myst_parser._compat import findall
1011
from myst_parser.mdit_to_docutils.base import clean_astext
@@ -138,7 +139,7 @@ def apply(self, **kwargs: t.Any) -> None:
138139
line=refnode.line,
139140
append_to=refnode,
140141
)
141-
refnode["refid"] = target
142+
refnode["refid"] = normalizeLink(target)
142143
if not refnode.children:
143144
refnode += nodes.inline(
144145
"#" + target, "#" + target, classes=["std", "std-ref"]

tests/test_renderers/fixtures/docutil_link_resolution.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
https://www.google.com
1616
.
1717

18-
[missing]
18+
[missing]
1919
.
2020
[](#test)
2121
[...](#test)
2222
[explicit](#test)
23+
[](<#name with spaces>)
2324
.
2425
<document source="<src>/index.md">
2526
<paragraph>
@@ -39,10 +40,16 @@
3940
<paragraph>
4041
'myst' reference target not found: 'test' [myst.xref_missing]
4142

43+
<reference id_link="True" refid="name%20with%20spaces">
44+
<system_message level="2" line="1" source="<src>/index.md" type="WARNING">
45+
<paragraph>
46+
'myst' reference target not found: 'name with spaces' [myst.xref_missing]
47+
4248

4349
<src>/index.md:1: (WARNING/2) 'myst' reference target not found: 'test' [myst.xref_missing]
4450
<src>/index.md:1: (WARNING/2) 'myst' reference target not found: 'test' [myst.xref_missing]
4551
<src>/index.md:1: (WARNING/2) 'myst' reference target not found: 'test' [myst.xref_missing]
52+
<src>/index.md:1: (WARNING/2) 'myst' reference target not found: 'name with spaces' [myst.xref_missing]
4653
.
4754

4855
[implicit_anchor] --myst-heading-anchors=1
@@ -131,6 +138,23 @@
131138
Other
132139
.
133140

141+
[id-with-spaces]
142+
.
143+
(name with spaces)=
144+
Paragraph
145+
146+
[](<#name with spaces>)
147+
.
148+
<document source="<src>/index.md">
149+
<target refid="name-with-spaces">
150+
<paragraph ids="name-with-spaces" names="name\ with\ spaces">
151+
Paragraph
152+
<paragraph>
153+
<reference id_link="True" refid="name-with-spaces">
154+
<inline classes="std std-ref">
155+
#name with spaces
156+
.
157+
134158
[ref-table]
135159
.
136160
```{table} caption

tests/test_renderers/fixtures/sphinx_link_resolution.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
[](#target)
7979
[...](#target)
8080
[explicit](#target)
81+
[](<#name with spaces>)
8182
.
8283
<document source="<src>/index.md">
8384
<target refid="target">
@@ -95,6 +96,9 @@
9596

9697
<reference id_link="True" refid="target">
9798
explicit
99+
100+
<pending_xref refdoc="index" refdomain="True" refexplicit="False" reftarget="name with spaces" reftype="myst" refwarn="True">
101+
<inline classes="xref myst">
98102
.
99103

100104
[explicit>implicit] {"myst_heading_anchors": 1}
@@ -120,6 +124,23 @@
120124
Other
121125
.
122126

127+
[id-with-spaces]
128+
.
129+
(name with spaces)=
130+
Paragraph
131+
132+
[](<#name with spaces>)
133+
.
134+
<document source="<src>/index.md">
135+
<target refid="name-with-spaces">
136+
<paragraph ids="name-with-spaces" names="name\ with\ spaces">
137+
Paragraph
138+
<paragraph>
139+
<reference id_link="True" refid="name-with-spaces">
140+
<inline classes="std std-ref">
141+
#name with spaces
142+
.
143+
123144
[ref-table]
124145
.
125146
```{table} caption
@@ -189,7 +210,7 @@ c | d
189210
relative to source dir
190211
.
191212

192-
[source-file]
213+
[source-file]
193214
.
194215
[](other.rst)
195216
[...](./other.rst)

0 commit comments

Comments
 (0)