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

Option to set a own date format separate from the locale #82

Closed Andre601 closed 2 years ago

Andre601 commented 2 years ago

It is surprising that there isn't a setting to set your own date format to use.

The only current way is to use the locale option which has unwanted side effects such as altering the displayed name of the month.

The reason why there should be such an option is the simple fact, that not everyone is actually using the same format for a date commonly used in that language.

As an example am I using English as my site's primary language, while using the german date format (01. January 2022) over the English one (January 01, 2022). This causes an issue where the plugin is using the English format while I would like to use the german one instead. The issue here is that changing the locale to de would change January to Januar which is in conflict with my site's main language.

I would really appreciate it if such a setting could be added.

timvink commented 2 years ago

It is surprising that there isn't a setting to set your own date format to use.

I guess it is. Maybe it's what happens when a plugin organically grows quite a bit.. I would probably design it differently if I would re-write it from scratch now.

The reason why there should be such an option is the simple fact, that not everyone is actually using the same format for a date commonly used in that language.

It makes sense, so let's design it :)

The format can be the format for strftime (cheatsheet), so you can use a format like %d. %B %Y (for 01. January 2022).

The format would apply to type: date, type: datetime, type: iso_date, type: iso_datetime, basically everything except timeago.

We could add a another type, name it custom, and add an extra option called custom_format. It can default to something simple like %Y-%m-%d (the only true date format, see https://xkcd.com/1179/ ;))

That way, we can also make the custom type available as extra variables available for overriding themes.

I am also planning on adding support for overriding settings on a per-page basis, using YAML frontmatter, to support use-cases like #73. You should then be able to override custom_format to change the format for a given page.

Thoughts?

Andre601 commented 2 years ago

That sounds good.

I would be fine with a custom type with an extra setting... Tho, maybe to save effort, how about just have that separate option and if it is present, just override the type as a whole?

timvink commented 2 years ago

Because then you can do this:

plugins:
  - git-revision-date-localized:
      type: timeago
      custom_format: "%Y"

And on a specific, individual page, choose to change the type:

---
git-revision-date-localized--type: custom
---

# My page

I'll pick this up next time I find a chunk of time to work on the plugin :)

Andre601 commented 2 years ago

Because then you can do this:

plugins:
  - git-revision-date-localized:
      type: timeago
      custom_format: "%Y"

And on a specific, individual page, choose to change the type:

---
git-revision-date-localized--type: custom
---

# My page

I'll pick this up next time I find a chunk of time to work on the plugin :)

Well... In that case could you just provide the custom date directly to the front matter for the same effect I would say.

Andre601 commented 2 years ago

I'm not sure if that actually works?

I have this PR from Dependabot open that updates the plugin: https://github.com/Andre601/blog/pull/45

And I also updated the configuration within that branch itself: https://github.com/Andre601/blog/blob/dependabot/pip/mkdocs-git-revision-date-localized-plugin-1.0.0/mkdocs.yml#L172-L174 https://github.com/Andre601/blog/blob/dependabot/pip/mkdocs-git-revision-date-localized-plugin-1.0.0/mkdocs.yml#L224 https://github.com/Andre601/blog/blob/dependabot/pip/mkdocs-git-revision-date-localized-plugin-1.0.0/mkdocs.yml#L227 https://github.com/Andre601/blog/blob/dependabot/pip/mkdocs-git-revision-date-localized-plugin-1.0.0/theme/partials/source-file.html#L13

Yet, when I check the preview of the PR, is the date not updated and still shows in the format %B %d, %Y

Example page (Date at the bottom): https://www.andre601.ch/blog/posts/2020/12-20-in-defense-of-spigotmc/

I also cleared the cache on netlify to make sure it's not an issue caused by that...

timvink commented 2 years ago

Cool setup! Looks really good.

I cloned your repo, checked out the branch and previewed it locally:

image

That's the custom_format: '%d. %B %Y' configured in your mkdocs.yml. Changing it also changes the format correctly.

So I'm not sure what's going on tbh. I don't have any experience with netlify. I would guess indeed some kind of caching going on..

timvink commented 2 years ago

Did you manage to fix it? Does it work for you locally as well?

Andre601 commented 2 years ago

Was indeed a caching issue.