Myst官方:Cross-references

Cross-references

MyST-Parser offers powerful cross-referencing features, to link to URLs, documents, headers, figures and more, that are portable across output formats and generate warnings when broken.

This page covers the basics of setting up referenceable targets for content and how to reference them.

Creating explicit targets

Targets are used to define custom anchors that you can refer to elsewhere in your documentation.

There are three primary ways to create targets:

  1. Annotating a syntax block with (target)=

  2. Annotating a syntax block/inline/span with an {#id} attribute (using the attrs_block and attrs_inline extensions)

  3. Adding a name option to a directive

There are also other ways to create targets, specific to certain directives, such as glossaries create targets for terms, and code APIs create targets for objects:

参见

The footnotes section, covers how to create and link to footnotes, and the sphinxcontrib.bibtex extension provides a means to reference bibliographies.

Implicit targets

Whole documents can be referenced by path. Headings within documents can also be assigned an implicit target, by setting the myst_heading_anchors configuration option. This is should be set to an integer, between 1 and 6, indicating the depth of headings to assign targets to.

The anchor “slugs” are created according to the GitHub implementation: heading titles are lower cased, punctuation is removed, spaces are replaced with -, and uniqueness is enforced by suffix enumeration.

For example, using myst_heading_anchors = 2:

For more information see the auto-generated header anchors section.

警告

In general, it is discouraged to rely on implicit targets, since they are easy to break, if for example a document/heading is moved or renamed.

Reference roles

Sphinx offers numerous roles for referencing specific objects.

These can also be used within MyST documents, although it is recommended to use the Markdown syntax where possible, which is more portable and native to MyST.

Handling invalid references

When building your documentation, it is recommended to run in , which will emit warnings for any invalid references.

you may encounter warnings such as:

intro.md:1: WARNING: 'myst' cross-reference target not found: 'reference' [myst.xref_missing]

intro.md:2: WARNING: Multiple matches found for 'duplicate': inter:py:module:duplicate, inter:std:label:duplicate [myst.iref_ambiguous]

To fully suppress a specific warning type, you can use the suppress_warnings configuration option, in Sphinx’s conf.py file:

suppress_warnings = ["myst.xref_missing", "myst.iref_ambiguous"]

or in docutils.conf or command-line tool:

[general]
myst-suppress-warnings = myst.xref_missing, myst.iref_ambiguous

In Sphinx specific reference warnings can also be suppressed, using the and configuration options.

nitpick_ignore = [("myst", "reference")]

To handle ambiguous references, for intersphinx references see the Cross-project (intersphinx) links section, or the domains searched for all Markdown references can be restricted globally or per-document using the myst_ref_domains configuration.

myst_ref_domains = ["std", "py"]