Skip to content

Links starting with # are marked as "external" #11335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mgeier opened this issue Apr 17, 2023 · 2 comments
Open

Links starting with # are marked as "external" #11335

mgeier opened this issue Apr 17, 2023 · 2 comments

Comments

@mgeier
Copy link
Contributor

mgeier commented Apr 17, 2023

Describe the bug

This might be a docutils problem ...

When creating a same-page-link that starts with #, the link is formatted as "external", even though it points to the same page, which I would consider "internal", right?

How to Reproduce

index.rst:

Title
=====

`link to subsection <#a-subsection>`_


A Subsection
------------

`link to top <#>`_

conf.py:

# empty

Run:

python -m sphinx . _build

After that, _build/index.html contains this:

<p><a class="reference external" href="#a-subsection">link to subsection</a></p>
...
<p><a class="reference external" href="#">link to top</a></p>

Environment Information

Platform:              linux; (Linux-6.1.0-6-amd64-x86_64-with-glibc2.36)
Python version:        3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0])
Python implementation: CPython
Sphinx version:        6.2.0+/0fb6716f3
Docutils version:      0.19.1b.dev
Jinja2 version:        3.1.2
Pygments version:      2.14.0

Sphinx extensions

No response

Additional context

spatialaudio/nbsphinx#530

@picnixz
Copy link
Member

picnixz commented Apr 18, 2023

I edited my comment because I was wrong before.


This should be because of:

def visit_reference(self, node: Element) -> None:
atts = {'class': 'reference'}
if node.get('internal') or 'refuri' not in node:
atts['class'] += ' internal'
else:
atts['class'] += ' external'

An "internal" reference would point to an explicit (hard-coded) label (see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-targets). Since there is no explicit label being created, it does not work. Now, by looking at the Sphinx implementation, I think we should mark internal=True when a reference has an explicit target starting with #.

@AA-Turner AA-Turner added this to the some future version milestone Apr 29, 2023
@gmilde
Copy link

gmilde commented Jun 3, 2025

An "internal" reference would point to an explicit (hard-coded) label (see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-targets).

An "internal reference" uses a refname instead of a refuri. The "reference name" may be established by an explicit target or an implicit target (like a section title).

Since there is no explicit label being created, it does not work.

Actually,

`link to subsection <a subsection_>`__


A Subsection
------------

works fine and creates an internal reference.

Now, by looking at the Sphinx implementation, I think we should mark internal=True when a reference has an explicit target starting with #.

This may be an enhancement for the Docutils HTML5 writer. It is a backwards incompatible change, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants