0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andreas Müller
  • 210
  • 2
  • 10
  • i want to add, that the docs of php-markdown extra say it is possible, showing this example: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.html #example-1} here: https://michelf.ca/projects/php-markdown/extra/#fenced-code-blocks – Andreas Müller Sep 22 '16 at 12:15

1 Answers1

0

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 :-)

Andreas Müller
  • 210
  • 2
  • 10