- I use sphinx w/ MyST-Parser for markdown, and
- I want GitHub or GitLab-style auto linking (linkfying) for references.
Is there a way to have MyST render the reference:
#346
In docutils-speak, this is a Text
node (example)
And behave as if it was:
[#346](https://github.com/vcs-python/libvcs/pull/346)
So when rendered it'd be like:
Not the custom role:
{issue}`1` <- Not this
Another example: Linkifying the reference @user
to a GitHub, GitLab, StackOverflow user.
What I'm currently doing (and why it doesn't work)
Right now I'm using the canonical solution docutils offers: custom roles.
I use sphinx-issues (PyPI), and does just that. It uses a sphinx setting variable, issues_github_path
to parse the URL:
e.g. in Sphinx configuration conf.py
:
issues_github_path = 'vcs-python/libvcs'
reStructuredText:
:issue:`346`
MyST-Parser:
{issue}`346`
Why custom roles don't work
Sadly, those aren't bi-directional with GitHub/GitLab/tools. If you copy/paste MyST-Parser -> GitHub/GitLab or preview it directly, it looks very bad:
Example of
CHANGES
:Example issue: https://github.com/vcs-python/libvcs/issues/363
What we want is to just be able to copy markdown including #347
to and from.
Does a solution already exist?
Are there any projects out there of docutils or sphinx plugins to turn @username
or #issues
into links?
sphinx (at least) can demonstrable do so for custom roles - as seen in sphinx-issues usage of
issues_github_path
- by using project configuration context.MyST-Parser has a linkify extension which uses linkify-it-py
This can turn
https://www.google.com
into https://www.google.com and not need to use<https://www.google.com>
.
Therefore, there may already be a tool out there.
Can it be done through the API?
The toolchain for myst, sphinx and docutils is robust. This is a special case.
This needs to be done at the Text
node level. Custom role won't work - as stated above - since it'll create markdown that can't be copied between GitLab and GitHub issues trivially.
The stack:
MyST-Parser API (Markdown-it-py API) > Sphinx APIs (MySTParser + Sphinx) > Docutils API
At the time of writing, I'm using Sphinx 4.3.2, MyST-Parser 0.17.2, and docutils 0.17.1 on python 3.10.2.
Notes
- For the sake of an example, I'm using an open source project of mine that is facing this issue.
- This is only about autolinking issues or usernames - things that'd easily be mappable to URLs. autodoc code-linking is out of scope.
There is a (defunct) project that does this: sphinxcontrib-issuetracker.
I've rebooted it:
conf.py:
_ext/link_issues.py:
Mirrors
Mypy users
mypy --strict docs/_ext/link_issues.py
work as of mypy 0.971If you use mypy:
pip install types-docutils types-requests
Install:
Example
via unihan-etl#261 / v0.17.2 (source, view, but page may be outdated)