class mdfy.elements.text.MdText(content: str, formatter: MdFormatter | None = None, no_style: bool = False)[source]

Bases: MdElement

MdElementt class to handle the text and styling of text.

content

The content string containing potential style markers.

Type:

str

formatter

The formatter to apply styling to the content.

Type:

MdFormatter

Examples

>>> # If you have installed mdfy[styled-text]
>>> from mdfy import MdText
>>> try:
>>>   import lark
>>> except ModuleNotFoundError:
>>>   print("Please install mdfy[styled-text] to use MdText with styles.")
>>>   exit(0)
>>>
>>> text = MdText("This is [bold:bold] text.")
>>> print(text)
This is **bold** text.
>>> MdText("[This is underline text:underline].").to_str()
<u>This is underline text</u>.
>>> MdText("[This is [nested:bold] style text:underline].").to_str()
<u>This is **nested** style text</u>.
>>> MdText("You can use aliases e.g. [st:st]  [bd:bo].").to_str()
You can use aliases e.g. ***st***  **bd**.

Note

Available style patterns:
  • strong: bold text (e.g. ***strong***)

  • bold: bold text (e.g. **bold**)

  • italic: italic text (e.g. *italic*)

  • not: strike-through text (e.g. ~~strike-through~~)

  • underline: underlined text (e.g. <u>underlined</u>)

  • code: inline code (e.g. code)

Also, the following aliases are available for the style patterns:
  • strong: st

  • bold: bo, bd

  • italic: it

  • not: no, nt

  • underline: un, ul

  • code: cd, quote

__str__() str[source]

Returns the styled content as per the specified style markers.

Returns:

Formatted markdown string with the appropriate styles applied.

Return type:

str

__add__(other: MdText) MdText[source]

Adds two MdText objects together.

Parameters:

other (MdText) – The other MdText object to be added.

Returns:

A new MdText object containing the concatenated content of the two objects.

Return type:

MdText