Skip to content

markdown

This module contains the classes that manipulate markdown content.

MkHeaderLevels(markdown: str)

Get the levels of the headers in a markdown.

Parameters:

  • markdown (str) –

    The markdown string to parse.

Returns:

  • MkLevel

    The levels of the headers in the markdown.

Source code in pyobsidian/markdown.py
41
42
def __init__(self: Self, markdown: str) -> None:
    self.__markdown = markdown

markdown: str property

Markdown parsed.

Returns:

  • str

    The markdown parsed.

pattern: str property

The pattern to match the headers.

Returns:

  • str

    The pattern to match the headers.

get_headers(markdown: str, pattern: re.Pattern[str]) -> MkHeader staticmethod

Get the headers in the markdown string.

Parameters:

  • markdown (str) –

    The markdown string to parse.

  • pattern (Pattern[str]) –

    The pattern to match the headers.

Returns:

  • MkHeader

    The headers in the markdown string.

Source code in pyobsidian/markdown.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@staticmethod
def get_headers(markdown: str, pattern: re.Pattern[str]) -> MkHeader:
    """Get the headers in the markdown string.

    Parameters
    ----------
    markdown : str
        The markdown string to parse.
    pattern : re.Pattern[str]
        The pattern to match the headers.

    Returns
    -------
    MkHeader
        The headers in the markdown string.
    """
    headers = [
        {
            'start': m.start(), 
            'end': m.end(),
            'level': len(m.group(1)),
            'content': m.group(2)
        }
        for m in re.finditer(pattern, markdown)
    ]
    return headers

get_levels() -> MkLevel

Get the levels of the headers in the markdown string.

Returns:

  • MkLevel

    The levels of the headers in the markdown string.

Source code in pyobsidian/markdown.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def get_levels(self: Self) -> MkLevel:
    """Get the levels of the headers in the markdown string.

    Returns
    -------
    MkLevel
        The levels of the headers in the markdown string.
    """
    result: MkLevel = {}
    c_pattern = re.compile(self.pattern, re.MULTILINE)
    headers = self.get_headers(self.markdown, c_pattern)        
    for header in headers:
        level = str(header['level'])
        if level not in result:
            result[level] = []
        result[level].append({
            'start': header['start'],
            'end': header['end'],
            'content': header['content']
        })
    return result

parse_markdown(markdown: str) -> str

Parse the markdown string.

Parameters:

  • markdown (str) –

    The markdown string to parse.

Returns:

  • str

    The parsed markdown string.

Source code in pyobsidian/markdown.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def parse_markdown(markdown: str) -> str:
    """Parse the markdown string.

    Parameters
    ----------
    markdown : str
        The markdown string to parse.

    Returns
    -------
    str
        The parsed markdown string.
    """
    return '\\n'.join(markdown.split('\\n'))