mkdocs-external-code
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
Requires Python 3.12–3.15.
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
orgis 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 totext. Omitting the language needs no space:@code:my-org/...and@code: my-org/...both work. @refis optional: omitted usesdefault_ref(or theEXTERNAL_DEFAULT_REFenv var if set). Pin an exact version with@<commit-sha>(refs containing/are not supported).- The generated block inherits the directive's indentation and is padded with
blank lines, so a directive may be nested in a list item or an admonition.
Indented fences need
pymdownx.superfences; without it the plugin emits the block at column 0 (still a code block, but outside the container). - 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:
- the environment variable (or a
.envfile — the plugin loads it itself; the directory of yourmkdocs.ymlis checked first, then the current working directory, and existing variables are never overridden); - 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.comURL 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. @refmust not contain/(e.g. branch names likefeature/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
Pushing a v* tag publishes that tag to PyPI automatically. The Release
workflow can also be run manually (workflow_dispatch) to publish an existing
tag again.
- Bump the version in
pyproject.tomlandmkdocs_external_code/__init__.py, commit and push. - Create and push the tag:
git tag -a v0.1.2 -m v0.1.2 && git push origin v0.1.2. This triggers the release; the workflow checks that the tag matches the package version, builds the sdist/wheel and publishes to PyPI via trusted publishing. - Manual fallback: GitHub → Actions → Release → Run workflow, enter the
tag (e.g.
v0.1.2) or leave it empty for the latestv*tag. - One-time PyPI setup: add a trusted publisher (pending publisher while the
project does not exist yet) with owner
calmzhu, repositorymkdocs-external-code, workflowrelease.ymland no environment name — no API token secret is required.
License
MIT
Metadata
Release files for mkdocs-external-code 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mkdocs_external_code-0.1.4.tar.gz | 11.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mkdocs_external_code-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.0 kB
Release files / mkdocs_external_code-0.1.4.tar.gz
| Download URL | mkdocs_external_code-0.1.4.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7cc41da5c25b3c4d0ee7c1730137bc1bdc6182a9a653efcec701002a3ef03dfa
|
|
BLAKE2b-256 checksum How to use checksums |
3fecba69c5e29640b5e3633f37caa5fd0029cf59d32af66692c1516bc07bf431
|
| 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 20, 2026.
Transparency logRelease files / mkdocs_external_code-0.1.4-py3-none-any.whl
| Download URL | mkdocs_external_code-0.1.4-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8884f22c8b23b7de5862340e13387e807582ed215e52313a40a07ed1e788e919
|
|
BLAKE2b-256 checksum How to use checksums |
4152544f17c45237c20bf608239cfb204c4ad10591ef0e9d81bc92cf98e23328
|
| 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 20, 2026.
Transparency logSecurity for mkdocs-external-code 0.1.4
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.
Attestation for mkdocs_external_code-0.1.4.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 20, 2026.
Transparency logA 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.