Assuming that this isn’t already possible (I can’t find anything in the docs) would this be something that has to be implemented in Go? Adding a modified
date variable on save to the front matter would be useful.
Setting it “on save” should be the responsibility of the editor. There is talk about building a REST api in another thread; this would be a natural fit there,
We’re required to have a “last updated” message on all of our content. The following node script will inject a lastUpdate
property into the front matter of a file on change
. If you’re using a build tool like gulp, grunt or npm scripts this will automate the process of adding lastUpdate to any file that you modify that contains front-matter.
(function () {
'use strict';
var chokidar = require('chokidar');
var matter = require('gray-matter');
var replace = require('replace-in-file');
var moment = require('moment');
var yaml = require('yamljs');
// If you want to use something other than lastUpdate change this var
var propertyName = 'lastUpdate';
// Where is your content located?
chokidar.watch('content/*.{md,html}').on('change', updateFrontMatter);
function updateFrontMatter(path) {
var regex = /^---[\s\S]*?---/;
var fm = matter.read(path);
fm.data[propertyName] = moment().format('MMMM-DD-YYYY');
var output = '---\n' + yaml.stringify(fm.data) + '---';
replace({
files: path,
replace: regex,
with: output
}, function (err, files) {
if (err) return console.error(err);
console.log('Modified files: ', files.join(', '));
});
}
})();
.Page.Lastmod.Format "20060102-15:04:05.000"
is now actually working in 0.16.1.
-
Does
Page.Now
return current os time instead of the last modified time of the markdown file? -
I am using
{{ .Page.Lastmod.Month }} {{ .Page.Lastmod.Day }}, {{ .Page.Lastmod.Year }}
now to display a last modified date in my post. Is there a smarter or shorter way?
- Yes.
- Use .Page.Lastmod.Format
Actually .Page.LastMod.Format
is still giving the same date just as the markdown’s created date, which is not the last modified time of the markdown file.
It is the value of the Lastmod front matter param (or Date if Lastmod not set). This will in most cases be different than the modified time of the file itself. This is discussed in another recent thread.
So the Lastmod param has to be manually set? I thought it would automatically pick up the last modified time of the markdown file from the OS.
As I noted elsewhere, that would in most cases be misleading.
How can I check whether a Lastmod
is available in the front matter in the template?
{{ if isset .Params "Lastmod" }}
Last Modified: {{ .Lastmod.Format "Jan 2, 2006" }}
{{ end }}
this method seems to always return false even a Lastmod
is set.
Lastmod is a member variable on Page, so
{{ if not .Lastmod.IsZero }}
Will be correct, but remember that Lastmod will get its value from Date if not set, so it will in most cases have a value.
I finally found what I needed: {{ if not (eq .Lastmod .Date) }}
. This way if lastmod is not specifically set to be different. The two will be equal.
Thanks for all the prompt replies.
Here is that thread: How to use modified timestamp from file as .Lastmod? - support - HUGO
This test is very useful even though .Lastmod always has a value.
Most of the time you will display the last modified date only if needed and is this case, its because it is different than .Date
You don’t have to use “not” though there is a “ne” function that does just that:
{{ if ne .Lastmod .Date }}
hi @rhewitt, I have a similar requirment to show last changed /last updated on all content files. Do you have a github repo. I am facing some errors related to ‘fs’. I added target: node
in webpack. Still no luck. Can you please help. Thanks.
My complete post is here - Last modified .gitinfo from git-submodule
Tq
yq
now supports front matter processing if you’re using yaml front matter:
now=$(date) yq e --front-matter="process" '.lastMod = strenv(now)' file
See GitHub - mikefarah/yq: yq is a portable command-line YAML processor