Python-Markdown/markdown

Table Extension not work

Closed this issue · 3 comments

I'm trying to use the TableExtension , but it does not seem to work as expected.

import markdown
from markdown.extensions.tables import TableExtension

md_table = """**Sample Table**:
| Genre        | Anime Example      | Live Action Example |
|--------------|--------------------|---------------------|
| Sci-Fi       | Cowboy Bebop       | Star Wars           |
| Romance      | Your Name          | The Notebook        |
| Fantasy      | Attack on Titan    | Lord of the Rings   |
the unique storytelling techniques of anime🎥✨"""

# Convert Markdown to HTML
html_output = markdown.markdown(md_table, extensions=[TableExtension(use_align_attribute=True)],
                                output_format="html")

# Print the HTML result
print(html_output)

Result :

imagesss

@abdilahsamari100 The table extension works fine, but you have to have the table as a block by itself, not part of another paragraph. So you need to have a blank line after the first paragraph, and before the second.

import markdown

print(f'Markdown Version: {markdown.__version__}')
print('-----')

MD = """**Sample Table**:

| Genre        | Anime Example      | Live Action Example |
|--------------|--------------------|---------------------|
| Sci-Fi       | Cowboy Bebop       | Star Wars           |
| Romance      | Your Name          | The Notebook        |
| Fantasy      | Attack on Titan    | Lord of the Rings   |

the unique storytelling techniques of anime🎥✨"""

extensions = ["tables"]
extension_configs = {'tables': {'use_align_attribute': True}}

print(markdown.markdown(MD, extensions=extensions, extension_configs=extension_configs))

Results:

Markdown Version: 3.7
-----
<p><strong>Sample Table</strong>:</p>
<table>
<thead>
<tr>
<th>Genre</th>
<th>Anime Example</th>
<th>Live Action Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sci-Fi</td>
<td>Cowboy Bebop</td>
<td>Star Wars</td>
</tr>
<tr>
<td>Romance</td>
<td>Your Name</td>
<td>The Notebook</td>
</tr>
<tr>
<td>Fantasy</td>
<td>Attack on Titan</td>
<td>Lord of the Rings</td>
</tr>
</tbody>
</table>
<p>the unique storytelling techniques of anime🎥✨</p>

@facelessuser
The suggestion to have a blank line before and after the table makes sense and i test and work, but I'm unable to edit the Markdown content directly.
I generate markdown content using AI, and I can't manually adjust the spacing between paragraphs and tables. Is there any way to handle this in the generated content without needing manual edits?"

Aside from teaching the AI to output the content like you want it, no.