Skip to main content

mkdocs-external-code

CI

A MkDocs plugin: the @code: directive fetches a file's raw content from a GitHub repository (via the GitHub API) at build/serve time and renders it as a syntax-highlighted code block — so docs reference the real source instead of maintaining a copy.

Install

pip install mkdocs-external-code

Configure

plugins:
  - external-code:
      org: my-org          # required: GitHub org whose repos may be referenced
      default_ref: main    # optional, default "main"
      token_env: GH_READ_TOKEN  # optional, default "GH_READ_TOKEN"

Usage

@code:bash my-org/my-repo/tools/deploy.sh
@code:yaml my-org/my-repo@a1b2c3d/values.yaml   # pin a ref with @<sha>
@code:my-org/my-repo/app.py                     # language omitted -> inferred

Rules:

  • Only the configured org is allowed; other orgs fail the build with a clear error.
  • The language name after @code: is optional — it is inferred from the file extension (.sh → bash, .hcl → hcl, .py → python, Dockerfile → dockerfile, ...) and defaults to text. Omitting the language needs no space: @code:my-org/... and @code: my-org/... both work.
  • @ref is optional: omitted uses default_ref (or the EXTERNAL_DEFAULT_REF env var if set). Pin an exact version with @<commit-sha> (refs containing / are not supported).
  • Directives inside code fences or inline code are ignored (so you can document the syntax in a fenced block without executing it).

Access to private repos

The plugin reads the token for token_env in this order:

  1. the environment variable (or a .env file — the plugin loads it itself; the directory of your mkdocs.yml is checked first, then the current working directory, and existing variables are never overridden);
  2. if unset, requests are anonymous (public repos only).

A GitHub Personal Access Token (prefer fine-grained) with Contents: Read on each referenced repo is enough. In GitHub Actions, inject it explicitly:

- run: mkdocs build
  env:
    GH_READ_TOKEN: ${{ secrets.REPO_READ_PAT }}

Note: the default GITHUB_TOKEN only covers the repository running the workflow; it cannot read other private repos.

Limitations

  • Only UTF-8 text files are supported — binary or non-UTF-8 content fails the build with a clear error.
  • Files are fetched through the GitHub Contents API, which caps file size at 1 MB; larger files fail with a clear error (use a raw.githubusercontent.com URL form if you need them).
  • Anonymous requests are rate-limited by GitHub (60/hour per IP); pass a token to raise the limit.
  • Fetched content is cached for the process lifetime — during mkdocs serve, restart the server to pick up changed remote content.
  • @ref must not contain / (e.g. branch names like feature/x); pin a commit SHA instead.
  • Fenced code blocks and inline code are detected; indented (4-space) code blocks are not — use a fenced block to show the directive syntax literally.

Development

python -m venv .venv && . .venv/bin/activate
pip install -e .
python -m unittest discover -s tests -v
ruff check . && ruff format --check .

Releasing

  1. Bump the version in pyproject.toml and mkdocs_external_code/__init__.py.
  2. Publish a GitHub release with tag v<version>.
  3. The Release workflow builds the sdist/wheel and uploads it to PyPI via trusted publishing. Configure the PyPI trusted publisher once (owner calmzhu, repository mkdocs-external-code, workflow release.yml, environment pypi) — no API token secret is required.

License

MIT

Metadata

Release files for mkdocs-external-code 0.1.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-external-code 0.1.0
File Size Uploaded Provenance
mkdocs_external_code-0.1.0.tar.gz 10.1 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-external-code 0.1.0
File Interpreter ABI Platform
mkdocs_external_code-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / mkdocs_external_code-0.1.0.tar.gz

Download URL mkdocs_external_code-0.1.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
44805ff6f5db2b8c1b4dbda406dbc4f712c8a9d86f7083562d8262af260277fa
BLAKE2b-256 checksum
How to use checksums
8e1a76322f13c4599765b37dc3b59380075148c0816ccc2ad9e87ad734e32c93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 19, 2026.

Transparency log

Release files / mkdocs_external_code-0.1.0-py3-none-any.whl

Download URL mkdocs_external_code-0.1.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12ca8c716158902e8379d9724ba3883d1b8add9fd3d1f7400e392dbeb46961bb
BLAKE2b-256 checksum
How to use checksums
66cf77f27a910426ceeb7bb99af21900199e52af4ef48074cbec31cff662b16a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 19, 2026.

Transparency log

Security for mkdocs-external-code 0.1.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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mkdocs_external_code-0.1.0.tar.gz

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 Oct 19, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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