Insert a link into bold text in reStructuredText
Asked Answered
R

4

24

I try to insert a link into bold text in reStructuredText but failed.

This is my rst source:

**Lorem ipsum dolor sit amet, `consectetur <http://www.example.com>`_ 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua.**

but I got:

<strong>Lorem ipsum dolor sit amet, `consectetur <http://www.example.com>`_ 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.</strong>

I want to get this:

<strong>Lorem ipsum dolor sit amet, <a href="http://www.example.com">consectetur</a> 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.</strong>
Reichert answered 10/3, 2012 at 9:23 Comment(1)
This question was answered at #4744345Amandine
W
22

At the moment it seems that this is not possible: see the Docutils FAQ, specifically the question "Is nested inline markup possible". The answer basically states that nested inline markup is on the to do list, so it will eventually be possible to do what you ask in the question, and provides a couple of work arounds (which they state are not recommended).

Since the workarounds are not recommended perhaps for the time being you could just do something like:

**Lorem ipsum dolor sit amet,** `consectetur <http://www.example.com>`_ 
**adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua.**

Of course your link would not be bold, but at least it will be a valid link.

Williamswilliamsburg answered 10/3, 2012 at 10:27 Comment(0)
S
5

Not exactly the HTML you ask for, but it creates a link inside of a fully bold text:

.. _consectetur: http://www.example.com
.. |consectetur| replace:: **consectetur** 

**Lorem ipsum dolor sit amet,** |consectetur|_
**dolor elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.**

will produce:

<strong>Lorem ipsum dolor sit amet,</strong>
<a href="http://www.example.com"><strong>consectetur</strong></a>
<strong>dolor elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</strong>
Seko answered 13/8, 2020 at 11:32 Comment(1)
Whoa, thank you! For my purpose, this works beautifully!Wills
C
3

One further workaround would be to mark the whole paragraph as bold, if applicable.

.. class:: bold

Lorem ipsum dolor sit amet, `consectetur <http://www.example.com>`_ 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua.
Centaur answered 20/3, 2012 at 13:23 Comment(1)
does not seem to work: gist.github.com/carbolymer/e8e2f5351aa7c0bf8c2130db72cc5e3bNich
L
0

I've made a Shpinx extension xxlink.py which registers Docutils roles :stlink: and :emlink:.

reST

`example <https://example.org>`__
:stlink:`example <https://example.org>`
:emlink:`example <https://example.org>`

emited HTML

<p><a class="reference external" href="https://example.org">example</a>
<strong><a class="reference external" href="https://example.org">example</a></strong>
<em><a class="reference external" href="https://example.org">example</a></em></p>

result

example example example

Now I don't have to define 2 extra text substitutions for every single em/strong link.

Suggestions are welcome.

Lietuva answered 31/7, 2021 at 5:7 Comment(1)
The GitHub link does not work.Hebetate

© 2022 - 2025 — McMap. All rights reserved.