MyST-Parser icon indicating copy to clipboard operation
MyST-Parser copied to clipboard

Support docutils "reference names" including hyperlinks to them

Open infinity0 opened this issue 1 year ago • 3 comments

Context

With regular rst docutils, you can do something like this:

Section heading
============

`Section heading`_

or:

.. image:: bild.png
  :name: my picture

`my picture`_

or:

.. math::
  :name: my formula
  e^{i\pi} + 1 = 0

`my formula`_

Internally, this is implemented by associating reference names to elements. Note that this is different from an "identifier key". From the previous link:

Reference names identify elements for cross-referencing. [..] [an] auto-generated identifier key [..] may differ from the reference name due to restrictions on identifiers/labels in the output format.

Furthermore in https://docutils.sourceforge.io/docs/ref/doctree.html#identifier-keys:

Identifier keys cannot be specified directly in reStructuredText. Docutils generates them by applying the identifier normalization to reference names or from the auto_id_prefix, prepending the id_prefix and potentially appending numbers for disambiguation.

In other words, reference names are more "user friendly" than identifier keys, since they are chosen directly by the user, whereas identifier keys are generated by docutils and semi-opaque to the user.

Despite being built on top of docutils, MyST does not appear to support reference names at the moment, but only identifier keys, with these keys either being generated manually via a special syntax, or automatically via a non-default option.

Proposal

This should "just work":

# section heading

[](section heading)

or:

```{math}
---
name: my formula
---
e^{i\pi} + 1 = 0
```

[](my formula)

In fact in this second case, myst-docutils-html already generates an identifier key #my-formula corresponding to the reference name "my formula" - indicating how core this concept is to docutils. It is just weird that MyST doesn't take advantage of it!

Tasks and updates

  • [ ] It shouldn't be that hard, just use docutils to resolve the reference name and generate a hyperlink.

infinity0 avatar Oct 27 '22 00:10 infinity0

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

welcome[bot] avatar Oct 27 '22 00:10 welcome[bot]

Hey @infinity0, as per https://spec.commonmark.org/0.30/#example-488, I think you just need to wrap these in <> like [](<my formula>)

For

# section heading

[](section heading)

It's not advised to use "implicit" references, which are brittle to changes in the heading text, and rather use a specific identifier:

(section-ref)=
# section heading

[](section-ref)

chrisjsewell avatar Feb 10 '23 01:02 chrisjsewell

It may be necessary to add angle brackets to my original examples, for them to be syntactically valid commonmark, as per your link. However that is a separate issue. The underlying functionality still currently doesn't work, even if you add angle brackets like you said.

infinity0 avatar Aug 12 '23 06:08 infinity0