Skip to content

Add theme specific configuration. #1164

@waylan

Description

@waylan

Currently, in addition to building (Markdown) pages via main.html, we have hardcoded provisions for theme templates to build 404.html, search.html, and sitemap.xml. However, there is no way for a theme to provide any additional pages (sitemap.html or Sphinx's genindex.html).

We do have the extra_templates config setting, but that specifically assumes the template files are in the docs_dir, not at a path in the Jinja environment (they go through mkdocs.commands.build.build_extra_templates() rather than mkdocs.commands.build.build_template()).

In the work on Plugins (#206), a plugin can alter the config and add an additional directory to the Jinja Environment. That same plugin can also add a hook which explicitly calls mkdocs.commands.build.build_template() with a template in its template dir (that's how I implemented search as a plugin). However, themes have no way to alter the config.

To allow something like this, we need some way to allow configuration for themes. I see a few possibilities:

  1. The theme could include a config file which defines specific variables. For example an extra_templates variable could include a list of additional templates to pass through build_template. As prior art, Sphinx themes provide for a config.ini file in their root dir. We might want to use a .yml file instead to match our config file format. Or we could use Python in the required __init__.py file.

  2. We could allow nested config settings in mkdocs.yml. Following existing conventions, perhaps something like:

    theme:
        themename:
            - extra_templates: [sitemap.html]
            - include_sidebar: false
  3. A combination of the two. The theme author could define some defaults and the user could override them. This would also make a great way for themes to define optional behavior (include_sidebar which defaults to True but the user can set to False).

Yes, we have the extra config setting. While that is already used by third party themes to enable/disable theme features, the theme has to provide for the setting not being defined at all. And extra does not provide a way for a theme to pass more templates through build_template.

If we just want to allow a theme to pass more pages through build_template, then option 1 makes the most sense to me. But personally, I would prefer option 3. Option 2 would work, but the user would need to explicitly set each setting before it would be defined at all.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions