@register_md("Heading")
@dataclass
class Heading(MdObj):
"""
Create a heading.
Pre-defined heading levels exists as exported objects 'H1' to 'H7'.
Args:
title (str): The heading title.
level (int): Level of the heading.
style (Literal["atx", "setext"]): Style of the heading in markdown.
anchor (Optional[Union[Anchor, str]]): Anchor to be added to heading.
"""
title: str
level: int
style: Literal["atx", "setext"] = "atx"
anchor: Optional[Union[Anchor, str]] = None
def __post_init__(self):
if isinstance(self.anchor, Anchor):
self._back = self.anchor.back
else:
self._back = None
if isinstance(self.anchor, str):
self.anchor = Anchor(self.anchor)
heading = mdt.Header.Header.choose_header(
self.level, self.title, self.style
).strip("\n")
if isinstance(self.anchor, Anchor):
# note, string conversion to Anchor done in post-init
heading += self.anchor.body.text
self._body = SpacedText(
heading,
(2, 2),
)
self._settings = None