trentm / python-markdown2

markdown2: A fast and complete implementation of Markdown in Python
Other
2.64k stars 431 forks source link

Add `breaks` extra with ability to hard break on backslashes (issue #525) #529

Closed Crozzers closed 1 year ago

Crozzers commented 1 year ago

This PR closes #525 by adding the ability to insert hard breaks when lines end in a backslash.

The feature

By passing extras={'breaks': {'on_backslash': True}}, users can now use backslashes to insert hard breaks, an example of which can be found in the break_on_backslash test case.

Implementation

This has been implemented via a new breaks extra, which contains this new functionality as well as the old break-on-newline functionality. The old extra name has been kept as an alias, so the two following examples are equivalent:

markdown2.convert(text, extras=['break-on-newline'])
markdown2.convert(text, extras={'breaks': {'on_newline': True}})
markdown2.convert(text, extras={'breaks': {'on_newline': True, 'on_backslash': False}})

This was done because I felt that having separate break-on-* extras was unnecessary and that grouping these similar extras made sense.

nicholasserra commented 1 year ago

Looks great, thank you!

m417z commented 9 months ago

Thank you. I assume it should be mentioned here at some point, right? Especially now that a version with this feature was released. https://github.com/trentm/python-markdown2/wiki/Extras

Crozzers commented 9 months ago

Good shout. I've added a quick page for it here: https://github.com/trentm/python-markdown2/wiki/breaks

m417z commented 9 months ago

Looks good. I think it makes sense to link it from the Extras page I mentioned.

Crozzers commented 9 months ago

Updated the list of implemented extras as well

m417z commented 9 months ago

Looks like the text in the wiki contradicts the actual behavior:

<p>This line will have a line break<br />
But not this one<br />
Because the backslash has to have a space before it</p>

A space before a backslash isn't required.

Also, I created a PR with some improvements: https://github.com/trentm/python-markdown2/pull/544

Crozzers commented 9 months ago

Looks like the text in the wiki contradicts the actual behavior

Thanks for spotting this. Updated the wiki to reflect actual behaviour.

Also, I created a PR with some improvements

Very cool! Thank you