~jhn/pwd.re

10714a2bcbc3fb13261d21b782d7a15d47d282f4 — Johan 7 months ago e21cc3c
Add sitemap plugin
3 files changed, 100 insertions(+), 1 deletions(-)

A plugins/sitemap.lua
M soupault.toml
M templates/main.html
A plugins/sitemap.lua => plugins/sitemap.lua +84 -0
@@ 0,0 1,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))

M soupault.toml => soupault.toml +15 -0
@@ 19,6 19,17 @@
  plugin_discovery = true
  plugin_dirs = ["plugins"]

[custom_options]
  base_url = "https://pwd.re"

[index]
  index = true
  force_indexing_path_regex = "(etc|writings)"

[index.fields]
  publish_date = { selector = ["span#publish-date"] }
  updated_date = { selector = ["span#updated-date"] }

# Takes the content of the first <h1> and inserts it into the <title>
[widgets.page-title]
  widget = "title"


@@ 35,3 46,7 @@

[widgets.check-links]
  widget = "check_links"

[widgets.sitemap]
  widget = "sitemap"
  sitemap_file = "sitemap.xml"

M templates/main.html => templates/main.html +1 -1
@@ 5,7 5,7 @@
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/feed.xml">-->
    <!--<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml">-->
    <link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml">
    <link rel="stylesheet" href="/css/main.css">
    <link rel="stylesheet" href="/css/prism.css">
  </head>