Search code examples
javascriptpythonhtmlmarkdownmkdocs

Markdown fails combining fenced_code and attr_list


I'm trying to write markdown files for mkdocs and want an id attribute with the pre tag, generated be fenced_code. If i use both extensions in combination there is no pre-tag but a p(aragraph tag):

import markdown

text = """# Welcome

This is *true* markdown text.

````python
a=5
print "Hello World"
````{: #hello }
"""

html = markdown.markdown(text, extensions= ['markdown.extensions.fenced_code', 'markdown.extensions.attr_list'])
print html

print returns

<h1>Welcome</h1>
<p>This is <em>true</em> markdown text.</p>
<p><code id="hello">python
a=5
print "Hello World"</code></p>

but i expected

<pre id="hello"><code>...

it's the same under mkdocs, which i use actually. I need with id to access it through javascript and run the embedded python code wit skulpt. Is there a solution to achieve this?


Solution

  • I posted an issue to mkdocs on github and they say it is not possible at the moment. So i tried something else. Because i needed the id of the pre-element in a javascript-function which reacts to an onclick, i figured out, how to access the pre content from there. I was lucky to find that parentNode.previousElementSibling does what in want. The event's target is the element with the onclick event.

    elem = event.target.parentNode.previousElementSibling
    

    hope, anyone in a comparable situation understands what i mean :-)