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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mkdocs_nav_async-0.17.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.17.tar.gz
Algorithm Hash digest
SHA256 322198a28d2be6cd6c2856b600349174ee4295e84a37904b9ab941695e0b8867
MD5 d7300bd2b2b6a522a83f1e279dac6e17
BLAKE2b-256 b7f396ea2b6efeef18f13f99a460b0a4acf5506ac0a3ed6c1a3cceffd3620ab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mkdocs_nav_async-0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 7f2d605ca6658b3ddc2c00149e4e3238232e966569af3eef49caa356832713a9
MD5 d4b93e3fb18224a416e9644f2c5fe839
BLAKE2b-256 844113dc19425140a7d467bd96f6bd87fbb72e63ec1be58071cb255d45cb438f

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