Skip to content

Keep hard line breaks that the output normalization strips - #2643

Open
Patrick Creighton (patrick-5546) wants to merge 1 commit into
microsoft:mainfrom
patrick-5546:fix/keep-hard-line-breaks
Open

Patrick Creighton (patrick-5546) wants to merge 1 commit into
microsoft:mainfrom
patrick-5546:fix/keep-hard-line-breaks

Conversation

@patrick-5546

Copy link
Copy Markdown

Fixes #2642.

markdownify writes <br> as two trailing spaces, and MarkItDown.convert() strips them from every output line, so hard line breaks became soft breaks. _CustomMarkdownify.convert_br now writes a backslash hard break when the <br> has text or an image on both sides of it in its parent element. A backslash break survives the normalization:

Line one\
Line two

Other breaks keep markdownify's handling, so their output is unchanged. These are breaks at the start or end of an element, and breaks in table cells, headings and preformatted text.

#2559 takes the other approach: it keeps two-space breaks in the normalization. That leaves converter output unchanged. However, it also keeps trailing double spaces that come from the input rather than from markdownify, such as those in plain-text files, and it changes the same lines as #2529 and #2633.

Tests:

  • New HTML and DOCX tests cover kept breaks, breaks at element edges, and breaks in table cells, headings and code.
  • Tests that recorded the two-space converter output now expect the backslash: RSS, XLSX, strikethrough, and the OCR plugin's DOCX, PPTX and XLSX tests.

Checks:

  • packages/markitdown: 1107 passed, 11 skipped.
  • markitdown-ocr: 108 passed.
  • markitdown-mcp: 21 passed.
  • black 23.7.0: clean.

markdownify writes <br> as two trailing spaces, which MarkItDown.convert()
then strips from every output line, so Word line breaks and HTML <br>
became soft breaks that render as a space. A break with text or an image
on both sides of it in its parent element is now written as a backslash
hard break, which survives the normalization. Other breaks, and those in
table cells, headings, and preformatted text, keep markdownify's handling.

Tests that recorded the old converter output, including the OCR
plugin's, now expect the backslash.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@patrick-5546

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hard line breaks are lost: <br> and Word line breaks become spaces

1 participant