Skip to content

allow a wrap_width value of None for unlimited line lengths - #169

Merged
chrispy-snps merged 2 commits into
matthewwithanm:developfrom
chrispy-snps:chrispy/allow-unlimited-line-lengths
Jan 19, 2025
Merged

chrispy-snps merged 2 commits into
matthewwithanm:developfrom
chrispy-snps:chrispy/allow-unlimited-line-lengths

Conversation

@chrispy-snps

Copy link
Copy Markdown
Collaborator

Fixes #168.

The convert_p() function is updated to bypass the line-wrapping heuristics when wrap_width is set to None, thus allowing unlimited line lengths.

For large volumes of text content, bypassing the wrapping heuristics is about 4x faster than executing the wrapping heuristics with an arbitrarily large numerical value.

The unit tests are updated to include this case, and the README file is updated to mention this feature.

@AlexVonB

AlexVonB commented Jan 2, 2025

Copy link
Copy Markdown
Collaborator

Could you just set wrap=False and therefore not wrap at all?

@chrispy-snps

Copy link
Copy Markdown
Collaborator Author

@AlexVonB - when wrap=False, content newlines are left as-is (which is reasonable behavior):

import markdownify

def md(html, **options):
    return markdownify.MarkdownConverter(**options).convert(html)

html = """
<p>This is
a bunch
of text.</p>
""".strip()

print(md(html, wrap=False))
# This is
# a bunch
# of text.

But with this code change, wrapping can be used to reflow the text:

print(md(html, wrap=True, wrap_width=None))
# This is a bunch of text.

Effectively, it is a form of "wrapping" but with unlimited line lengths. The value of None explicitly bypasses the wrapping loop for better performance when doing this.

We were using a large numeric value in our content pipeline as a workaround, but this approach would be faster.

Comment thread markdownify/__init__.py
break_on_hyphens=False)
new_lines.append(line + trailing)
text = '\n'.join(new_lines)
if self.options['wrap_width'] is not None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to merge this with the preceding if? Disregard this comment if it does not.

@chrispy-snps chrispy-snps Jan 2, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexVonB - sharp eyes! Originally I had it on the higher-level if, but I decided to move it down here to reinforce the comment about newlines being replaced by spaces when wrap==True. It added a level of indent and made the diff bigger, but runtime is practically identical and I felt the code was a bit clearer this way.

@chrispy-snps
chrispy-snps merged commit 600f77d into matthewwithanm:develop Jan 19, 2025
@chrispy-snps
chrispy-snps deleted the chrispy/allow-unlimited-line-lengths branch February 17, 2025 13:45
Wuhall pushed a commit to Wuhall/python-markdownify that referenced this pull request May 21, 2025
…ithanm#169)

allow a wrap_width value of None to reflow text to unlimited line length
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.

Support a wrap width value of None that explicitly indicates no width limit (unlimited line length)

2 participants