Skip to content

Commit 9dc6ed2

Browse files
authored
Honor openIfExists: true when using Page: From Template command (#1831)
* Improve Page Template option behaviour documentation * Check pageName == nil or "" when getting page template name from prompt * Honor `openIfExists: true` when using `Page: From Template` command
1 parent ab9965a commit 9dc6ed2

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

libraries/Library/Std/Infrastructure/Page Templates.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ the page.
1717
Optional keys you can set in the page’s frontmatter:
1818

1919
* `suggestedName`: the proposed name for the new page, can use embedded Lua expressions, like `Daily/${date.today()}`.
20-
* `confirmName`: Confirm the suggested page name before creating it (defaults to `true`).
21-
* `openIfExists`: If a page with the `suggestedName` already exists, open it rather than attempting to create it anew.
20+
* `confirmName`: Confirm the suggested page name before creating it (defaults to `true`). Only effects pages created by a key binding, otherwise you will always be prompted to confirm the new page name.
21+
* `openIfExists`: If a page with the `suggestedName` already exists, open it rather than showing an error.
2222
* `command`: expose the page template as a command with this name.
2323
* `key`: Bind the snippet to a keyboard shortcut (note: this requires to _also_ specify the `command` configuration).
2424
* `mac`: Bind the snippet to a Mac-specific keyboard shortcut.
@@ -79,11 +79,16 @@ This is my quick note version
7979
```space-lua
8080
-- priority: 10
8181
82-
local function createPageFromTemplate(templatePage, pageName)
82+
local function createPageFromTemplate(templatePage, pageName, openIfExists)
8383
-- Won't override an existing page
8484
if space.pageExists(pageName) then
85-
editor.flashNotification("Page " .. pageName .. " already exists", "error")
86-
return
85+
if openIfExists then
86+
editor.navigate(pageName)
87+
return
88+
else
89+
editor.flashNotification("Page " .. pageName .. " already exists", "error")
90+
return
91+
end
8792
end
8893
local tpl, fm = template.fromPage(templatePage)
8994
local initialText = ""
@@ -115,16 +120,13 @@ for pt in query[[
115120
pageName = (template.new(pt.suggestedName))()
116121
end
117122
if pt.confirmName != false then
118-
pageName = editor.prompt("Page name", pageName)
119-
end
120-
if not pageName then
121-
return
123+
pageName = string.trim(some(editor.prompt("Page name", pageName)) or "")
122124
end
123-
if pt.openIfExists and space.pageExists(pageName) then
124-
editor.navigate(pageName)
125+
if pageName == "" then
126+
editor.flashNotification("No page name given for '" .. pt.command .. "', unable to continue.", "error")
125127
return
126128
end
127-
createPageFromTemplate(pt.name, pageName)
129+
createPageFromTemplate(pt.name, pageName, pt.openIfExists)
128130
end
129131
}
130132
end
@@ -145,7 +147,8 @@ command.define {
145147
select {
146148
name = cleanName(_.name),
147149
fullName = _.name,
148-
suggestedName = _.suggestedName
150+
suggestedName = _.suggestedName,
151+
openIfExists = _.openIfExists
149152
}
150153
]]
151154
local selected = editor.filterBox("Page template", pageTemplates, "Pick the template you would like to instantiate")
@@ -156,11 +159,12 @@ command.define {
156159
if selected.suggestedName then
157160
pageName = (template.new(selected.suggestedName))()
158161
end
159-
pageName = editor.prompt("Page name", pageName)
160-
if not pageName then
162+
pageName = string.trim(some(editor.prompt("Page name", pageName)) or "")
163+
if pageName == "" then
164+
editor.flashNotification("No page name given for template '" .. cleanName(selected.fullName) .. "', unable to continue.", "error")
161165
return
162166
end
163-
createPageFromTemplate(selected.fullName, pageName)
167+
createPageFromTemplate(selected.fullName, pageName, selected.openIfExists)
164168
end
165169
}
166170
```

0 commit comments

Comments
 (0)