Skip to main content

mkdocs plugin to control the base_url, for use with mike (for multiple versions) and i18n.

Project description

mkdocs-modify-base-url

This mkdocs plugin is meant to be used with:

The code in mkdocs-material that renders the version selection dropdown in the header dynamically requests versions.json relative to the page's base_url. This works well normally, but when you try to add multiple languages, things start to get a bit tricky.

I've set up my project documentation inside my repo like this:

.
├─ config/
│    ├─ en/
│    │  └─ mkdocs.yml
│    ├─ nl/
│    │  └─ mkdocs.yml
│    ├─ .../
│
├─ content/
│    ├─ en/
│    │   ├─ index.md 
│    │   └─ ...
│    └─ nl/
│        ├─ index.md 
│        └─ ,,,
│
├─ generated/
│    ├─ branch
│    │  ├─ en/
│    │  └─ nl/
│    └─ index.html
│
└─ ...

Each language has its own mkdocs.yml. Each is configured like this (only showing relevant parts, using en as an example):

docs_dir: '../../content/en'
site_dir: '../../generated/branch/en'
theme:
  language: en
extra:
  version:
    provider: mike
  alternate:
  - name: English
    link: en
    lang: en

plugins:
- modify-base-url:
    prefix: '../'

All languages are individually generated into docs/generated/branch/$LANGUAGE

There is a separate mkdocs.yml for mike to support versioning. There is really only one key, required setting, that tells mike to use what we previously generated:

docs_dir: generated/branch

When you run mike deploy VERSION from within the docs directory, it combines all the generated documentation for all the languages into a single version. There is also a static index.html file in docs/generated/branch that redirects initial requests to a default language.

Given a URL such as docs.kcp.io/kcp/main/en, when visiting index.html, its base_url is normally . because we told mkdocs to generate directly into the en directory. What's unique about this situation is that mkdocs thinks that one level up from en should be where versions.json lives. But it's actually up one more level, at docs.kcp.io/kcp/versions.json because of our configuration.

And this is why this plugin exists. We need to adjust the base_url to move it up one more level, so instead of it being . for docs.kcp.io/kcp/main/en/index.html, it's actually ... Now, when mkdocs-material requests base_url/../versions.json, that becomes ../../versions.json, which is really docs.kcp.io/kcp/versions.json.

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-modify-base-url-0.0.2.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

mkdocs_modify_base_url-0.0.2-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

Supported by

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