Usage
mkdocs-material theme
Material for MkDocs 9.5.0 and newer shows the page authors at the bottom of each page. No setup is needed.
Material uses its own template. It shows only the author names, as mailto: links when show_email_address is true, and ignores show_contribution, href and the links and avatars of an authors_file. To use this plugin's output instead, override the partials/source-file.html partial:
- Copy
material/templates/partials/source-file.htmlfrom your installed mkdocs-material tooverrides/partials/source-file.html, next to yourdocsdirectory. - In the
render_authorsmacro, replace the contents of the<nav>element with{{ git_page_authors }}. - Set
custom_dir: overridesunderthemeinmkdocs.yml.
Markdown pages
The plugin replaces these tags in your Markdown pages:
{{ git_page_authors }}: the authors of the page, wrapped in<span class='git-page-authors git-authors'>.{{ git_site_authors }}: a list of the authors of all pages, wrapped in<span class='git-authors'>.
For example, {{ git_page_authors }} becomes:
<span class='git-page-authors git-authors'><a href="mailto:jane@abc.com">Jane Doe</a>, <a href="mailto:john@abc.com">John Doe</a></span>
Author names and links are HTML-escaped.
Lines that are not committed yet, including all lines of a new page that git does not track yet, are attributed to the author Not Committed Yet. Files ignored by git get no authors.
Theme templates
Templates can use the same git_page_authors and git_site_authors variables. For example, to show the authors below the content in the default mkdocs theme, copy its content.html to overrides/content.html, next to your docs directory, and add the authors below {{ page.content }}:
{{ page.content }}
{% if git_page_authors %}
<p><small>Authors: {{ git_page_authors }}</small></p>
{% endif %}
Then set custom_dir: overrides under theme in mkdocs.yml.
For full control, use the git_info variable:
{
"page_authors": [
{
"name": "Jane Doe",
"email": "jane@abc.com",
"last_datetime": "Mon Feb 24 17:49:14 2020 +0100", # last change to this page
"lines": 200, # lines on this page
"lines_all_pages": 900,
"contribution": "40.0%", # share of this page
"contribution_all_pages": "35.5%",
"url": "https://github.com/jane", # see below
"avatar": "https://github.com/jane.png", # or None
},
...
],
"site_authors": [...], # authors of all pages, same fields
}
In site_authors too, last_datetime, lines and contribution describe the current page. Use lines_all_pages and contribution_all_pages for the whole site. On a generated page without a source file, lines and contribution are site-wide and last_datetime is None. For example:
{% if git_info %}
{% for author in git_info.page_authors %}
<a href="mailto:{{ author.email }}">{{ author.name }}</a> ({{ author.contribution }})
{% endfor %}
{% endif %}
url is the url from the authors_file. Without one, it is the href link when show_email_address is true, and None otherwise. avatar is the avatar from the authors_file, or None.
Avatars and profile links
The plugin's own HTML has no avatars. To show them in mkdocs-material, set them in an authors_file and replace docs/overrides/partials/source-file.html (see mkdocs-material theme) with:
{% if git_info %}
<aside class="md-source-file">
<nav>
{% for author in git_info.page_authors %}
{% if author.avatar %}
<a href="{{ (author.url or '#') | e }}" class="md-author" title="{{ author.name | e }}">
<img src="{{ author.avatar | e }}" alt="{{ author.name | e }}">
</a>
{% elif author.url %}
<a href="{{ author.url | e }}">{{ author.name | e }}</a>
{% else %}
{{ author.name | e }}
{% endif %}
{% endfor %}
</nav>
</aside>
{% endif %}
This replaces the whole partial, so it leaves out the dates that other plugins add there. MkDocs templates do not escape values, hence | e.
Page metadata
The plugin also adds the page authors to page.meta, so other plugins (such as mkdocs-macros-plugin) and templates can use them:
page.meta.git_authors: the same list asgit_info.page_authors.page.meta.git_authors_names: the author names as one string, for exampleJane Doe, John Doe, in the order the plugin shows them.
Both key names are reserved. Values already set, in the front matter or by mkdocs-material's meta plugin, are kept. Excluded pages get no keys. When no git history is available (see fallback_to_empty), and on generated pages without a source file, the values are an empty list and an empty string. To leave page.meta alone, set add_page_meta: false.
For example, to add an author meta tag, put this in overrides/main.html, next to your docs directory, and set custom_dir: overrides under theme:
{% extends "base.html" %}
{% block extrahead %}
{{ super() }}
{% if page and page.meta.git_authors_names %}
<meta name="author" content="{{ page.meta.git_authors_names | e }}">
{% endif %}
{% endblock %}
The names are plain text, so | e escapes them. Some themes, such as mkdocs-material, render the 404 page without a page.
Generated pages
Pages generated by another plugin have no git history of their own. If the generating plugin points the page's edit link at a source file, the page gets the authors of that file instead.
With mkdocs-gen-files, for example in the mkdocstrings API docs recipe, call set_edit_path:
mkdocs_gen_files.set_edit_path("reference/mod.md", "../src/pkg/mod.py")
The edit path can be relative to the docs_dir or to the repository root. Authors are counted for the whole source file, not only for the lines that end up on the page. Generated pages without such a source file get no page authors.