Skip to content

Use anonymous references for links with embedded URLs - #13615

Merged
AA-Turner merged 2 commits into
sphinx-doc:masterfrom
timhoffm:doc-external-links
Jun 7, 2025
Merged

AA-Turner merged 2 commits into
sphinx-doc:masterfrom
timhoffm:doc-external-links

Conversation

@timhoffm

@timhoffm timhoffm commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

Closes #13613.

The explanation got a bit longer than I would have liked. But I think it's important to make people aware that they should use two underscoes even though one may also work. And if we do that, we need to briefly explain the difference.

@timhoffm
timhoffm force-pushed the doc-external-links branch from b0ff0f7 to bd1adac Compare June 4, 2025 22:01

@jfbu jfbu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits.

There is one more syntax but it would be confusing to state it here. It is `Link text <goto_>`__, with the .. _goto: standing after the paragraph. As I reported at #13576 (comment), with Docutils 0.21.2, even with a single underscore one could use this with identical Link text e.g. See `DOI <goto1_>`_ or `DOI <goto2_>`_ without Docutils raising warnings but this is no more true since Docutils r10151.

Comment thread doc/usage/restructuredtext/basics.rst Resolved
@AA-Turner AA-Turner changed the title DOC: Use two underscores for links with embedded URLs Use anonymous references for links with embedded URLs Jun 5, 2025
@AA-Turner

Copy link
Copy Markdown
Member

cc: @gmilde

@timhoffm
timhoffm force-pushed the doc-external-links branch from bd1adac to 02cc1df Compare June 5, 2025 20:49
Comment thread doc/usage/restructuredtext/basics.rst Resolved
@AA-Turner
AA-Turner merged commit 74627b2 into sphinx-doc:master Jun 7, 2025
@timhoffm
timhoffm deleted the doc-external-links branch June 7, 2025 17:50
@mgeier

mgeier commented Jun 11, 2025

Copy link
Copy Markdown
Contributor

Thanks @timhoffm, I wasn't aware of the single vs. double underscore issue.

For reference, and in case anybody is wondering (as I was), I tried it (with Sphinx master branch and docutils 0.21.2) and observed the following behavior:

When I use multiple embedded URLs with the same link name "link" and identical URLs, everything works as expected and there are no warnings.

When I use different URLs with the same name (and with a single underscore), it still works fine, and the links point to the correct URLs, but I'm getting these messages.

WARNING: Duplicate explicit target name: "link". [docutils]
ERROR: Duplicate target name, cannot be used as a unique reference: "link". [docutils]

Using two underscores made the messages go away.


And another thing: what about "anonymous hyperlinks" as described here: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#anonymous-hyperlinks?

I like using those but they don't seem to be mentioned on the Sphinx doc page (https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html).

Are they discouraged for some reason?

If not, I think it would be great to add them to the docs!

Comment on lines +219 to +221
This is a paragraph that contains `a link`_.

.. _a link: https://domain.invalid/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, the exact same example is repeated below the inserted text, now it appears two times on the page.

Is this intentional?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is rather a leftover from the last edit.
See my comment at #13613 (comment)

@AA-Turner

Copy link
Copy Markdown
Member

@mgeier happy to look at a PR that adds a mention of the __: <URI> syntax, I also use it.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 12, 2025
@AA-Turner AA-Turner added this to the 9.0.0 milestone Nov 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading documentation on rST hyperlink syntax.

5 participants