From 663a15fe33e8cae717930f3f1a880e155842b1d4 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 15 Nov 2023 14:56:34 +0100 Subject: [PATCH] Template sets --- plugs/query/template.ts | 19 +++++----- web/components/page_navigator.tsx | 2 +- website/CHANGELOG.md | 5 +-- website/Objects.md | 4 +-- website/Template Sets.md | 43 +++++++++++++++++++++++ website/template/debug/debug.md | 12 +++++++ website/template/documented-template.md | 4 +++ website/template/maintenance/conflicts.md | 9 +++++ website/template/pages/page.md | 6 ++++ website/template/tagged-tasks.md | 2 +- website/template/tasks/incoming.md | 10 ++++++ website/template/tasks/tagged.md | 10 ++++++ website/template/tasks/task.md | 7 ++++ website/🔌 Tasks.md | 4 +-- 14 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 website/Template Sets.md create mode 100644 website/template/debug/debug.md create mode 100644 website/template/documented-template.md create mode 100644 website/template/maintenance/conflicts.md create mode 100644 website/template/pages/page.md create mode 100644 website/template/tasks/incoming.md create mode 100644 website/template/tasks/tagged.md create mode 100644 website/template/tasks/task.md diff --git a/plugs/query/template.ts b/plugs/query/template.ts index 75537fa..a248672 100644 --- a/plugs/query/template.ts +++ b/plugs/query/template.ts @@ -1,9 +1,10 @@ import { WidgetContent } from "$sb/app_event.ts"; -import { handlebars, markdown, space, system, YAML } from "$sb/syscalls.ts"; +import { markdown, space, system, YAML } from "$sb/syscalls.ts"; import { rewritePageRefs } from "$sb/lib/resolve.ts"; import { loadPageObject, replaceTemplateVars } from "../template/template.ts"; import { renderToText } from "$sb/lib/tree.ts"; import { PageMeta } from "$sb/types.ts"; +import { renderTemplate } from "../template/plug_api.ts"; type TemplateConfig = { // Pull the template from a page @@ -39,13 +40,15 @@ export async function widget( ) : undefined; - let rendered = config.raw ? templateText : await handlebars.renderTemplate( - templateText, - value, - { - page: pageMeta, - }, - ); + console.log("Value", value); + + let { text: rendered } = config.raw + ? { text: templateText } + : await renderTemplate( + templateText, + pageMeta, + value, + ); if (templatePage) { const parsedMarkdown = await markdown.parseMarkdown(rendered); diff --git a/web/components/page_navigator.tsx b/web/components/page_navigator.tsx index 47d854d..31ca73e 100644 --- a/web/components/page_navigator.tsx +++ b/web/components/page_navigator.tsx @@ -57,7 +57,7 @@ export function PageNavigator({ darkMode={darkMode} completer={completer} allowNew={true} - helpText="Press Enter to open the selected page, or Shift-Enter to create a new page." + helpText="Press Enter to open the selected page, or Shift-Enter to create a new page with this exact name." newHint="Create page" completePrefix={completePrefix} onSelect={(opt) => { diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 1e870ba..aab962b 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -4,6 +4,7 @@ release. --- ## Next * The `Alt-q` command is now bound to the new {[Live Queries and Templates: Refresh All]} command refreshing all [[Live Queries]] and [[Live Templates]] on the page. This is to get y’all prepared to move away from directives. +* It’s time to get ready for the removal of directives, and convert your entire space over using (one time operation). Please backup your space before you do: {[Directive: Convert Entire Space to Live/Templates]} --- @@ -505,8 +506,8 @@ Besides these architectural changes, a few other breaking changes were made to s ## 0.0.31 * Update to the query language: the `render` clause now uses page reference - syntax `[[page]]`. For example `render [[template/task]]` rather than - `render "template/task"`. The old syntax still works, but is deprecated, + syntax `[[page]]`. For example `render [[template/tasks/task]]` rather than + `render "template/tasks/task"`. The old syntax still works, but is deprecated, completion for the old syntax has been removed. * Updates to templates: * For the `Template: Instantiate Page` command, the page meta value `$name` is diff --git a/website/Objects.md b/website/Objects.md index 07b5010..9a5aed2 100644 --- a/website/Objects.md +++ b/website/Objects.md @@ -48,10 +48,10 @@ The following query shows all attributes available for tasks: ```query upnext ``` -Although you may want to render it using a template such as [[template/task]] instead: +Although you may want to render it using a template such as [[template/tasks/task] instead: ```query -upnext render [[template/task]] +upnext render [[template/tasks/task]] ``` ## taskstate diff --git a/website/Template Sets.md b/website/Template Sets.md new file mode 100644 index 0000000..bca97bd --- /dev/null +++ b/website/Template Sets.md @@ -0,0 +1,43 @@ +This is an attempt at collecting useful, reusable templates so you don’t have to reinvent the wheel. + +The most convenient ways to use them is using [[Federation]]. This will synchronize these templates into your space and make them available for use instantly. + +To set this up, add this to your [[SETTINGS]]: + +```yaml +federate: +- uri: silverbullet.md/template +``` + +If you don’t want to sync _all_ these templates, you can use more specific URIs, e.g. +```yaml +federate: +- uri: silverbullet.md/template/tasks +``` +to just get the `tasks` stuff. + +To reference a template, use the federation syntax, e.g. `[[!silverbullet.md/template/tasks/task]]`. + +## Maintenance +```query +template where name =~ /^template\/maintenance/ +order by order +render [[template/documented-template]] +``` +## Pages +```query +template +where name =~ /^template\/pages/ +order by order +render [[template/documented-template]] +``` +## Tasks +```query +template where name =~ /^template\/tasks/ +order by order +render [[template/documented-template]] +``` +## Debugging +```query +template where name =~ /^template\/debug/ render [[template/documented-template]] +``` diff --git a/website/template/debug/debug.md b/website/template/debug/debug.md new file mode 100644 index 0000000..f141a4b --- /dev/null +++ b/website/template/debug/debug.md @@ -0,0 +1,12 @@ +--- +tags: template +description: | + Renders its object value in a `key: value` format +usage: | + Can be used by passing in a YAML object in a template via `value` or in a `render` clause of a query +--- +{{#each .}} +{{@key}}: {{.}} +{{/each}} + +--- \ No newline at end of file diff --git a/website/template/documented-template.md b/website/template/documented-template.md new file mode 100644 index 0000000..c4196af --- /dev/null +++ b/website/template/documented-template.md @@ -0,0 +1,4 @@ +* [[{{ref}}|{{ref}}]] {{description}} +{{#if usage}} + * **Usage:** {{usage}} +{{/if}} \ No newline at end of file diff --git a/website/template/maintenance/conflicts.md b/website/template/maintenance/conflicts.md new file mode 100644 index 0000000..476b45a --- /dev/null +++ b/website/template/maintenance/conflicts.md @@ -0,0 +1,9 @@ +--- +tags: template +description: Lists all pages with ".conflicted" in the name, created as a result of a synchronization conflict. +--- + +### Conflicting pages +```query +page where name =~ /\.conflicted/ render [[template/pages/page]] +``` diff --git a/website/template/pages/page.md b/website/template/pages/page.md new file mode 100644 index 0000000..6df0f78 --- /dev/null +++ b/website/template/pages/page.md @@ -0,0 +1,6 @@ +--- +tags: template +description: A page reference link as a list item +--- + +* [[{{name}}]] \ No newline at end of file diff --git a/website/template/tagged-tasks.md b/website/template/tagged-tasks.md index 017eed7..e7425b9 100644 --- a/website/template/tagged-tasks.md +++ b/website/template/tagged-tasks.md @@ -1,4 +1,4 @@ #template ```query -task where tags = "{{.}}" and done = false render [[template/task]] +task where tags = "{{.}}" and done = false render [[template/tasks/task]] ``` \ No newline at end of file diff --git a/website/template/tasks/incoming.md b/website/template/tasks/incoming.md new file mode 100644 index 0000000..c31b9f2 --- /dev/null +++ b/website/template/tasks/incoming.md @@ -0,0 +1,10 @@ +--- +tags: template +description: | + Shows all tasks that reference (tag) the current page. For instance a task that references `[[John]]` in its name, would appear on the `John` page if it would use this [[sets/tasks/incoming]] template. +order: 2 +--- + +```query +task where name =~ /\[\[{{escapeRegexp @page.name}}\]\]/ where done = false render [[template/tasks/task]] +``` \ No newline at end of file diff --git a/website/template/tasks/tagged.md b/website/template/tasks/tagged.md new file mode 100644 index 0000000..eb5727f --- /dev/null +++ b/website/template/tasks/tagged.md @@ -0,0 +1,10 @@ +--- +tags: template +description: Queries all tasks tagged with a specific tag. +usage: Pass in the tag to filter on as the `value` of this template +order: 2 +--- + +```query +task where tags = "{{.}}" and done = false render [[template/tasks/task]] +``` diff --git a/website/template/tasks/task.md b/website/template/tasks/task.md new file mode 100644 index 0000000..be08331 --- /dev/null +++ b/website/template/tasks/task.md @@ -0,0 +1,7 @@ +--- +tags: template +description: generic task template that supports updating the status back in the origin page +order: 1 +--- + +* [{{state}}] [[{{ref}}]] {{name}} \ No newline at end of file diff --git a/website/🔌 Tasks.md b/website/🔌 Tasks.md index 3792741..b113d34 100644 --- a/website/🔌 Tasks.md +++ b/website/🔌 Tasks.md @@ -42,12 +42,12 @@ task where page = "{{@page.name}}" ``` ## Rendering -There is a [[!silverbullet.md/template/task]] template you can use to render tasks nicely rather than using the default table (as demonstrated above). When you use this template, you can even cycle through the states of the task by click on its state _inside_ the rendered query, and it will update the state of the _original_ task automatically (although not yet in reverse) — this works across pages. +There is a [[!silverbullet.md/template/tasks/task]] template you can use to render tasks nicely rather than using the default table (as demonstrated above). When you use this template, you can even cycle through the states of the task by click on its state _inside_ the rendered query, and it will update the state of the _original_ task automatically (although not yet in reverse) — this works across pages. Try it (by clicking on the checkbox inside of the directive): ```query -task where page = "{{@page.name}}" and name = "Remote toggle me" render [[template/task]] +task where page = "{{@page.name}}" and name = "Remote toggle me" render [[template/tasks/task]] ``` * [ ] Remote toggle me