Here’s where I ended up, at least for now. I overrode a Library/Std function because I was having an issue the override seems to have fixed, but I’m not convinced this was necessary.
-- Function: Create a page from a template (override Library/Std to test what may be a race condition
local function createPageFromTemplate(templatePage, pageName)
-- Won't override an existing page
if space.pageExists(pageName) then
editor.flashNotification("Page " .. pageName .. " already exists", "error")
return
end
local tpl, fm = template.fromPage(templatePage)
local initialText = ""
if fm.frontmatter then
initialText = "---\n"
.. string.trim(template.new(fm.frontmatter)())
.. "\n---\n"
end
-- Write an empty page to start
space.writePage(pageName, initialText)
editor.navigate({kind = "page", page = pageName})
-- Insert content once page is fully loaded
event.listen {
name = "editor:pageLoaded",
once = true,
run = function(e)
if e.data[1] == pageName then
local tries = 0
local function tryInsert()
local doc = editor.getText()
if doc and #doc >= #initialText then
editor.insertAtPos(tpl(), #initialText, true)
elseif tries < 10 then
tries = tries + 1
setTimeout(tryInsert, 100)
else
editor.flashNotification("Failed to insert template content", "error")
end
end
tryInsert()
end
end
}
end
-- Listener: When creating a page, use a template based on the name in the path (top-level, e.g., "Meeting"), or "Daily Note" for Journal, or a blank bullet for anything else
-- TODO: Change Daily Note to New Journal
event.listen {
name = "editor:pageCreating",
run = function(e)
local templateFlag = e.data.name:match("([^/]*)")
local templateName = ""
if templateFlag == "Journal" then
templateName = "Library/Templates/Daily Note"
else
templateName = "Library/Templates/New " .. templateFlag
end
if not space.pageExists(templateName) then
templateName = "Library/Templates/Default"
end
createPageFromTemplate(templateName, e.data.name)
end
}
-- Function: Generic function to create structured links for drive-by linking to certain types of content (meetings, people, organizations, etc.)
local function insertNewLink(opts)
local name = editor.prompt("Please enter " .. opts.promptLabel)
if not name then
name = opts.defaultPrefix .. os.date(opts.fallbackFormat or "%Y-%m-%d-%H-%M")
end
local pageName = opts.contentType .. "/"
.. (opts.subfolder and opts.subfolder() or "")
.. name
local link = "[[" .. pageName .. "]]"
if opts.prefixTag then
link = opts.prefixTag .. " " .. link
end
editor.insertAtCursor(link)
end
-- COMMANDS
-- Use case: I tend to do drive-by linking, creating links to content as I go
-- and then revisiting the links to fill in new content (including for new pages)
-- I could create the page when creating the link, so I may revisit doing that
-- recycling the above event listener.
command.define {
name = "New Meeting",
key = "Alt-Shift-m",
run = function()
insertNewLink {
promptLabel = "meeting name",
defaultPrefix = "New Meeting - ",
fallbackFormat = "%H%M",
contentType = "Meeting",
subfolder = function() return os.date("%Y-%m-%d/%H/") end,
prefixTag = "#meeting-notes"
}
end
}
command.define {
name = "New Person",
key = "Alt-Shift-p",
run = function()
insertNewLink {
promptLabel = "the person's name",
defaultPrefix = "New Person - ",
contentType = "Person",
prefixTag = "#person"
}
end
}
command.define {
name = "New Organization",
key = "Alt-Shift-o",
run = function()
insertNewLink {
promptLabel = "the organization's name",
defaultPrefix = "New Organization - ",
contentType = "Organization",
prefixTag = "#organization"
}
end
}
Everything seems to be working.