Skip to content

Rich text

rich_text

Utilities for working with rich texts & markdown in Notion.

MAX_TEXT_OBJECT_SIZE = 2000 module-attribute

The max text size according to the Notion API is 2000 characters.

MD_STYLES = ('bold', 'italic', 'strikethrough', 'code', 'link') module-attribute

Markdown styles supported by Notion.

MD_STYLE_MAP = {'bold': '**', 'italic': '*', 'strikethrough': '~~', 'code': '`'} module-attribute

Mapping from markdown style to markdown symbol.

T = TypeVar('T', bound=objs.RichTextBaseObject) module-attribute

render_md = get_md_renderer() module-attribute

Convert Markdown to HTML.

Math(expression: str, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT, href: str | None = None)

A inline equation object.

A LaTeX equation in inline mode, e.g. $ \mathrm{E=mc^2} $, but without the $ signs.

Note

Only used internally, use the math function instead, to create proper Text object.

Mention(target: User | Page | Database | dt.datetime | dt.date | pnd.Interval, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT)

A Mention object.

Note

Only used internally, use the mention function instead, to create proper RichText object.

obj_ref = objs.DateRange.build(target).build_mention(style=annotations) instance-attribute

type: str property

Type of the mention, e.g. user, page, etc.

RichText(text: str, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT, href: str | None = None)

A Text object.

Note

Only used internally, use Text instead.

RichTextBase(*args: Any, **kwargs: Any)

Super class for text, equation and mentions of various kinds.

is_equation: bool property

is_mention: bool property

is_text: bool property

Text(text: str)

User-facing class holding several RichTextsBase objects.

obj_ref: list[objs.RichTextBaseObject] property

rich_texts: tuple[RichTextBase, ...] property

Return the rich texts as immutable tuple.

__add__(other: str) -> Text

__eq__(other: object) -> bool

__hash__()

from_markdown(text: str) -> Text classmethod

Create RichTextList by parsing the markdown.

from_plain_text(text: str) -> Text classmethod

Create RichTextList from plain text.

This method is a more explicit alias for the default constructor.

to_html() -> str

Return rich text as HTML.

to_markdown() -> str

Convert the list of RichText objects to markdown.

to_plain_text() -> str

Return rich text as plaintext

This method is a more explicit variant then just using the object.

wrap_obj_ref(obj_refs: list[objs.RichTextBaseObject] | None) -> Text classmethod

camel_case(string: str) -> str

Make a Python identifier in CamelCase.

Attention: This may result in an empty string and a CamelCase sting will be capitalized!

chunky(text: str, length: int = MAX_TEXT_OBJECT_SIZE) -> Iterator[str]

Break the given text into chunks of at most length size.

decapitalize(string: str) -> str

Inverse of capitalize.

get_md_renderer() -> Markdown

Create a markdown renderer.

html_img(url: str, size: float) -> str

Create a img tag in HTML.

is_url(string: str) -> bool

Check if a string is a valid URL.

join(texts: Sequence[str], *, delim: str = ' ') -> Text

Join multiple str objects, including Text, into a single Text object with a given delimeter.

math(expression: str, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT) -> Text

Create a Text that holds a formula.

md_comment(text: str) -> str

Create a markdown comment.

md_spans(rich_texts: list[RichTextBase]) -> np.ndarray

Convert rich text to markdown spans.

An span is a sequence of rich texts with the same markdown style expressed as a row in the returned array. The value k of the j-th array column corresponds to the length of the current span richt_texts[j-k:j].

mention(target: User | Page | Database | dt.datetime | dt.date | pnd.Interval, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT) -> Text

Create a Text that mentions another object.

python_identifier(string: str) -> str

Make a valid Python identifier.

This will remove any leading characters that are not valid and change all invalid interior sequences to underscore.

Attention: This may result in an empty string!

rich_texts_to_markdown(rich_texts: list[RichTextBase]) -> str

Convert a list of rich texts to markdown.

snake_case(string: str) -> str

Make a Python identifier in snake_case.

Attention: This may result in an empty string!

sorted_md_spans(md_spans: np.ndarray) -> Iterator[tuple[int, int, str]]

Sort the spans of the given markdown spans in the right order.

We have to iterate from the smallest spans to the largest spans and from left to right.

text(text: str, *, bold: bool = False, italic: bool = False, strikethrough: bool = False, code: bool = False, underline: bool = False, color: Color = Color.DEFAULT, href: str | None = None) -> Text

Create a rich text Text object from a normal string with formatting.