timvink / mkdocs-git-revision-date-localized-plugin

MkDocs plugin to add a last updated date to your site pages
https://timvink.github.io/mkdocs-git-revision-date-localized-plugin/index.html
MIT License
198 stars 40 forks source link

Unable to display `raw_timeago` value #81

Closed Andre601 closed 2 years ago

Andre601 commented 2 years ago

My goal is to display both the last update and creation date as <day>. <month> <year> <h>:<m>:<s> (<relative time>)

For example, a page created on the 13th of June 2021 and last updated on the 18th of January 2022 should display something like this:

Last updated: 18. January 2022 00:00:01 (1 month ago)
Created: 13. June 2021 00:00:01 (7 months ago)

When I however use the {{ page.meta.git_creation_date_localized_raw_timeago }} value, as mentioned by the docs, inside the partial override does this end up in an empty parenthesis (()) being displayed.

I'm not sure if I'm doing anything wrong in particular, so feedback is appreciated.

Here is the current source-file.html override in its entirety:

{% import "partials/language.html" as lang with context %}

<hr />
<div class="md-source-file">
  <small>

    <!-- mkdocs-git-revision-date-localized-plugin -->
    {% if page.meta.git_revision_date_localized %}
      <span class="twemoji">
        {% include ".icons/octicons/pencil-24.svg" %}
      </span>
      {{ lang.t("source.file.date.updated") }}:
      {{ page.meta.git_revision_date_localized_raw_datetime }} ({{ page.meta.git_revision_date_localized_raw_timeago }})
      {% if page.meta.git_creation_date_localized %}
        <br />
        <span class="twemoji">
          {% include ".icons/octicons/checklist-24.svg" %}
        </span>
        {{ lang.t("source.file.date.created") }}:
        {{ page.meta.git_creation_date_localized_raw_datetime }} ({{ page.meta.git_creation_date_localized_raw_timeago }})
      {% endif %}

    <!-- mkdocs-git-revision-date-plugin -->
    {% elif page.meta.revision_date %}
      <span class="twemoji">
        {% include ".icons/octicons/pencil-24.svg" %}
      </span>
      {{ lang.t("source.file.date.updated") }}:
      {{ page.meta.revision_date }}
    {% endif %}
  </small>
</div>
Andre601 commented 2 years ago

Small update. Looking at the generated HTML source, does it seem like that {{page.meta.git_revision_date_localized_raw_timeago }} and its creation counterpart are replaces with a <span> element having the timeago class applied alongside a datetime and locale setting. The span itself is completely empty.

Andre601 commented 2 years ago

I found a temporary workaround for this weird situation.

I changed type in the mkdocs.yml to timeago and updated the above lines to {{ page.meta.git_revision_date_localized_raw_datetime }} ({{ page.meta.git_revision_date_localized }})


My personal guess as to why this acts so weird is the fact that the timeago.js isn't added by default, which causes this unwanted behaviour. Maybe a setting to manually enable this in the future would be useful?

timvink commented 2 years ago

Hi @Andre601. You figured it out. timeago.js dependency is only added when type: timeago is set. The empty <span> is correct.. the javascript library will populate it.

I will update the documentation with instructions to use extra_javascript in mkdocs.yml.

Thanks for reporting!