ojacques / mkdocs-git-committers-plugin-2

MkDocs plugin for displaying a list of GitHub (.com or Enterprise) contributors on each page. Encourages contributions to the documentation!
MIT License
60 stars 21 forks source link
mkdocs mkdocs-plugin

mkdocs-git-committers-plugin-2

MkDocs plugin for displaying a list of committers associated with a file in mkdocs. The plugin uses GitHub or GitLab API to fetch the list of contributors for each page.

🥳 NEW! Works with GitLab too!

For ease of use, this plugin is integrated in the material for mkdocs theme by Martin Donath. See Mkdocs material documentation.

Other MkDocs plugins that use information to fetch authors:

Setup

Install the plugin using pip:

pip install mkdocs-git-committers-plugin-2

Activate the plugin in mkdocs.yml:

For a repository hosted on GitHub:

plugins:
  - git-committers:
      repository: organization/repository

For a repository hosted on GitLab:

plugins:
  - git-committers:
      gitlab_repository: 12345678
      token: !ENV ["CI_JOB_TOKEN"]

For a repository hosted on GitLab, you need to provide a token so that the plugin can access the GitLab API. If the token is not set in mkdocs.yml it will be read from the MKDOCS_GIT_COMMITTERS_APIKEY environment variable.

For a repository hosted on GitHub, you can provide a token to increase the rate limit and go beyond the default 60 requests per hour per IP address. The plugin will make one request per mkdocs document. The token does not need any scope: uncheck everything when creating the GitHub Token at github.com/settings/personal-access-tokens/new, unless you access private repositories.

For private GitHub repositories, you only need to allow read-only access to Contents and Metadata on the target repository. This could be done by setting Read-only access of Permissions > Repository permissions > Contents.

Counting Contributors

Config

History

This is a fork from the original mkdocs-git-committers-plugin by @byrnereese.

I had to create this fork so that it could be uploaded and distributed through PyPi. The package has been renamed to mkdocs-git-committers-plugin-2.

This "v2" differs from the original by:

All of the above improves accuracy and performances.

Note: the plugin configuration in mkdocs.yml still uses the original git-committers sections.

Limitations

Usage

You have 2 options to use this plugin:

  1. Use Mkdocs material theme (see Mkdocs material documentation).
  2. Use the plugin directly in your template. See below.

Display Last Commit

In addition to displaying a list of committers for a file, you can also access the last commit date for a page if you want to display the date the file was last updated.

Template Code for last commit

<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
  <li class="last-updated-holder displayDate loading">
    <span class="last-updated-text">Last updated:</span>
    <time role="presentation" datetime="2018-10-25T00:00:00.000Z" data-article-date-source="ms.date">{% if last_commit_date %}{{ last_commit_date }}{% endif %}</time>
  </li>
</ul>

Display List of Committers

Avatar

The avatar of the contributors is provided by GitHub. It uses maximal resolution.

Template Code for avatars

{% block footer %}
<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
  <li class="contributors-holder">
    <span class="contributors-text">Contributors</span>
    <ul class="contributors" data-bi-name="contributors">
      {%- for user in committers -%}
      <li><a href="https://github.com/ojacques/mkdocs-git-committers-plugin-2/blob/master/{{ user.url }}" title="{{ user.name }}" data-bi-name="contributorprofile" target="_blank"><img src="https://github.com/ojacques/mkdocs-git-committers-plugin-2/raw/master/{{ user.avatar }}" alt="{{ user.name }}"></a></li>
      {%- endfor -%}
    </ul>
  </li>
</ul>
{% endblock %}

CSS

.metadata{
    list-style:none;
    padding:0;
    margin:0;
    margin-bottom: 15px;
    color: #999;
    font-size:0.85em;
}
.metadata.page-metadata .contributors-text{
    margin-right:5px
}
body[dir=rtl] .metadata.page-metadata .contributors-text{
    margin-right:0;
    margin-left:5px
}
.page-metadata .contributors{
    display:inline-block;
    list-style:none;
    margin:0!important;
    padding:0!important
}
.page-metadata .contributors li{
    display:inline-block;
    vertical-align:top;
    margin:0;
    padding:0
}

Javascript

    $( '.contributors img[data-src]' ).each( function() {
        src = $(this).attr("data-src");
        $(this).attr('src',src);
    });

More information about templates here.

More information about blocks here.

Acknowledgements

Thank you to the following contributors: