polarwinkel / mdtex2html

python3-library to convert Markdown with included LaTeX-Formulas to HTML with MathML
GNU Lesser General Public License v2.1
41 stars 5 forks source link

markdown.extensions.toc compatibility issue #4

Closed AugustinWinther closed 2 weeks ago

AugustinWinther commented 1 month ago

First of, thank you for this derivation of the Python-Markdown module! Saved me a lot of time hehe. Just wanted to let you know about a extension compatibility issue.

The toc extension is one of the officially supported extensions of Python-Markdown. It is used to generate a Table of Contents at any arbitrary point in the file by inserting the tag [TOC].

When using the "toc" extension with mdtex2html, it will only be able to generate a table of contents of the headers before the first codeblock. I assume this is due to the extension needing to see the all of the headers when converting, something that is not possible with mdtex2html as it splits the input markdown up in chunks based on the codeblocks :/

I assume this issue will affect all extensions that need to see the whole document to work properly.

You can run the code below to verify this:

import markdown
import mdtex2html

md_text = """
**Table of Contents**

[TOC]

# Can see this
## And even this

```py
print("Hello world!")

But not this

And not this

"""

md = markdown.Markdown(extensions=["toc"]) html_markdown = md.convert(md_text) print("\n\nWITH MARKDOWN:\n" + html_markdown)

html_mdtex2html = mdtex2html.convert(md_text, extensions=["toc"]) print("\n\nWITH MDTEX2HTML:\n" + html_mdtex2html)

polarwinkel commented 1 month ago

Hi AugustinWinther, thanks for your report andd your analysis. I think you are right, the code is split up to chunks which I remember having good reasons for, though I don´t know why anymore. So it looks a little tricky right now, I'll have time to take a deeper dive into this by the end of next month. hank's for a little patience :-)

AugustinWinther commented 1 month ago

I'll have time to take a deeper dive into this by the end of next month. hank's for a little patience :-)

No worries :) I did however now manage to find a Python-Markdown extension that does MathML conversion (released last month actually) which is able to do what I want, so I'll be sticking with that one :)

polarwinkel commented 2 weeks ago

I just pushed a fix for this, your verification code runs now as expected. I'll do some more testing in my production systems and check other extensions for similar bugs before releasing a new version.

Thanks again for your detailed and clear report!