Skip to content

Rich text

rich_text

Utilities for working with plain & rich texts in Notion.

MAX_TEXT_OBJECT_SIZE = 2000 module-attribute

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

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

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.

Rather use the constructor function text to create a Text object from a normal string with formatting.

mentions: tuple[Mention, ...] property

Return all mentions in the text.

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.

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.

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!

snake_case(string: str) -> str

Make a Python identifier in snake_case.

Attention: This may result in an empty string!

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.

Warning

If a Text object is passed, the original formatting will be lost!