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
{{ message }}
Repository navigation
Bug: nl2br and toc extensions removes line that come immediatly after [toc] #1160
While this may be considered a bug, you really shouldn't make the [TOC] part of another paragraph. Having [TOC] in its own paragraph would resolve your issue, but, technically, I would consider this a bug.
you really shouldn't make the [TOC] part of another paragraph
Yes I know 😅 , I expect this pattern not being populated as a toc.
I manage a wiki with more than 80,000 users and 1,000,000 documents, and almost all of them don't know markdown, and write it with a trial/error process. Any inconsistency in our markdown parser end up with terrible hacks in our base :/
A lead for the root cause: if I use TocTreeprocessor.iterparent and TocTreeprocessor.replace_marker from 3.3.1, I do not have the issue.
c is an element, and we check that c.text == marker.
But, with nl2br extension, it's possible to have children in c. In our case, it would be <p>[TOC]<br />Where am I?</p>. And as it comes from an extension, the br is a real Element, not a placeholder.
The issue is that, when an element is in that situation, c.text returns only the very first content, so [TOC]. And the extension entirely replace the content of the <p>.
To fix this, simply add a condition on len(c) == 0.
My first reaction was to say that this is user error and no bug exists. However, on closer inspection, I realized that there is data loss (the second line of the "paragraph" is not included in the output) and that is a bug.
Activity
facelessuser commentedon Jul 27, 2021
While this may be considered a bug, you really shouldn't make the
[TOC]part of another paragraph. Having[TOC]in its own paragraph would resolve your issue, but, technically, I would consider this a bug.cbeauchesne commentedon Jul 27, 2021
Hi @facelessuser,
Yes I know 😅 , I expect this pattern not being populated as a toc.
I manage a wiki with more than 80,000 users and 1,000,000 documents, and almost all of them don't know markdown, and write it with a trial/error process. Any inconsistency in our markdown parser end up with terrible hacks in our base :/
A lead for the root cause: if I use
TocTreeprocessor.iterparentandTocTreeprocessor.replace_markerfrom 3.3.1, I do not have the issue.I'll try to dig it the next days.
cbeauchesne commentedon Jul 27, 2021
That's not true, sorry, it was a version I used locally.
So, the answer. On line 198: https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/toc.py#L195-L198
cis an element, and we check thatc.text == marker.But, with
nl2brextension, it's possible to have children inc. In our case, it would be<p>[TOC]<br />Where am I?</p>. And as it comes from an extension, thebris a realElement, not a placeholder.The issue is that, when an element is in that situation,
c.textreturns only the very first content, so[TOC]. And the extension entirely replace the content of the<p>.To fix this, simply add a condition on
len(c) == 0.I'll propose a PR.
Better toc detection Python-Markdown#1160
Better toc detection Python-Markdown#1160
waylan commentedon Jul 27, 2021
My first reaction was to say that this is user error and no bug exists. However, on closer inspection, I realized that there is data loss (the second line of the "paragraph" is not included in the output) and that is a bug.
Better toc detection Python-Markdown#1160
cbeauchesne commentedon Jul 28, 2021
Thanks @waylan !
Last question : do you know when you'll release the next version ?
And again, thank you for this great project !
Better toc detection