You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can modify the markdownify function to add a custom renderer for code blocks inside ordered lists.
Here's an example of how you can modify the markdownify function to achieve this:
from markdownx.utils import markdownify as original_markdownify
from markdown.extensions import Extension
from markdown.blockprocessors import BlockProcessor
from markdown.util import etree
class CodeBlockProcessor(BlockProcessor):
def test(self, parent, block):
return block.startswith('```') and parent.tag == 'ol'
def run(self, parent, blocks):
block = blocks.pop(0)
code = block.strip('```')
li = etree.SubElement(parent, 'li')
pre = etree.SubElement(li, 'pre')
pre.text = code
return True
class OrderedCodeExtension(Extension):
def extendMarkdown(self, md):
md.parser.blockprocessors.register(CodeBlockProcessor(md.parser), 'code_block', 175)
def markdownify(markdown_text):
html_text = original_markdownify(markdown_text, extensions=[OrderedCodeExtension()])
return html_text
In the above code, we first import the original markdownify function from markdownx.utils, as well as the Extension, BlockProcessor, and etree classes from the markdown module.
Then, we define a new CodeBlockProcessor class that extends BlockProcessor. This class tests whether a block starts with ``` and the parent tag is ol. If so, it creates a new li element and a pre element inside it, and sets the text of the code block as the text of the pre element.
Next, we define an OrderedCodeExtension class that extends Extension. This class registers our CodeBlockProcessor with the Markdown parser.
Finally, we define a new markdownify function that calls the original markdownify function with our OrderedCodeExtension added as an extension.
If I want to insert a code block with ``` inside an ordered list I cannot continue the list, it restarts. What can I do to fix this?
The following do not work:
<pre>
.Thanks a ton in advance.
The text was updated successfully, but these errors were encountered: