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

Markdown tables inside of ordered lists don't get parsed #158

Closed
supersam654 opened this issue May 3, 2018 · 5 comments
Closed

Markdown tables inside of ordered lists don't get parsed #158

supersam654 opened this issue May 3, 2018 · 5 comments

Comments

@supersam654
Copy link

When I nest a table inside of an ordered list (so the list numbers continue), the table doesn't get parsed into HTML.

import mistune

content = """
1. List item 1

    | Title |
    | Value |

2. List item 2
"""

markdown = mistune.Markdown()
print(markdown(content))

Prints

<ol>
<li><p>List item 1</p>
<p>| Title |
 | Value |</p>
</li>
<li><p>List item 2</p>
</li>
</ol>

even though I was expecting an HTML table inside of the list.

@lepture
Copy link
Owner

lepture commented May 4, 2018

@supersam654 That is designed on purpose, if you think you need this feature, you can subclass BlockLexter, and change here: https://github.com/lepture/mistune/blob/master/mistune.py#L184

@lepture lepture closed this as completed Sep 15, 2018
@nottrobin
Copy link

@lepture could you please expand a little more on why it was designed this way?

@nottrobin
Copy link

nottrobin commented Nov 20, 2018

@lepture I tried to subclass BlockLexer as suggested:

#! /usr/bin/env python3

from mistune import BlockLexer, Markdown

class WebteamBlockLexer(BlockLexer):
    list_rules = (
        'newline', 'block_code', 'fences', 'lheading', 'hrule',
        'block_quote', 'list_block', 'block_html', 'text',
        'table', 'nptable',
    )

parse_markdown = Markdown(
    parse_block_html=True, parse_inline_html=True,
    block=WebteamBlockLexer()
)

md_text = """
| Title |
| ----- |
| Value |

1. List item 1
   
   | Title |
   | ----- |
   | Value |

2. List item 2
"""

print(parse_markdown(md_text))

But it didn't work, giving the following output:

<table>
<thead><tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td>Value</td>
</tr>
</tbody>
</table>
<ol>
<li><p>List item 1</p>
<p>| Title |
 | ----- |
 | Value |</p>
</li>
<li><p>List item 2</p>
</li>
</ol>

@lepture would you be able to help me understand where I went wrong?

@nottrobin
Copy link

Okay, I worked it out - the order in that list matters. So table needs to precede block_html.

So, for posterity, here's a working example of how to support tables in lists:

from mistune import BlockLexer, Markdown

class MyBlockLexer(BlockLexer):
    list_rules = (
        'newline', 'block_code', 'fences', 'lheading', 'hrule',
        'block_quote', 'list_block', 'table', 'block_html', 'text',
    )

parse_markdown = Markdown(block=MyBlockLexer())

print(
    parse_markdown("""
1. List item 1

    | Title |
    | ----- |
    | Value |

2. List item 2
""")
)

Which outputs:

<ol>
<li><p>List item 1</p>
<table>
<thead><tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td>Value</td>
</tr>
</tbody>
</table>
</li>
<li><p>List item 2</p>
</li>
</ol>

@phseiff
Copy link

phseiff commented May 1, 2021

Did you manage to get the same thing to work with mistune==2.0.0rc1?

I'm currently trying, but subclassing mistune.block_parser.BlockParser doesn't seem to work anymore (errors due to a variety of reasons), and changing the Markdown object's rules order after creating it doesn't do the trick either.

UPDATE: It does work, and it is even easier than subclassing BlockParser (or BlockLexer, which I assume is how it was called in older versions).
All you need to do is

markdown = mistune.create_markdown(...)
# or markdown = mistune.Markdown(...)

markdown.block.list_rules += ['table', 'nptable']

Tested with 2.0.0rc1.

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

4 participants