Skip to content

markdown

Markdown processing utilities

The intended use case is for pre-formatting / correcting markdown headings that are not correctly formatted and/or are at the incorrect levels. The algorithm works in the following steps:

  1. Get a list of all chapter names from the source document.
  2. Identify the top-level headings.
  3. Increase the indent of all headings in the source document.
  4. Apply corrected headings.
  5. Run through TextProc/fix.

Classes

MarkdownProcessor

MarkdownProcessor(source_path: str)

Attributes

data instance-attribute
data = _load()
source_path instance-attribute
source_path = source_path

Functions

build_heading_map
build_heading_map() -> Dict[int, str]

Returns a heading map of line numbers and heading texts from the source text.

Parameters:

  • source_text (str) –

    The markdown content as a string.

Returns:

  • Dict[int, str]

    Dict[int, str]: A dictionary where the keys are line numbers and the values are heading texts.

decrease_heading_level
decrease_heading_level(heading_text: str) -> str

Decreases the heading level for a heading text string.

Parameters:

  • heading_text (str) –

    The heading text to decrease the level of.

Returns:

  • str ( str ) –

    The modified markdown content with a decreased heading level.

Raises:

  • ValueError

    If the heading text is not found in the source data.

get_heading_level
get_heading_level(level: int) -> Dict[int, str]

Gets entries in the heading map at a specific level.

Parameters:

  • heading_map (Dict[int, str]) –

    A dictionary where the keys are line numbers and the values are heading texts.

  • level (int) –

    The heading level to filter by.

Returns:

  • Dict[int, str]

    Dict[int, str]: A dictionary of headings at the specified level.

increase_all_headings
increase_all_headings() -> Dict[int, str]

Increases the heading level of all headings in the source text.

Parameters:

  • heading_map (Dict[int, str]) –

    A dictionary where the keys are line numbers and the values are heading texts.

Returns:

  • Dict[int, str]

    Dict[int, str]: A new dictionary with all heading levels increased.

Example
original_map = {0: "# Heading 1", 1: "## Heading 2"}
new_map = increase_all_headings(original_map)
# new_map will be {0: "## Heading 1", 1: "### Heading 2"}
increase_heading_level
increase_heading_level(heading_text: str) -> str

Increases the heading level for a heading text string.

Parameters:

  • source_text (str) –

    The markdown content as a string.

  • heading_text (str) –

    The heading text to increase the level of.

Returns:

  • str ( str ) –

    The modified markdown content with an increased heading level.

update_headings
update_headings(
    heading_map: Dict[int, str] | None = None
) -> None

Refresh internal data with updated headings.