Skip to content

Text

text

Utilities for working text, markdown & rich text in Notion.

AnyText: TypeAlias = RichTextBase[Any] | RichText module-attribute

For type hinting purposes, when working with various text types, e.g. Text, RichText, Mention, Math.

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.

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.

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(plain_text: str)

User-facing class holding several RichTextsBase objects.

obj_ref: list[objs.RichTextBaseObject] property

__add__(other: RichTextBase | RichText | str) -> RichText

__eq__(other: object) -> bool

__hash__()

from_markdown(text: str) -> RichText classmethod

Create RichTextList by parsing the markdown.

from_plain_text(text: str) -> RichText 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) -> RichText classmethod

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

__add__(other: RichTextBase | RichText | str) -> RichText

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)

A Text object.

Note

Use RichText instead for longer texts with complex formatting.

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: list[AnyText], *, delim: str | AnyText = ' ') -> RichText

Join multiple text objects into a single text object with a given delimeter.

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].

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_to_obj_ref(text: str | RichText | RichTextBase | list[RichTextBase]) -> list[objs.RichTextBaseObject]

Convert various text representations to a list of rich text objects.