Search code examples
pythonmarkdownpermalinks

Generate permalinks on header with python markdown library


I was wondering how to generate permalinks from the following markup, using python markdown library:

A header
========

A paragraph

The desired output would be something like

<span id="a-header"></span>
<h1>
  A header
  <a class="headerlink" title="Permalink to this headline" href="#a-header">¶</a>
</h1>
<p>A paragraph</p>

Answer:

Thanks @BlaXpirit (see answer)

Use headerid python markdown extension and input the following:

# A header [¶](#a-header) {#a-header}

A paragraph

This generates the following output:

<h1 id="a-header">
  A header
  <a href="#a-header">¶</a>
</h1>

Then use some css styling to get the common output, something like :

h1 a{visibility:hidden;}
h1:hover a{visibility:visible;}

Solution

  • Markdown in Python has an extension that does this.
    It also lets you specify an id you like for the header, like this:

    A header            {#a-header}
    ========