Skip to main content

MkDocs plugin to extract navigation and load it asynchronously

Project description

MkDocs Navigation Async

MkDocs Navigation Async is an MkDocs plugin designed to extract navigation from one page of your site and load it asynchronously across the other pages. This helps reduce HTML duplication for large sites by keeping navigation in a separate file and injecting it via JavaScript. It also shows a loading spinner until the navigation is fully loaded.

Features

  • Extracts navigation from a single page and applies it to the rest of the site.
  • Reduces the HTML size of each page by dynamically loading the navigation via fetch.
  • Uses threads to speed up the removal of navigation from other pages.
  • Displays a loading spinner while the navigation is being fetched.

Installation

You can install the plugin via pip by adding the following line to your requirements.txt or installing it directly:

pip install mkdocs-nav-async

Or install it directly from your local development environment using the -e option:

pip install -e .

Usage

Once installed, configure the plugin in your mkdocs.yml configuration file:

plugins:
  - nav_async

You can also specify options like prettify, in order to prettify output html:

plugins:
  - nav_async:
      prettify: true
      minify: true

How it Works

The plugin extracts the navigation from the first page it processes and saves it as nav.html. Then it replaces the navigation in each page with a placeholder and loads the navigation asynchronously when the page is visited. A spinner is shown during the loading process.

Example mkdocs.yml:

site_name: "My Documentation"
theme:
  name: "material"

plugins:
  - nav_async:
      prettify: true # Optional, specify output html prettified
      minify: true # Optional, minify html output

extra_css:
  - css/custom.css # Optional, if you want to customize the spinner or styling

nav:
  - Home: index.md
  - About: about.md
  - Contact: contact.md

Debugging in Development

To debug the plugin locally during development, follow these steps:

  1. Clone or download the plugin to your local environment.

  2. Install it in "editable" mode using the following command in the plugin’s directory:

    pip install -e .
    

    This will install the plugin in development mode, allowing you to make changes without having to reinstall it each time.

  3. Run mkdocs serve in your project’s directory to serve your documentation locally. Any changes you make to the plugin will be immediately reflected when you reload the MkDocs server.

  4. Use Python's print() or a logger to print debugging information to the console.

Example Debugging Output

When running the plugin, you can expect to see output like this:

Taking nav from: /path/to/site/index.html
Spinner SVG copied to: /path/to/site/bars-rotate-fade.svg
Starting to clear navigation in pages using ThreadPool (50 files)...
Total execution time: 1.35 seconds

Customization

  • Spinner Icon: The default spinner icon is an SVG file (bars-rotate-fade.svg). You can replace this with your own by modifying the plugin or adding your own spinner.
  • Asynchronous Loading: The plugin uses fetch to load the navigation dynamically. You can customize the JavaScript if needed by modifying the insert_spinner_and_script function.

Performance Statistics

  • With this plugin using BeautifulSoup4, it processes each page in 0.8 seconds. In a 4000 files project, 53min.
  • With this plugin using lxml, it processes each page in 0.08 seconds. In a 4000 files project, 5min.
  • Using this plugin with on_post_page in a project with over 4000 files adds an additional 6 minutes to the build time compared to not using the plugin.
  • Using this plugin with on_post_build in a project with over 4000 files also adds an additional 6 minutes to the build time compared to not using the plugin.

License

This project is licensed under the MIT License.

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_nav_async-0.18.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

mkdocs_nav_async-0.18-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs_nav_async-0.18.tar.gz.

File metadata

  • Download URL: mkdocs_nav_async-0.18.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for mkdocs_nav_async-0.18.tar.gz
Algorithm Hash digest
SHA256 f7e78fab8922a37a6c24f280895a042c803977e9cb549e5429b7f0e685ec898c
MD5 f7656b8d7273d2d3f11c031265f9bd5d
BLAKE2b-256 b60ce3ccb4619883a4bbe6dbbe5ec7e9897013ab8aabbcce46a273a5d191af60

See more details on using hashes here.

File details

Details for the file mkdocs_nav_async-0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_nav_async-0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 1f67a7b25d8d07032ae20828e617df1bd2785be0d0d397a6e4b49dd89726593f
MD5 d18cfe06cfe92f82b67bced9dfa756f3
BLAKE2b-256 2f382af404429be0f99358404797a48e460927d56c2a0a587a29968a01cadef7

See more details on using hashes here.

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