Skip to content
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

feat: Backlinks #65

Merged
merged 6 commits into from
Feb 23, 2025
Merged

feat: Backlinks #65

merged 6 commits into from
Feb 23, 2025

Conversation

pawamoy
Copy link
Member

@pawamoy pawamoy commented Feb 19, 2025

TODO:

@pawamoy pawamoy force-pushed the backlinks branch 2 times, most recently from dec902a to b566031 Compare February 23, 2025 14:26
pawamoy added a commit that referenced this pull request Feb 23, 2025
This feature only allows recording and retrieving backlinks, for other plugins or systems to render them. Backlinks are not rendered directly by autorefs (though we could consider offering such a feature in the future).

PR-65: #65
Issue-mkdocstrings-723: mkdocstrings/mkdocstrings#723
Issue-mkdocstrings-python-153: mkdocstrings/python#153
@pawamoy pawamoy marked this pull request as ready for review February 23, 2025 14:31
This change makes autorefs record heading titles alongside URLs, but doesn't actually change the rendering logic. This will be done in a later change that will rely on new title-related options.

Issue-33: #33
Previously, we used  the `on_post_page` hook to fix cross-references. This was problematic because it cornered us to the last per-page event, making it difficult to coordinate ourselves with other plugins such as mkdocstrings.

Fixing cross-references must be done once all pages have been rendered to HTML. The earliest we can do that is in `on_env`, which acts as a synchronization barrier here.

We coordinate the logic with mkdocstrings thanks to event priorities: autorefs' `on_env` must run after mkdocstrings has finished loading inventories, so that all cross-references can be fixed. Since mkdocstrings uses the default priority, 0, our -50 priority makes us run last.

Discussion-mkdocs-3917: mkdocs/mkdocs#3917
pawamoy added a commit that referenced this pull request Feb 23, 2025
This feature only allows recording and retrieving backlinks, for other plugins or systems to render them. Backlinks are not rendered directly by autorefs (though we could consider offering such a feature in the future).

PR-65: #65
Issue-mkdocstrings-723: mkdocstrings/mkdocstrings#723
Issue-mkdocstrings-python-153: mkdocstrings/python#153
pawamoy added a commit that referenced this pull request Feb 23, 2025
This feature only allows recording and retrieving backlinks, for other plugins or systems to render them. Backlinks are not rendered directly by autorefs (though we could consider offering such a feature in the future).

PR-65: #65
Issue-mkdocstrings-723: mkdocstrings/mkdocstrings#723
Issue-mkdocstrings-python-153: mkdocstrings/python#153
This change adds a `link_titles` option that defaults to `"auto"`. In automatic mode, link titles are either:

- always set if Material for MkDocs and its instant preview feature aren't detected
- only set on external links otherwise (since instant preview are not supported on external links anyway)

The option also accepts the `True` and `False`, for always/never setting titles, respectively.

An update to the title logic accompanies this change in order to make use of recorded heading titles (a change brought two commit ago):

- optional cross-references will use the original title, and optionally append the identifier if it doesn't already appear in the title
- mandatory cross-references will use either the original title if there's one, or no title at all

This is because optional cross-refs are almost exclusively created by mkdocstrings handlers, and therefore displaying the identifier (full qualified name of objects) is useful when hovering on a link. Manual cross-references on the other hand can often be references to text sections, and should never display the section anchor. The limitation being that manual cross-references to API objects won't show the identifier. We could consider using an additional attribute (other than `optional`) to label cross-refs as "API objects" or not, though users would still have to annotate their manual cross-refs with such an attribute to enjoy the appended identifier to the title.

Issue-33: #33
Issue-62: #62
The plugin records heading titles, and sets them as `title` attribute of cross-references (HTML links). Sometimes these titles contain HTML, and some MkDocs themes do not support HTML in them (they are shown as tooltips when hovering on links). This option allows to strip tags (while keeping text) from titles.

When set to `"auto"` (default value), it only strips tags for unsupported themes (all except Material for MkDocs).

Issue-33: #33
…ent_page` attribute

We will need the page instance for the backlinks feature, to build breadcrumbs with titles and URLs of the parents of each backlink.
This feature only allows recording and retrieving backlinks, for other plugins or systems to render them. Backlinks are not rendered directly by autorefs (though we could consider offering such a feature in the future).

PR-65: #65
Issue-mkdocstrings-723: mkdocstrings/mkdocstrings#723
Issue-mkdocstrings-python-153: mkdocstrings/python#153
@pawamoy pawamoy merged commit 5a3b387 into main Feb 23, 2025
51 checks passed
@pawamoy pawamoy deleted the backlinks branch February 23, 2025 16:14
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

Successfully merging this pull request may close these issues.

1 participant