MkNode
The base class of every node, page and nav: tree handling (parent, children,
iter_nodes()), the project context (ctx), the Jinja environment (env) and the
rendering API (node.to_markdown() / str(node), to_html(),
get_resources()). Rendering is synchronous and never fetches: external inputs
(icons, remote files, LLM texts, ...) come from the inputs store, resolved before rendering.
to_html(flavor=...) converts with a parser that knows the flavour:
Python-Markdown for material, comrak for github / mdbook, markdown-it-py with the MyST
plugins (directives mapped to Sphinx-like HTML, an approximation) for myst; the last two
need the html extra (pip install mknodes[html]). Its node page
(MkNode) shows the examples.
import mknodes as mk
node = mk.MkAdmonition("Careful!", typ="warning")
markdown = node.to_markdown() # same as str(node)
html = node.to_html()
github_html = node.to_html(flavor="github") # needs mknodes[html]
resources = node.get_resources() # Markdown extensions, CSS, JS
mknodes.MkNode
¶
Base class for everything which can be expressed as Markup.
This class combines tree node functionality with markdown generation. Starting from the root nav (aka Docs) down to nested Markup blocks, the whole project can be represented by one tree.
MkNode is the base class for all nodes. We dont instanciate it directly.
All subclasses carry an MkAnnotations node (except the MkAnnotations node itself)
They can also pass an indent as well as a shift_header_levels keyword argument
in order to modify the resulting markdown.
ancestors
property
¶
ancestors: Iterable[MkNode]
Get iterator to yield all ancestors of self, does not include self.
ctx
property
¶
The tree context.
Will return the attached context or the closest context from parents. If no context is found, return an "empty" ProjectContext.
descendants
property
¶
descendants: Iterable[MkNode]
Get iterator to yield all descendants of self, does not include self.
env
cached
property
¶
The node jinja environment.
The environment has additional loaders for the class file path as well as the resolved parent nav file path.
files
property
¶
Return a dict containing the virtual files attached to this tree element.
This can be overridden by nodes if they want files to be included dynamically.
For static files, use add_file.
parent_navs
property
¶
parent_navs: list[MkNav]
Return a list of parent MkNavs, ordered from root to leaf.
parent_page
property
¶
parent_page: MkPage | None
Return the page which contains this node if existing.
resolved_parts
property
¶
Return a tuple containing all section names.
__init__
¶
__init__(
*,
header: str = "",
indent: str = "",
name: str | None = None,
shift_header_levels: int = 0,
variables: dict[str, Any] | None = None,
context: ProjectContext | None = None,
parent: MkNode | None = None,
**_kwargs: Any
) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
str
|
Optional header for contained Markup |
''
|
indent
|
str
|
Indentation of given Markup (unused ATM) |
''
|
name
|
str | None
|
An optional unique identifier (allows getting node via MkNode.get_node) |
None
|
shift_header_levels
|
int
|
Regex-based header level shifting (adds/removes #-chars) |
0
|
variables
|
dict[str, Any] | None
|
Variables to use for rendering |
None
|
context
|
ProjectContext | None
|
Context this Nav is connected to. |
None
|
parent
|
MkNode | None
|
Parent for building the tree |
None
|
add_css_class
¶
Wrap node markdown with given css class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str
|
CSS class to wrap the node with |
required |
add_file
¶
Add a static file as data to this node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Filename of the file to add |
required |
data
|
str | bytes
|
Data of the file |
required |
attach_annotations
¶
Attach annotations block to given markdown.
Can be reimplemented if non-default annotations are needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Markdown to annote |
required |
attach_css_classes
¶
Attach CSS classes to given markdown in attr-list style.
Can be reimplemented if non-default behavior is needed. Default behavior is appending the css class snippet with a space as separator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Markdown to annote |
required |
get_content
¶
Return markdown and resources for this node in a single pass.
Override this method in subclasses to compute both markdown and resources together when they share expensive computation.
Default implementation calls legacy methods to_md_unprocessed() and
_build_node_resources() for backward compatibility.
get_input_requests
¶
Return the external inputs this node needs to render (none by default).
The builder resolves the inputs of all nodes concurrently before rendering;
the node reads them from self.ctx.inputs.
get_node_resources
¶
Return the resources specific for this node.
Default implementation calls _build_node_resources(). Override
get_content() if you need to compute markdown and resources together.
get_processors
¶
Return list of processors used to created markdown.
get_resources
¶
Return the resources of this node and all children (extensions, CSS, JS, ...).
is_descendant_of
¶
Returns True if any ancestor is of given type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kls
|
type | UnionType
|
The class (union) the check the ancestors for |
required |
iter_nodes
¶
iter_nodes(indent: int = 0, max_depth: int | None = None) -> Iterator[tuple[int, MkNode]]
Iterate over all nodes, including self and children.
Yields current-depth-node tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int
|
The start "level". The first element of the returned tuple will be relative to this |
0
|
max_depth
|
int | None
|
The max depth to iterate |
None
|
to_child_node
¶
to_child_node(other: str | list[Any] | MkNode | None)
Convert given nodes / strings to child nodes.
Either converts text to an MkNode sets parent of node to self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
str | list[Any] | MkNode | None
|
The node / string to convert to a child node. |
required |
to_html
¶
to_html(flavor: str | Renderer | None = None) -> str
Convert node to HTML using the resources from node + children.
The Markdown is rendered in given flavour (default: the active one) and converted
with a parser knowing that flavour (Renderer.to_html, see rendering.html).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flavor
|
str | Renderer | None
|
Markdown flavour (renderer name like |
None
|
to_markdown
¶
to_markdown(flavor: str | Renderer | None = None) -> str
Return the Markdown of this node and all children (same as str(node)).
Rendering is sync and only reads resolved inputs. At the top level it runs in
render passes: inputs found while rendering get resolved, then the node renders
again (see mknodes.inputs.passes); nested inside a render it just renders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flavor
|
str | Renderer | None
|
Markdown flavour (renderer name like |
None
|
to_md_unprocessed
¶
Return raw markdown for this node. Override in subclasses for custom markdown.
with_context
classmethod
¶
Same as the Ctor, but auto-adds a context for the repo url (or the cwd).