core.html

Attributes

SANE_HTML_TAGS

SANE_HTML_ATTRS

VALID_PLAINTEXT_CHARACTERS

EMPTY_LINK

sanitize_policy

Functions

sanitize_html(html) → markupsafe.Markup

Takes the given html and strips all but a whitelisted number of tags

sanitize_svg(svg) → T

I couldn't find a good svg sanitiser function yet, so for now

html_to_text(html, *[, body_width, ignore_images, ...]) → str

Takes the given HTML text and extracts the text from it.

truncate_node(node, limit) → int

Safely truncates a parsed turbohtml node to limit characters.

Module Contents

core.html.SANE_HTML_TAGS[source]
core.html.SANE_HTML_ATTRS[source]
core.html.VALID_PLAINTEXT_CHARACTERS[source]
core.html.sanitize_policy[source]
core.html.sanitize_html(html: str | None) → markupsafe.Markup[source]

Takes the given html and strips all but a whitelisted number of tags from it.

core.html.sanitize_svg[T: str](svg: T) → T[source]

I couldn’t find a good svg sanitiser function yet, so for now this function will be a no-op, though it will try to detect svg files which are harmful.

I tried to go with bleach/html5lib, but the lack of xml namespace support makes those options a no go.

In the future we want a proper SVG sanitiser here!

core.html.html_to_text(html: str, *, body_width: int = 0, ignore_images: bool = True, single_line_break: bool = True, ignore_emphasis: bool = False, ul_item_mark: str = '*', strong_mark: str = '**', emphasis_mark: str = '_') → str[source]

Takes the given HTML text and extracts the text from it.

The result is markdown. The driver behind it is turbohtml.

core.html.truncate_node(node: turbohtml.Node, limit: int) → int[source]

Safely truncates a parsed turbohtml node to limit characters.

This avoids splitting HTML tags in the middle or leaving some tags unclosed, it also ensures the limit only applies to the actual text, not to the markup that doesn’t get rendered.

Based on the recipe provided by the turbohtml docs on truncating: https://turbohtml.readthedocs.io/en/stable/how-to/truncating.html