Skip to main content

MkDocs plugin for interactive side-by-side JavaScript documentation examples.

Project description

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
//@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} title="Basic Example"

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.

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:

  1. Referenced header fragments.
  2. Local header fragment.
  3. Referenced body fragments.
  4. 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:

  1. Aborts the previous execution signal.
  2. Runs registered cleanup callbacks in reverse order.
  3. Clears render and console containers.
  4. 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:

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.

MkDocs Integration

Add the plugin to your MkDocs config after installing it:

plugins:
  - search
  - sidecode

The plugin injects its own runtime script and styles when a page contains interactive example fences.

Release

Recommended release flow:

  1. Run local verification:
    npm test
    npm run build
    PYTHONPATH=src pytest
    mkdocs build -f demo/mkdocs.yml
    python -m build
    
  2. Commit and push main.
  3. Create a Git tag such as v0.1.1 and push it.
  4. Let GitHub Actions publish the built package to PyPI through trusted publishing.

Initial GitHub setup:

  • Create a GitHub repository for this directory.
  • Push the main branch.
  • In PyPI, create the mkdocs-sidecode project 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs_sidecode-0.1.1.tar.gz (205.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mkdocs_sidecode-0.1.1-py3-none-any.whl (179.4 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs_sidecode-0.1.1.tar.gz.

File metadata

  • Download URL: mkdocs_sidecode-0.1.1.tar.gz
  • Upload date:
  • Size: 205.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mkdocs_sidecode-0.1.1.tar.gz
Algorithm Hash digest
SHA256 65d0a7c538d24a2cc9796f2b9d7f3e8b08baf7f93183129fba9978c0e76e682c
MD5 0afa049a27f541e6b59d9c3e4feaf7ca
BLAKE2b-256 30406a292b66225f1a8a505dddd67f59b300292f9acd862d867745544d30586e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mkdocs_sidecode-0.1.1.tar.gz:

Publisher: publish.yml on filipinascimento/mkdocs-sidecode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mkdocs_sidecode-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mkdocs_sidecode-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 179.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mkdocs_sidecode-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 59a140eb394dba134df9ca0235e629b70850a7843de00eb80e6dc5d2e95ac3f3
MD5 56a7b924b9f4164fef4b8a2c2f0b955b
BLAKE2b-256 9fe6b9bb051eddfb57c8d4b37b73d3d9393fa2b68d716fbb82fe4c4d7d6547e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for mkdocs_sidecode-0.1.1-py3-none-any.whl:

Publisher: publish.yml on filipinascimento/mkdocs-sidecode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page