all repos

olexsmir.xyz @ 3f1230b37b4afc069b0c414c78985c7d7f853512

my site, yes, i like lua

olexsmir.xyz/lua/lego/sitemap.lua(view raw)

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
local a = require "lego.html.attribute"
local formatDate = require("lego.date").date
local h = require "lego.html"
local sitemap = {}

---@param opts {url:string, date:string, priority: string}
local function url(opts)
  return h.el("url", {}, {
    h.el("loc", {}, { h.text(opts.url) }),
    h.el("lastmod", {}, { h.text(formatDate(opts.date)) }),
    h.el("priority", {}, { h.text(opts.priority) }),
  })
end

---@param posts lego.Post[]
---@param config {site_url:string}
---@return string
function sitemap.sitemap(posts, config)
  local urls = vim
    .iter(posts)
    ---@param post lego.Post
    :map(function(post)
      return url {
        url = config.site_url .. "/" .. post.meta.slug,
        date = post.meta.date,
        priority = "0.80",
      }
    end)
    :totable()

  return h.render(h.el("urlset", {
    a.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    a.attr("xmlns:xhtml", "http://www.w3.org/1999/xhtml"),
  }, {
    url {
      url = config.site_url,
      date = posts[1].meta.date,
      priority = "1.0",
    },
    unpack(urls),
  }))
end

return sitemap