fralau / mkdocs-macros-plugin

Create richer and more beautiful pages in MkDocs, by using variables and calls to macros in the markdown code.
https://mkdocs-macros-plugin.readthedocs.io
Other
335 stars 51 forks source link

Git information not showing page specific info #147

Closed smholvoet closed 1 year ago

smholvoet commented 2 years ago

I'm trying to make use of the Git info plugin, but I keep getting the latest commit of the entire repo instead of the latest commit of that specific page.

Last modified {{ git.date.strftime("%B %d, %Y at %H:%M %Z") }}: [{{ git.message }} ({{ git.short_commit }})](https://dev.azure.com/org/project/_git/repo/commit/{{ git.commit }})

Version I'm using:

mkdocs                                    1.4.1
mkdocs-macros-plugin                      0.7.0
mkdocs-material                           8.5.7

On a sidenote, I'd like to display this info by default on every page. I'm trying to use an override block to do this which ends up in a mkdocs build error:

overrides/main.html:

{% extends "base.html" %}

{% block content %}
  {{ super() }}

  {% if git.status %}
  Last modified {{ git.date.strftime("%B %d, %Y at %H:%M %Z") }}: [{{ git.message }} (`{{ git.short_commit }}`)](https://dev.azure.com/org/project/_git/repo/commit/{{ git.commit }})
  {% endif %}
{% endblock %}

{% block next_prev %}{% endblock %}

Results in jinja2.exceptions.UndefinedError: 'git' is undefined

I now have the shortcodes (at the top of this issue) at the bottom of every Markdown page.

github-actions[bot] commented 2 years ago

Welcome to this project and thank you!' first issue

fralau commented 2 years ago

For the first part: yes, the macro git.date refers to the last commit for the whole repo. Getting the commit date for a specific page is non-trivial (one needs to formulate the exact path: git log -1 --format="%ad" -- path/to/file). My best suggestion, at the moment, would be to write a specific macro for it.

For the second question, it is important to remember that the jinja2 engine for macros is not the same as for the override of templates. To makes things clear, the jinja2 engine for templates is strictly for HTML, whole the macros jinja2 engine works with markdown (HTML allowed).