-->

reST: inline links with arbitrary titles?

2020-07-18 19:54发布

问题:

If I've defined an internal hyperlink target, how can I link to it using arbitrary text as the link title?

For example:

_`This is a very long internal link target`.

...

A `short reference`_ to the very long link target.

How could I make the “short reference” target the “very long internal target”?

Edit: Another example:

Widgets
=======

...

Use a `widget`_.

How can I make “widget” (singular) link to the “Widgets” (plural) heading?

(of course, I realize that this could be done using `widget`_\s… But that doesn't solve the general problem)

回答1:

I haven't found a way to do this using "standard" reST. But with Sphinx, the following works:

.. _`This is a very long internal link target`:

:ref:`Short reference <This is a very long internal link target>` to the very long link target.


回答2:

Nicely, mzjn's answer also works for classes:

:class:`MyClasses <mymodule.MyClass>` in plural is awesome.

Hurray!



回答3:

A pure-reStructuredText method is to define multiple target names for the same target:

.. _my widget:
.. _above:
My long title about widgets
===========================
…
See how to use  `my widget`_ in the section above_.

(A limitation of this approach is that you may want to use the same text (e.g. "above") for two different locations, and the approach above fails in this case.)