Limiting the visual length of rich content (e.g. HTML, markdown)

Viewed 33

When displaying, for example, the summary of a blog post, I'd like to trim the "above the fold" section to a particular visual length. Say, 300 letters.

I can trim the source content (e.g. the markdown or HTML), but this runs the risk of splitting up elements which really shouldn't be split. What if the 300th source character is in the middle of <a href="https://google?

The same goes for content in any "rich" format, like Markdown.

One potential solution is to strip all formatting before creating the summary, but that means that bolding, italics, and even hyperlinks or other content will be unnecessaryly stripped.

I can imagine an algorithm based on depth-first search to find all text nodes in, for example, an HTML fragment, and cut off the text content after a limit is reached. But I'd love to know if there's any prior art. I've been trying to search the internet, but haven't had any luck. I did find WordPress's algorithm, which seems to be a naive word counter.

0 Answers
Related