Skip to content

Markdown

rn_forge.commons.text.markdown

Markdown structure: headings, anchors, links, tables and fenced code.

Documents are parsed with markdown-it-py (CommonMark plus GFM tables). Heading anchors follow Python-Markdown's toc extension, which MkDocs uses.

MarkdownHeading(level, text, anchor, line) dataclass

A heading of a Markdown document.

Attributes:

Name Type Description
level int

The heading level, 1 to 6.

text str

The heading as plain text; inline markup is dropped and code spans are kept as text.

anchor str

The anchor a renderer gives the heading, unique within the document.

line int

The 0-based line of the heading.

MarkdownTable(header, rows, start, end) dataclass

A GFM pipe table of a Markdown document.

Attributes:

Name Type Description
header tuple[str, ...]

The header cells.

rows tuple[tuple[str, ...], ...]

The body rows, each exactly len(header) cells.

start int

The 0-based line of the header row.

end int

The 0-based line after the last row.

MarkdownUtils

Read the structure of Markdown text.

anchors(text) staticmethod

Return the anchors of the headings of text.

headings(text) staticmethod

List the ATX and setext headings of text, in document order.

A heading inside code is not listed. A repeated anchor gets _1, _2 and so on.

is_external(link) staticmethod

Return whether link has a URL scheme or is scheme-relative.

List the link and image targets of text, in document order.

Inline, reference, collapsed-reference and autolink targets are included, percent-decoded. A reference with no definition and anything inside code is not a link.

Example::

MarkdownUtils.links("[a](b%20c.md) ![i](p.png)")  # ["b c.md", "p.png"]

slugify(text, separator='-') staticmethod

Return the anchor slug of text, as Python-Markdown's toc makes it.

Example::

MarkdownUtils.slugify("Hello, World!")  # "hello-world"

strip_fenced_code(text) staticmethod

Return text with every line of every fenced code block removed.

tables(text) staticmethod

List the GFM pipe tables of text, in document order.

A table inside code is not listed. Cells are stripped; a short row is padded with empty cells to the header's width.

title(text) staticmethod

Return the text of the first level-1 heading, or None.