~jhn/pwd.re

ref: 10714a2bcbc3fb13261d21b782d7a15d47d282f4 pwd.re/plugins/sitemap.lua -rw-r--r-- 2.1 KiB
10714a2b — Johan Add sitemap plugin 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--
-- Sitemap generator
-- Version: 1.0.0
-- Author: johan@pwd.re
--
-- Based on https://git.sr.ht/~hristoast/hristoast/tree/master/item/plugins/sitemap.lua
-- but modified to fit my needs.
--
-- This plugin will generate a sitemap.xml file, getting the dates in the following order:
--   - Updated date
--   - Published date
--   - Last modified in git repo
--
--
--
-- Sample soupault.toml configuration
--
-- [custom_options]
--   base_url = "https://domain.tld"
--
-- [index]
--   index = true
--   force_indexing_path_regex = "(etc|writings)"   # Force inclusion of index.html files
--
-- [index.fields]
--   publish_date = { selector = ["span#publish-date"] }
--   updated_date = { selector = ["span#updated-date"] }
--
-- [widgets.sitemap]
--   widget = "sitemap"
--   sitemap_file = "sitemap.xml"
--
--

Plugin.require_version("2.4.0")

function getGitDate(file)
  local cmd = format("git log -n 1 --pretty=format:%%ad --date=short -- %s", file)
  return Sys.get_program_output(cmd)
end

local c = 1
local count = size(site_index)
local all_pages = {}

while (c <= count) do
  local p = site_index[c]
  local date = ""

  if p.updated_date then
    date = p.updated_date
  elseif p.publish_date then
    date = p.publish_date
  else
    date = getGitDate(p.page_file)
  end

  all_pages[c] = { url = p.url, date = date }
  c = c + 1
end

-- Force inclusion of the "main" index.html
all_pages[c] = { page = "/index.html", date = getGitDate("site/index.html") }

config["base_url"] = soupault_config["custom_options"]["base_url"]
config["all_pages"] = all_pages

local sitemap_template = [[
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for p in all_pages -%}
  {% if p.date %}
  <url>
    <loc>{{ base_url }}{{ p.url }}</loc>
    <lastmod>{{ p.date }}</lastmod>
  </url>
  {%- endif -%}
{%- endfor %}
</urlset>
]]

-- Write the sitemap file to disk using <build_dir>/<sitemap_file>
local sitemap = String.render_template(sitemap_template, config)
Sys.write_file(Sys.join_path(soupault_config["settings"]["build_dir"], config["sitemap_file"]), String.trim(sitemap))