Skip to main content

mkdocs-treeview

A Python-Markdown extension and MkDocs plugin that renders ASCII file trees with Material icon theme icons. Works with MkDocs, Material for MkDocs, and Zensical.

Inspired by asciidoctor-treeview.

```treeview
├── src/
│   ├── main.py
│   └── utils.py
├── tests/
│   └── test_main.py
├── Dockerfile
├── tsconfig.json
└── README.md
```
Light Dark
Treeview light theme Treeview dark theme

Features

  • Renders ```treeview fenced code blocks as styled file trees
  • Icons automatically chosen by file extension, file name, or folder name
  • Supports ASCII-tree format (tree command output) and symbol-based (*/#) format
  • Dynamic CSS — only icons actually used in your site are emitted (~7 KB instead of 1.3 MB)
  • Dark/light mode: separate light-variant icons for [data-md-color-scheme="slate"]
  • Three asset modes: files (local SVGs), embedded (base64 in CSS), cdn (jsDelivr)

Installation

pip install mkdocs-treeview

Usage

Write an ASCII tree in a fenced code block tagged treeview:

```treeview
├── src/
│   ├── main.py
│   └── utils.py
├── tests/
│   └── test_main.py
├── Dockerfile
├── tsconfig.json
└── README.md
```

Both formats are accepted:

ASCII-tree (output of the tree command):

├── src/
│   └── main.py
└── README.md

Symbol-based (indent depth = number of markers):

* src/
** main.py
* README.md

Inline comments

End an entry with whitespace, # and the text to add a comment. The comment is shown muted after the name and is ignored for the icon and the folder detection:

```treeview
├── src/          # application sources
│   └── main.py   # entry point
└── dist/         # build output
```

A # without whitespace before it is part of the name (C#, notes#1.md).


Configuration

MkDocs (mkdocs.yml)

plugins:
  - treeview:
      icon_mode: files   # files | embedded | cdn   (default: files)

The plugin registers the Markdown extension automatically and injects assets/treeview/treeview.css into extra_css. No other configuration needed.

Material for MkDocs (mkdocs.yml)

Identical to the MkDocs configuration — the plugin works with any MkDocs theme:

theme:
  name: material

plugins:
  - treeview:
      icon_mode: embedded

Zensical (zensical.toml)

Add the extension via markdown_extensions and point extra_css at the output file:

[project]
extra_css = ["stylesheets/treeview.css"]

[project.markdown_extensions."mkdocs_treeview.extension"]
css_output_path = "docs/stylesheets/treeview.css"
icon_mode = "embedded"

css_output_path is resolved relative to the directory where zensical build is run (the project root). The extension writes a lean CSS file there after each page.

Add .cache/ to your .gitignore:

.cache/

The extension writes a manifest file to .cache/treeview.manifest.json to accumulate icon data across pages. Zensical isolates each page render in its own Python sub-interpreter, so this file is the mechanism that lets the final CSS contain icons from all pages rather than just the last one rendered. The manifest is a build artifact — do not commit it.


Asset modes

Mode Behavior Best for
files SVGs copied to assets/treeview/icons/, CSS uses relative URLs Production MkDocs sites
embedded SVGs inlined as base64 data URIs — single self-contained CSS file Zensical, offline output
cdn CSS references jsDelivr URLs — no local files Quick preview, GitHub rendering

How it works

The MkDocs plugin hooks into on_config to register the Markdown extension and inject extra_css. During the build each page's treeview blocks are converted to HTML with tv-icon tv-icon-<name> CSS classes. After the build, on_post_build writes a lean CSS file containing only the icons that were actually rendered.

For Zensical, TreeviewCSSPostprocessor writes the CSS file after every page. Zensical isolates each page render in its own Python sub-interpreter, so the extension cannot keep an in-memory registry across pages. Instead, the extension persists the accumulated icon registry to .cache/treeview.manifest.json on disk. Each page render merges its icons into that file, then regenerates the full CSS. The final CSS contains every icon used across the entire site.


HTML output

The renderer emits flat <span class="tv-line"> rows with ASCII prefix connectors (matching the asciidoctor-treeview approach), rather than a nested <ul>/<li> tree:

<div class="treeview">
  <span class="tv-line">
    <span class="tv-line-prefix">├──&nbsp;</span>
    <span class="tv-line-element">
      <i class="tv-icon tv-icon-folder-src-open"></i>
      <span class="tv-item-name">src/</span>
    </span>
  </span>
  <span class="tv-line">
    <span class="tv-line-prefix">│&nbsp;&nbsp;&nbsp;├──&nbsp;</span>
    <span class="tv-line-element">
      <i class="tv-icon tv-icon-python"></i>
      <span class="tv-item-name">main.py</span>
    </span>
  </span>
  <span class="tv-line">
    <span class="tv-line-prefix">│&nbsp;&nbsp;&nbsp;└──&nbsp;</span>
    <span class="tv-line-element">
      <i class="tv-icon tv-icon-python"></i>
      <span class="tv-item-name">utils.py</span>
    </span>
  </span>
  <span class="tv-line">
    <span class="tv-line-prefix">└──&nbsp;</span>
    <span class="tv-line-element">
      <i class="tv-icon tv-icon-readme"></i>
      <span class="tv-item-name">README.md</span>
    </span>
  </span>
</div>

Folders with children use the open variant (folder-src-open.svg); leaf folders use the closed variant. Unknown file types fall back to a generic file icon.


Development

Requirements

  • Python >= 3.9
  • uv (recommended)
  • Node.js / npm (only for scripts/build_icons.py — regenerating the icon bundle)

Setup

uv sync

Run tests

uv run pytest tests/unit/
uv run pytest tests/integration/

Integration tests build real demo sites into tests/demos/ — inspect them in your editor after a test run:

tests/demos/
  mkdocs/       MkDocs default theme
  material/     Material for MkDocs
  zensical/     Zensical

Linting and type checking

uv run ruff format mkdocs_treeview/ tests/ scripts/
uv run ruff check mkdocs_treeview/ tests/ scripts/
uv run mypy mkdocs_treeview/

Pre-commit hooks run these automatically before every commit:

uv run pre-commit install   # once, after cloning

Update icons

To update to a new version of vscode-material-icon-theme:

uv run python scripts/build_icons.py --version 5.x.y

Requires npm on PATH. Regenerates mkdocs_treeview/icon_map.py and mkdocs_treeview/icons/. Commit the result.


Publishing

Build

uv build

Produces a wheel and sdist in dist/. Verify contents before publishing:

tar -tzf dist/*.tar.gz | sort
unzip -l dist/*.whl

Bump version

Edit version in pyproject.toml, then tag the release:

git tag v0.2.0

Publish to PyPI

UV_PUBLISH_TOKEN=pypi-... uv publish

Credits and license

SVG icons from vscode-material-icon-theme by Philipp Kief and contributors — MIT license.

Rendering approach inspired by asciidoctor-treeview by lask79 — MIT license.

This package is MIT licensed. See LICENSE.

Metadata

Release files for mkdocs-treeview 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mkdocs-treeview 0.3.0
File Size Uploaded Provenance Actions
mkdocs_treeview-0.3.0.tar.gz 371.8 kB Without provenance Details for mkdocs_treeview-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-treeview 0.3.0
File Interpreter ABI Platform Actions
mkdocs_treeview-0.3.0-py3-none-any.whl Python 3 none any Details for mkdocs_treeview-0.3.0-py3-none-any.whl

Total release size: 1.2 MB

Release files / mkdocs_treeview-0.3.0.tar.gz

Download URL mkdocs_treeview-0.3.0.tar.gz
Size 371.8 kB
Tags Source
SHA-256 checksum
How to use checksums
11eb340bb4f878278624626c9f93018cbd8daaee649eba18e943fc886a19f704
BLAKE2b-256 checksum
How to use checksums
54f42aeabb8eb07ca6c202e20a91b7f3eb62ac5eaf32e41c58da1d46c56d1392
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.16 {"installer":{"name":"uv","version":"0.12.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mkdocs_treeview-0.3.0-py3-none-any.whl

Download URL mkdocs_treeview-0.3.0-py3-none-any.whl
Size 838.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34d93cfaf4018fc84fb4607cd46cd28122de08693ad93d2e24f06f03e5825bb8
BLAKE2b-256 checksum
How to use checksums
07e857e0ac01bbce619732f2f3dcea649de4a15eaeb43daee26d229be69a171e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.16 {"installer":{"name":"uv","version":"0.12.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for mkdocs-treeview 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page