Skip to main content

Create a page that contains all of your listings and/or a search function for listings

Project description

mkdocs-extract-listings-plugin

A small plugin to extract all your listings and put them in a single page. It can also generate a search function of code listings with different search methods (fuzzy match, substring, contains words).

Demo

You can try out the demo at https://mkdocs-extract-listings-plugin.six-two.dev. It is configured to offer both the search and all listings pages an uses the plugin with some common MkDocs themes (mkdocs, readthedocs, and material). The source for this demo is also in this repo (mkdocs.yml, docs/ and build.sh).

Setup

  1. Install the plugin using pip:

    pip install mkdocs-extract-listings-plugin
    
  2. Add the plugin to your mkdocs.yml:

    plugins:
    - search
    - extract_listings
    

    If you have no plugins entry in your config file yet, you'll likely also want to add the search plugin. MkDocs enables it by default if there is no plugins entry set.

    More information about plugins in the MkDocs documentation.

  3. Configure a page with all listings, a page with listing search, or both (see below).

  4. Optional: To properly detect which language a listing belongs to, you may have to add the following to your mkdocs.yml as described in the Material for MkDocs page on code blocks:

    markdown_extensions:
    - pymdownx.highlight:
        anchor_linenums: true
        line_spans: __span
        pygments_lang_class: true
    - pymdownx.inlinehilite
    - pymdownx.snippets
    - pymdownx.superfences
    

Listing page

Add a Markdown file for the page that will be filled with all the listings. In that file add the placeholder where the listings should be inserted. Then reference that file and specify the placeholder like this in your mkdocs.yml:

plugins:
- extract_listings:
    listings_file: listings.md
    placeholder: PLACEHOLDER_LISTINGS_PLUGIN

Listing search

  1. Create a page, which should contain the search function.

  2. Add a tag where the search elements should be inserted and load the search script:

    <div id="listing-extract-search"></div>
    <script src="/listing-search.js">
    
  3. Specify where you want the plugin to write the script file to. This should match the path you used in the previous step. In mkdocs.yml:

    plugins:
    - extract_listings:
        javascript_search_file: listing-search.js
    

Configuration

You can configure the plugin like this:

plugins:
- extract_listings:
    listings_file: listings.md
    placeholder: PLACEHOLDER_LISTINGS_PLUGIN
    javascript_search_file: listing-search.js
    default_css: true
    offline: false
    exclude_language_list:
    - python
    - bash

default_css

default_css determines, whether the search JavaScript should also load the default styling for the search dialog and results. By default it is set to true, but you can set it to false if the theme you use does not work well with the style. You should then define your own custom style and include it on the search page. Also has some effect on the all listings page.

listings_file

listings_file is expected to contain the relative path to the Markdown file, where the listings should be written to. If the file does not exist, an error will be raised during the build process. The default value is empty.

placeholder

The value for placeholder will be searched in the file referenced by listings_file and be replaced with the list of all listings.

javascript_search_file

The JavaScript code for the search function will be written to this path. The default value is empty, meaning that neither the JSOn file nor the JavaScript are generated.

offline

When set to true the listing data is included into the JavaScript file. This way it can be used even when the site if opened from the file system (via a file:// URL). The disadvantage is that the loading of the script will take longer and the data is not loaded asynchronously, so the loading of the page via the Internet will be delayed. Because of that it is set to false by default.

exclude_language_list

Ignore any listings that are in the given languages. They will not appear on the all listings page or in the listings search.

Search mode

You can set the search mode via the data-searchmode attribute:

<div id="listing-extract-search" data-searchmode="substr"></div>

To see all current valid values inspect the search type dropdown menu using your browser's developer tools. The data in the value attribute is the value you should put in the attribute:

Firefox's Developer tools

Alternatively you can put in a random value and will receive an warning message in the developer tools, that also lists the valid values (but without descriptions).

Changelog

Version 0.1.3

  • Added exclude_language_list option which excludes listings in the given languages from the search
  • Correctly recognize mermaid language when using MkDocs for Material
  • Hide the language selector when only one language exists

Version 0.1.2

  • Added a dropdown to filter snippets by language.

Version 0.1.1

  • Fix: Links incorrect if search page is not in the root directory.

Version 0.1.0

  • The plugin should now be able to work when served from file:// URLs:
    • Search JSON can be inlined to the script via offline: true setting.
    • Use relative links on the listings_file page.
  • Improved the search code:
    • Allow users to specify which matching mode the search uses by default.
    • Added mode Contains words (case insensitive).
  • Fixed crash when using use_directory_urls: false and not setting listings_file.

Version 0.0.4

  • Added styling for the search page and the option default_css to disable it.
  • Fixed bug: URL for index pages starts with //

Version 0.0.3

  • Added snippet search JavaScript and JSON file.
  • Changed default for listings_file to empty string.

Version 0.0.2

  • Fixed Unknown path being shown on with different themes (readthedocs and material)

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_extract_listings_plugin-0.1.3.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file mkdocs_extract_listings_plugin-0.1.3.tar.gz.

File metadata

File hashes

Hashes for mkdocs_extract_listings_plugin-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ab9c2da19118279789e1dc2833251b094ce2fd7076dba46d0d5c9fa4670f1baf
MD5 672756e54a71e641c7a7f235a4df6c6e
BLAKE2b-256 d9b1fb4683101ab45df8f543e77eabfba6b06baa847466d721d9478de9a4cedf

See more details on using hashes here.

File details

Details for the file mkdocs_extract_listings_plugin-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_extract_listings_plugin-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d0ba600a28564dafa4e47045f7f9af09ad3b6d21716b164199ad5dcef6f725b6
MD5 e92c66834154e7261073d4d541a9e417
BLAKE2b-256 69b4af8f35f8e8e8f0bb2c73e4e7751f08cbf1cb799b8d036266b078191df8be

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