~hristoast/hristoast

ref: 9e22a608d77e7b652643053b8fd5c069e9d6c4d7 hristoast/plugins/sitemap.lua -rw-r--r-- 1.2 KiB
9e22a608Hristos N. Triantafillou Don't use fancy multi-line comments 3 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
--
-- Sitemap generator, based on dmbaturin's atom generator.
--
-- Uses the list of pages contained in `site_index'.
--
-- Requires a "site_url" value as seen below:
--
-- [custom_options]
--   site_url = "https://domain.tld"
--
-- Sample widget configuration:
--
-- [widgets.sitemap]
--   widget = "sitemap"
--   sitemap_file = "sitemap.xml"
--

Plugin.require_version("2.2.0")

config["site_url"] = soupault_config["custom_options"]["site_url"]

local c = 1
local count = size(site_index)
local file = config["sitemap_file"]
local all_pages = {}

while (c <= count) do
  local p = site_index[c]
  all_pages[c] = p
  c = c + 1
end

config["all_pages"] = all_pages

-- From: https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap
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 -%}
  <url>
    <loc>{{ site_url }}{{ p.url }}</loc>
   <lastmod>{%- if p.date -%}{{ p.date }}{%- else -%}{{ p.git_timestamp }}{%- endif -%}</lastmod>
  </url>
{%- endfor -%}
</urlset>
]]
local sitemap = String.render_template(sitemap_template, config)

Sys.write_file(Sys.join_path(soupault_config["settings"]["build_dir"], file), String.trim(sitemap))