Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordered list restart after a code block. #80

Open
rasulkireev opened this issue Oct 22, 2019 · 1 comment
Open

Ordered list restart after a code block. #80

rasulkireev opened this issue Oct 22, 2019 · 1 comment

Comments

@rasulkireev
Copy link

rasulkireev commented Oct 22, 2019

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:

  • {:start="3"} - renders this exact text
  • Indenting the code block do not work. Code renders as code, rather than as <pre>.

Thanks a ton in advance.

@some1ataplace
Copy link

Maybe something like this. I did not test this.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants