Skip to content

header

Heading

Bases: MdObj

Source code in mkreports/md/header.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@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