MkDocs Sidecode
mkdocs-sidecode is a separate MkDocs plugin project for interactive side-by-side documentation examples. It turns specially marked JavaScript fences into side-by-side examples with an editable editor, live output, optional console capture, named fragments, and deterministic fragment references.
The fence intentionally stays javascript so ordinary Markdown editors still highlight the source as JavaScript:
```javascript sidecode title="Counter Example" console=true height=420
//@HEADER counter_setup
container.innerHTML = '<button type="button">Count: 0</button>';
const button = container.querySelector('button');
let count = 0;
function setCount(value) {
count = value;
button.textContent = `Count: ${count}`;
}
//@BODY counter_body
setCount(3);
console.log('counter initialized');
```
Why javascript Fences Stay Intact
The plugin does not introduce a custom fence language such as sidecode. Instead it recognizes JavaScript fences whose info string also contains sidecode. That keeps normal editor highlighting intact while still giving MkDocs a reliable marker.
Supported forms:
```javascript sidecode title="Basic Example"```javascript sidecode```javascript {sidecode} title="Basic Example"
Fence options:
title="...": optional heading. Omit it for an untitled example.console=true: add a console panel and captureconsole.log,console.info,console.warn, andconsole.error.render=false console=true: show only the console panel. A hidden runtime container is still created so examples can safely usecontainer.width=720orwidth="42rem": set the example width. Bare numbers are treated as pixels.height=420orheight="32rem": set the editor/output height. Bare numbers are treated as pixels.autorun=false: disable initial and debounced edit-time execution. The Run button andCmd/Ctrl+Enterstill execute the example.autorun=true: default behavior. Edits rerun with a short debounce, and incomplete JavaScript is skipped until it parses.
Authoring Syntax
Supported directives inside the fence:
//@HEADER <name>: hidden setup code that runs before the editable body.//@BODY <name>: visible editable code shown in the editor.//@REF HEADER <name>: include a previously defined named header fragment on the same page.//@REF BODY <name>: include a previously defined named body fragment on the same page.
Example with references:
```javascript sidecode title="Counter Follow-up" console=true
//@REF HEADER counter_setup
//@REF BODY counter_body
//@BODY counter_followup
button.style.fontWeight = '700';
console.log(`current count: ${count}`);
```
Rules:
- Fragment names are page-local.
- Referenced fragments must already exist earlier on the page.
- Missing references fail the docs build clearly.
- Circular references are rejected clearly.
- Body-fragment propagation is one-way in this milestone: editing a source body updates dependent examples that reference that body fragment.
- Examples with
REFdirectives show a small "Uses code from" strip above the editor with the imported HEADER/BODY fragment names.
Execution Model
Each interactive example receives:
container: the render target DOM node.consoleTarget: the console panel DOM node when enabled.context: an execution context with cleanup registration and an abort signal.registerCleanup(fn): helper for disposal hooks.
The runtime composes execution code in this order:
- Referenced header fragments.
- Local header fragment.
- Referenced body fragments.
- Local editable body fragment.
Header code is hidden by default. Body code is editable. Console capture uses a scoped console object inside the example module so async callbacks keep writing into the example console panel instead of depending on browser devtools.
Cleanup Model
Before rerunning an example, the runtime:
- Aborts the previous execution signal.
- Runs registered cleanup callbacks in reverse order.
- Clears render and console containers.
- Re-executes the current composed example.
This gives the docs runtime a standard cleanup contract for canvases, listeners, timers, and retained DOM nodes without forcing each author to reimplement boilerplate.
Local Development
Install Python and frontend dependencies:
cd mkdocs-sidecode
python -m pip install '.[dev]'
npm install
npm run build
Run tests:
pytest
npm test
Run the demo site:
mkdocs serve -f demo/mkdocs.yml
Build the package artifacts:
python -m build
MkDocs Material compatibility and theming:
mkdocs-sidecode uses standard MkDocs plugin hooks plus extra_css and extra_javascript asset registration, so it works with the built-in MkDocs theme and Material for MkDocs. Material's instant navigation is supported by listening for its document$ page-change event when it is present.
The stylesheet follows Material's data-md-color-scheme attribute for light/dark palettes and falls back to prefers-color-scheme outside Material. The editor uses the same CSS variables as the surrounding sidecode frame, so it tracks the active docs theme rather than forcing a fixed light UI.
MkDocs Integration
Add the plugin to your MkDocs config after installing it:
plugins:
- search
- sidecode:
import_map:
helios-web: ../vendor/helios/helios-web.es.js
helios-network: ../vendor/helios/helios-network.js
The plugin injects its own runtime script and styles when a page contains interactive example fences.
import_map lets examples show package-style ES imports while the runtime rewrites
those specifiers to local browser assets. Relative mapped paths resolve from
assets/mkdocs-sidecode/runtime.js, so ../vendor/... points at
assets/vendor/... in the built site.
Release
Recommended release flow:
- Run local verification:
npm test npm run build PYTHONPATH=src pytest mkdocs build -f demo/mkdocs.yml python -m build
- Commit and push
main. - Create a Git tag such as
v0.1.2and push it. - Let GitHub Actions publish the built package to PyPI through trusted publishing.
Initial GitHub setup:
- Create a GitHub repository for this directory.
- Push the
mainbranch. - In PyPI, create the
mkdocs-sidecodeproject and configure trusted publishing for the GitHub repository. - After that, each pushed
v*tag can publish automatically through.github/workflows/publish.yml.
Deploying docs that use this plugin:
pip install mkdocs-material mkdocs-sidecode
mkdocs build
mkdocs gh-deploy
For GitHub Pages through Actions, install mkdocs-sidecode in the docs workflow before running mkdocs build.
Metadata
Release files for mkdocs-sidecode 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mkdocs_sidecode-0.1.7.tar.gz | 212.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mkdocs_sidecode-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 395.9 kB
Release files / mkdocs_sidecode-0.1.7.tar.gz
| Download URL | mkdocs_sidecode-0.1.7.tar.gz |
|---|---|
| Size | 212.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed826399988f7958f039f69eedc15523faf6772b9297f96c01a719b8a703cbe3
|
|
BLAKE2b-256 checksum How to use checksums |
5defc43ddea6af43fada492fbca1654763fdde16f81f9e28f79eb1010d499e38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 26, 2026.
Transparency logRelease files / mkdocs_sidecode-0.1.7-py3-none-any.whl
| Download URL | mkdocs_sidecode-0.1.7-py3-none-any.whl |
|---|---|
| Size | 183.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
73eb1ef7cd61ca9e400ca8c8aadc0120a8c6def2b439313656edd065a5d5e9be
|
|
BLAKE2b-256 checksum How to use checksums |
f1231e517cb0704a523738243d18d0ec08300c16966637821766621f00d279b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 26, 2026.
Transparency log