all repos

olexsmir.xyz @ cbe21a7

my site, yes, i like lua

olexsmir.xyz/lua/lego/post.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
local file = require "lego.file"
local frontmatter = require "lego.frontmatter"
local liblego = require "liblego"
local post = {}

---@class lego.Post
---@field content string
---@field hidden boolean
---@field meta lego.PostMeta

---@class lego.PostMeta
---@field title string
---@field date string
---@field slug string
---@field desc string

---@param fpath lego.FilePath
---@return lego.Post
function post.read_file(fpath)
  local p = file.read(fpath)
  local content = table.concat(frontmatter.content(p) or {}, "\n")
  local meta = frontmatter.extract(p)
  local hidden = meta["hidden"] == "true"
  assert(meta["title"] ~= nil, (file.to_path(fpath) .. " doesn't have title"))
  assert(meta["date"] ~= nil, (file.to_path(fpath) .. " doesn't have date"))
  assert(meta["slug"] ~= nil, (file.to_path(fpath) .. " doesn't have slug"))

  return {
    meta = meta,
    hidden = hidden,
    content = liblego.md_to_html(content),
  }
end

---MUTATES THE TABLE
---@param posts lego.Post[]
function post.sort_by_date(posts)
  table.sort(posts, function(a, b)
    return a.meta.date > b.meta.date
  end)
end

return post