Search code examples
pythonpython-sphinxrestructuredtext

How to add anchor in list element


I have a list in my rst file that looks like this:

- Item 1
- Item 2
- Item 3

It renders something like the following (which is exactly what I want):

  • Item 1
  • Item 2
  • Item 3

I would like to create links for each item, so I did

.. _item-1:

- Item 1

.. _item-1:

- Item 2

.. _item-1:

- Item 3

Now my list renders something like this:

  • Item 1

  • Item 2

  • Item 3

This is clearly happening because of the anchors I inserted between the elements. Is there a way to insert referenc-able anchors inline in sphinx/rST?


Solution

  • This is pretty close to the desired result, with funky white space.

      .. _item-1:
    
    - Item 1
    
      .. _item-2:
    
    - Item 2
    
      .. _item-3:
    
    - Item 3
    

    This yields the following output. Note the id attribute is on the parent <ul> instead of the first <li>, but effectively resolves to the same location on the page.

    <ul class="simple" id="item-1">
    <li>Item 1</li>
    <li id="item-2">Item 2</li>
    <li id="item-3">Item 3</li>
    </ul>