Files
plainleaf/Library/Std/Template.md
T
Zef Hemel 18df394ef8 Add page template functionality and restructure existing templates
- Introduced a new page template system to automate page creation based on defined templates.
- Added configuration options for templates, including suggested names, confirmation prompts, and keyboard shortcuts.
- Created a new "Quick Note" template with specific command and key bindings.
- Removed the old "Page Templates" file and integrated its content into the new structure for better organization.
2025-04-06 08:59:49 +02:00

1.2 KiB

#meta

Implements useful template functions

-- priority: 50
-- Template API root table
template = template or {}
-- Template storage table
templates = {}

-- Iterates over a table/array and applies a function to each element,
-- concatenating the results
function template.each(tbl, fn)
    local result = {}
    for _, item in ipairs(tbl) do
        table.insert(result, fn(item))
    end
    return table.concat(result)
end

-- Creates a new template function from a string template
function template.new(templateStr)
  -- Preprocess: strip indentation
  local lines = {}
  local splitLines = string.split(templateStr, "\n")
  for _, line in ipairs(splitLines) do
    line = string.gsub(line, "^    ", "")
    table.insert(lines, line)
  end
  templateStr = table.concat(lines, "\n")
  return function(obj)
    return spacelua.interpolate(templateStr, obj)
  end
end

-- Reads a template from a page
-- Return value:
-- * The template itself
-- * The extracted frontmatter
function template.fromPage(name)
  local fm = index.extractFrontmatter(space.readPage(name),  {
    removeFrontmatterSection = true,
    removeTags = true
  })

  return template.new(string.trimStart(fm.text)), fm.frontmatter
end