Skip to content

Commit

Permalink
Support markup protocol in the preview filter (for markdown) (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
jace authored May 2, 2024
1 parent c85ca70 commit 069503a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/baseframe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,15 @@ def preview(html: str, min: int = 50, max: int = 158) -> str: # noqa: A002
:param int max: Maximum number of characters in the preview (default 158,
recommended for Google)
"""
# Check if we got a string-like object that implements the `__html__` protocol, and
# use that. This is the case for Coaster's Markdown Composite, where the text and
# html variants are different strings, so we can't use `str(html)` to get the text.
if callable(htmlfunc := getattr(html, '__html__', None)):
html = htmlfunc()
# Get the max length we're interested in, for efficiency in grapheme counts. A large
# blob of text can impair performance if we're only interested in a small preview.
# `max` can be < `min` when the caller specifies a custom `max` without `min`
# `max` can be < `min` when the caller specifies a custom `max` without `min`. In
# this case, we use `min`.
max_length = (max if max > min else min) + 1
blocks = text_blocks(html)
if blocks:
Expand Down

0 comments on commit 069503a

Please sign in to comment.