Skip to content

Build step

DocBuilder renders a node tree into a BuildOutput: pages (Markdown of one flavour), assets, the nav as NavItem tree and the Markdown extensions / CSS / JS the pages need. MarkdownExporter writes it as plain Markdown files plus a SUMMARY.md nav. The site generator (mknodes build) does the same and then runs Zensical, so this is only needed to use MkNodes without it.

Example
import mknodes as mk

nav = mk.MkNav()
page = nav.add_page("Usage", is_index=True)
page += mk.MkAdmonition("Install with `pip install mknodes`.", typ="tip")

output = await mk.DocBuilder(flavor="github").build(nav)
await mk.MarkdownExporter().export(output, "site/")  # index.md + SUMMARY.md

mknodes.DocBuilder

Renders a node tree (root MkNav + optional theme) into a BuildOutput.

For each page: render the Markdown in the chosen flavour, collect its resources, wire up its page template and optionally render it with Jinja. Afterwards the resources of all pages (+ theme) get turned into assets, stylesheets, scripts and Markdown extensions, and the nav into a NavItem tree.

__init__

__init__(
    *,
    flavor: str | Renderer | None = None,
    render_jinja: bool = True,
    show_page_info: bool = False,
    global_resources: bool = True,
    max_workers: int | None = None
) -> None

Constructor.

Parameters:

Name Type Description Default
flavor str | Renderer | None

Markdown flavour to render (default: the active renderer, Material).

None
render_jinja bool

Render pages with their Jinja environment (pages can opt out with the render_macros: false metadata).

True
show_page_info bool

Append a collapsible box with build info to every page (pages can override with the show_page_info metadata).

False
global_resources bool

Collect page CSS / JS for the whole site. If False, they get moved into the page templates (only these pages load them).

True
max_workers int | None

Render pages in that many threads (default: sequentially).

None

build async

build(root: MkNav, theme: Theme | None = None) -> BuildOutput

Render given tree.

Problems which don't stop the build (failed nodes, broken code fences, ...) get logged and collected in BuildOutput.diagnostics.

Parameters:

Name Type Description Default
root MkNav

Root nav of the tree.

required
theme Theme | None

Theme whose resources, templates and extras get included.

None

mknodes.BuildOutput dataclass

Everything a node tree produced, independent of the target.

Pages are rendered in one Markdown flavour (see mknodes.rendering). Exporters / site generators turn this into files or into the input of a static site engine.

assets class-attribute instance-attribute

assets: dict[str, str | bytes] = dataclasses.field(default_factory=dict)

Static files: path relative to the docs root -> content (CSS, JS, images, ...).

diagnostics class-attribute instance-attribute

diagnostics: list[Diagnostic] = dataclasses.field(default_factory=list)

Problems reported while building (failed nodes / blocks, broken fences, references).

edit_paths class-attribute instance-attribute

edit_paths: dict[str, str] = dataclasses.field(default_factory=dict)

Page path -> repository path of the source that generated it.

extra class-attribute instance-attribute

extra: dict[str, Any] = dataclasses.field(default_factory=dict)

Theme variables (extra in MkDocs / Zensical configs).

extra_css class-attribute instance-attribute

extra_css: list[str] = dataclasses.field(default_factory=list)

Stylesheets: URLs or paths relative to the docs root (contained in assets).

extra_javascript class-attribute instance-attribute

extra_javascript: list[Script] = dataclasses.field(default_factory=list)

Scripts: URLs or paths relative to the docs root (contained in assets).

files property

files: dict[str, str | bytes]

Pages and assets combined.

flavor class-attribute instance-attribute

flavor: str = 'material'

Markdown flavour the pages are rendered in.

markdown_extensions class-attribute instance-attribute

markdown_extensions: dict[str, dict[str, Any]] = dataclasses.field(default_factory=dict)

Python-Markdown extensions required by the pages: name -> config.

metadata class-attribute instance-attribute

metadata: SiteMetadata = dataclasses.field(default_factory=SiteMetadata)

Site metadata inferred from the project.

nav class-attribute instance-attribute

nav: list[NavItem] = dataclasses.field(default_factory=list)

Navigation tree.

page_resources class-attribute instance-attribute

page_resources: dict[str, Resources] = dataclasses.field(default_factory=dict)

Page path -> resources required by that page.

pages class-attribute instance-attribute

pages: dict[str, str] = dataclasses.field(default_factory=dict)

Markdown pages: path relative to the docs root -> Markdown.

resources property

resources: Resources

Resources of all pages merged into one.

templates class-attribute instance-attribute

templates: dict[str, str] = dataclasses.field(default_factory=dict)

Theme template overrides (Material-style Jinja blocks): filename -> template.

mknodes.build.output.NavItem dataclass

An entry of the site navigation.

Either a link to a page / URL (target set) or a section (children set).

target class-attribute instance-attribute

target: str | None = None

Page path (relative to the docs root) or an external URL.

mknodes.MarkdownExporter

Writes the pages and assets plus the nav as SUMMARY.md.

The result is readable in any Markdown viewer (and on GitHub, with the github flavour). SUMMARY.md uses the literate-nav format.

__init__

__init__(summary_file: str | None = SUMMARY_FILE) -> None

Constructor.

Parameters:

Name Type Description Default
summary_file str | None

Filename for the nav (None: don't write the nav).

SUMMARY_FILE

export async

export(output: BuildOutput, target: str | PathLike[str]) -> None

Write the files of given output into target folder (local path or fsspec URL).

get_files

get_files(output: BuildOutput) -> dict[str, str | bytes]

Return all files to write: path relative to the target -> content.

Page info
Code for this page
mknodes.manual.api_section._add_page.<locals>._
@nav.route.page(title, hide="toc")
def _(page: mk.MkPage) -> None:
    page += INTROS[title]
    page += mk.MkCode(EXAMPLES[title].rstrip(), language="py", title="Example")
    for identifier in identifiers:
        page += mk.MkDocStrings(identifier, **DOCSTRING_OPTIONS)
Resources
Resources(css=[],
          markdown_extensions={'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/api_section.py
  source_function: _add_page.<locals>._
  source_line_no: 168
hide:
- toc
title: Build step