I wanted to change the filename to Title Case when creating a page.
Is this a correct approach?
So far it doesn't work.
aza = aza or {}
-- `editor.getCurrentPage()` gives me the full path.
-- This function is only return the last path.
function aza.getCurrentPageName()
return editor.getCurrentPage():match("[^/]*$")
end
)
event.listen {
name = "editor:pageCreating",
run = function(e)
if not e.data.name:startsWith("In/") then return end
local title = aza.getCurrentPageName():gsub("(%a)([%w_']*)", function(f, r)
return f:upper() .. r:lower()
end)
local text = "# " .. title
return { text = text, perm = "rw" }
end
}
I'm not sure this is feasible. I highly recommend working with deepwiki for these kind of questions. It's not right with it's suggestions, but it does often help me in the right direction.
In this case, it suggests to "abuse" the template system for this - which may or may not work for your use case.
command.define {
name = "In: New Page",
run = function()
local name = editor.prompt("Page name")
if not name then return end
-- Convert to title case
local titleCaseName = name:gsub("(%w+)", function(word)
return word:sub(1,1):upper() .. word:sub(2):lower()
end)
local fullName = "In/" .. titleCaseName
editor.navigate(fullName)
end
}
The pageCreating event is fired before the page exists and there is no way to modify the name of the page at that point, only the content. What could work is listening to the editor:pageSaved event and renaming the page (potentially using invokeCommand and Page: Rename). This may cause some artifacts when creating the page, but should work