Skip to content

Commit

Permalink
👌 Improve rendering of implicit link text
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Mar 1, 2023
1 parent f791cea commit de00c89
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ def render_inventory_link(self, token: SyntaxTreeNode) -> None:
elif match.text:
ref_node.append(nodes.Text(match.text))
else:
ref_node.append(nodes.Text(match.name))
ref_node.append(nodes.literal(match.name, match.name))

def get_inventory_matches(
self,
Expand Down
12 changes: 7 additions & 5 deletions myst_parser/mdit_to_docutils/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,31 @@ def render_internal_link(self, token: SyntaxTreeNode) -> None:
refdomain="doc", reftarget=docname, reftargetid=path_id, **kwargs
)
classes = ["xref", "myst"]
text = ""
else:
wrap_node = addnodes.download_reference(
refdomain=None, reftarget=path_dest, **kwargs
)
classes = ["xref", "download", "myst"]
text = destination if not token.children else ""
else:
wrap_node = addnodes.pending_xref(
refdomain=None, reftarget=destination, **kwargs
)
classes = ["xref", "myst"]
text = ""

self.add_line_and_source_path(wrap_node, token)
self.copy_attributes(token, wrap_node, ("class", "id", "title"))
self.current_node.append(wrap_node)

inner_node = nodes.inline("", text, classes=classes)
wrap_node.append(inner_node)
if explicit:
inner_node = nodes.inline("", "", classes=classes)
with self.current_node_context(inner_node):
self.render_children(token)
elif isinstance(wrap_node, addnodes.download_reference):
inner_node = nodes.literal(path_dest, path_dest, classes=classes)
else:
inner_node = nodes.inline("", "", classes=classes)

wrap_node.append(inner_node)

def get_inventory_matches(
self,
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/sphinx_ext/myst_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def _resolve_myst_ref_intersphinx(
)
else:
newnode.append(
contnode.__class__(match.name, match.name, classes=["iref", "myst"])
nodes.literal(match.name, match.name, classes=["iref", "myst"])
)

return newnode
5 changes: 3 additions & 2 deletions tests/test_renderers/fixtures/sphinx_link_resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ c | d
<document source="<src>/index.md">
<paragraph>
<download_reference filename="dd18bf3a8e0a2a3e53e2661c7fb53534/test.txt" refdoc="index" refdomain="True" refexplicit="False" reftarget="test.txt" reftype="myst">
<inline classes="xref download myst">
<literal classes="xref download myst">
test.txt

<download_reference filename="dd18bf3a8e0a2a3e53e2661c7fb53534/test.txt" refdoc="index" refdomain="True" refexplicit="False" reftarget="./test.txt" reftype="myst">
<inline classes="xref download myst">
<literal classes="xref download myst">
./test.txt

<download_reference filename="dd18bf3a8e0a2a3e53e2661c7fb53534/test.txt" refdoc="index" refdomain="True" refexplicit="True" reftarget="/test.txt" reftype="myst">
<inline classes="xref download myst">
Expand Down
24 changes: 15 additions & 9 deletions tests/test_sphinx/test_sphinx_builds/test_references.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ <h1>
</p>
<p>
<a class="reference download internal" download="" href="_downloads/1952147a8403903cb78cecf56f049085/file_link2.txt">
<span class="xref download myst">
subfolder/file_link2.txt
</span>
<code class="xref download myst docutils literal notranslate">
<span class="pre">
subfolder/file_link2.txt
</span>
</code>
</a>
</p>
<p id="insidecodeblock">
Expand Down Expand Up @@ -225,9 +227,11 @@ <h1>
<p>
Known no title
<a class="reference external" href="https://example.com/index.html#paragraph-target" title="Example 0.0.1">
<span class="iref myst">
paragraph-target
</span>
<code class="iref myst docutils literal notranslate">
<span class="pre">
paragraph-target
</span>
</code>
</a>
</p>
<p>
Expand All @@ -251,9 +255,11 @@ <h1>
<p>
Ambiguous
<a class="reference external" href="https://example.com/index.html#module-duplicate" title="Example 0.0.1">
<span class="iref myst">
duplicate
</span>
<code class="iref myst docutils literal notranslate">
<span class="pre">
duplicate
</span>
</code>
</a>
</p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
link
<paragraph>
<download_reference filename="1952147a8403903cb78cecf56f049085/file_link2.txt" refdoc="index" refdomain="True" refexplicit="False" reftarget="subfolder/file_link2.txt" reftype="myst">
<inline classes="xref download myst">
<literal classes="xref download myst">
subfolder/file_link2.txt
<target refid="insidecodeblock">
<paragraph ids="insidecodeblock" names="insidecodeblock">
Expand Down Expand Up @@ -132,7 +132,7 @@
<paragraph>
Known no title
<reference internal="False" reftitle="Example 0.0.1" refuri="https://example.com/index.html#paragraph-target">
<inline classes="iref myst">
<literal classes="iref myst">
paragraph-target
<paragraph>
Known explicit
Expand All @@ -148,5 +148,5 @@
<paragraph>
Ambiguous
<reference internal="False" reftitle="Example 0.0.1" refuri="https://example.com/index.html#module-duplicate">
<inline classes="iref myst">
<literal classes="iref myst">
duplicate
2 changes: 1 addition & 1 deletion tests/test_sphinx/test_sphinx_builds/test_references.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
link
<paragraph>
<download_reference filename="1952147a8403903cb78cecf56f049085/file_link2.txt" refdoc="index" refdomain="True" refexplicit="False" reftarget="subfolder/file_link2.txt" reftype="myst">
<inline classes="xref download myst">
<literal classes="xref download myst">
subfolder/file_link2.txt
<target refid="insidecodeblock">
<paragraph ids="insidecodeblock" names="insidecodeblock">
Expand Down

0 comments on commit de00c89

Please sign in to comment.