|
| 1 | +--[[ |
| 2 | +Quarto filter to set date-modified from git commit history. |
| 3 | +
|
| 4 | +This filter runs during Quarto rendering and sets the date-modified |
| 5 | +metadata field based on the last git commit for each document. |
| 6 | +]]-- |
| 7 | + |
| 8 | +local function get_git_date(file_path) |
| 9 | + -- Get the last commit date for the file |
| 10 | + local command = 'git log -1 --format=%cI -- "' .. file_path .. '"' |
| 11 | + local handle = io.popen(command) |
| 12 | + local result = handle:read("*a") |
| 13 | + handle:close() |
| 14 | + |
| 15 | + -- Trim whitespace |
| 16 | + result = result:gsub("^%s*(.-)%s*$", "%1") |
| 17 | + |
| 18 | + if result ~= "" then |
| 19 | + -- Parse ISO date |
| 20 | + local year, month, day = result:match("(%d%d%d%d)%-(%d%d)%-(%d%d)") |
| 21 | + if year and month and day then |
| 22 | + -- Format as "MMMM D, YYYY" (e.g., "October 14, 2025") |
| 23 | + local months = { |
| 24 | + "January", "February", "March", "April", "May", "June", |
| 25 | + "July", "August", "September", "October", "November", "December" |
| 26 | + } |
| 27 | + local month_name = months[tonumber(month)] |
| 28 | + local day_num = tonumber(day) |
| 29 | + |
| 30 | + return month_name .. " " .. day_num .. ", " .. year |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + return nil |
| 35 | +end |
| 36 | + |
| 37 | +-- This runs after Quarto processes metadata, so we need to set date-modified |
| 38 | +-- regardless of what's already there |
| 39 | +function Meta(meta) |
| 40 | + -- Get the input file path |
| 41 | + local input_file = quarto.doc.input_file |
| 42 | + |
| 43 | + if not input_file then |
| 44 | + return meta |
| 45 | + end |
| 46 | + |
| 47 | + -- Get git date for this file |
| 48 | + local git_date = get_git_date(input_file) |
| 49 | + |
| 50 | + if git_date then |
| 51 | + -- Set date-modified to the git commit date |
| 52 | + -- Use MetaString so Quarto can format it |
| 53 | + meta["date-modified"] = pandoc.MetaString(git_date) |
| 54 | + else |
| 55 | + -- Fallback: use current date if no git history |
| 56 | + meta["date-modified"] = pandoc.MetaString(os.date("%Y-%m-%d")) |
| 57 | + end |
| 58 | + |
| 59 | + return meta |
| 60 | +end |
0 commit comments