fix(html): keep the whitespace of a whitespace-only inline element - #2507
Open
Lukas (L4XB) wants to merge 1 commit into
Open
Lukas (L4XB) wants to merge 1 commit into
Lukas (L4XB) wants to merge 1 commit into
Conversation
markdownify's chomp() lifts the surrounding whitespace out of an inline element's text and then returns an empty string once nothing is left, so a whitespace-only element takes its whitespace with it: further<strong> </strong>reference converts to furtherreference, and <b>First</b><b> </b><b>Last</b> to **First****Last**. A word processor keeps a differently formatted space as a run of its own, so a document with a bolded space between two plain words converts through exactly that shape. microsoft#2477 fixed it for <u> alone. Wrap the conversion function markdownify resolves per tag instead, which covers the other twelve inline tags at once. Anchors are left out: microsoft#2396 is open for those and goes further than the whitespace-only case. Closes microsoft#1539
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1539
The bug
markdownify.chomp()lifts the whitespace surrounding an inline element's textout of that text, and the conversion functions then return
''once nothing isleft. A whitespace-only element therefore takes its whitespace with it and the
words on either side run together.
mainfurther<strong> </strong>referencefurtherreferencefurther reference<b>First</b><b> </b><b>Last</b>**First****Last****First** **Last**Hello<code> </code>worldHelloworldHello worldThis is #1539, which was closed pointing at an upstream fix.
markdownify 1.2.3is the current release and
abstract_inline_conversionstill reads:so the behaviour is unchanged on
maintoday, with that version installed.It is not only an HTML-input problem. A word processor keeps a differently
formatted space as a run of its own, so a
.docxwhere the space between twoplain words is bold reaches mammoth as
First<strong> </strong>Lastand comesout of MarkItDown as
FirstLast.tests/test_docx_inline_whitespace.pybuildsexactly that document; three of its four formattings fail on
main.The fix
#2477 solved this for
<u>by overridingconvert_u. There are twelve moretags with the same conversion shape, so rather than twelve more overrides this
wraps the function markdownify resolves for a tag in
get_conv_fn: awhitespace-only element returns its text unchanged, everything else takes the
normal path.
b, code, del, em, i, kbd, s, samp, strike, strong, sub, sup, uais deliberately not in that set. #2396 is open for anchors and covers morethan the whitespace-only case, including
<a> middle </a>, which this branchdoes not touch.
get_conv_fnarrived in markdownify 1.0. On an older release the dispatchernever calls it, so the override is simply never reached and nothing changes.
Tests
tests/test_html_converter.pygains the whitespace-only case for all thirteentags, the exact-character case (
 stays), the empty-elementcase, the joined-emphasis case, and controls that an element with real content
still converts to
**word**,`word`,~~word~~,<u>word</u>.tests/test_docx_inline_whitespace.pyis new: it builds a minimal.docxwhose middle run is a single space with bold, italic, underline or
strikethrough, and asserts the result is
First Last.15 of the new cases fail on
main. Seven mutants of the changed lines arekilled by them: the wrapper removed, the guard never firing, the guard inverted,
every element returning unconverted text, and
b,i,sdropped from the tagset one at a time.
Python 3.12, macOS, markdownify 1.2.3.