silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.04k stars 141 forks source link

Feature Request: Virtual Pages #769

Open schroederc opened 4 months ago

schroederc commented 4 months ago

Generalize https://github.com/silverbulletmd/silverbullet/blob/main/plugs/index/tag_page.ts to any number of “virtual” pages.

Instead of 📌 {{tag}} pages being specially handled, have a general mechanism for rendering read-only pages derived from a template.

My suggestion would be to add a new field to Page Templates to optionally denote it as “virtual”:

---
description: "Index of tagged objects"
tags: template
hooks.newPage:
  forPrefix: "Tag/"
  virtual: true
---

These are all objects in your space tagged with #{{replace(@page.name, "Tag/", "")}}.
```template
template: |
    {{#if .}}
    # Pages
    {{#each .}}
    * [[{{name}}]]
    {{/each}}
    {{/if}}
query: |
    page where tags = {{replace(@page.name, "Tag/", "")}}
```.

When a user opens such a page, its template is rendered in a read-only fashion and the title can be consumed to vary its contents like for the above simplified tag index page.

Along with allowing the template for tag pages to be customized by the user with this feature (by moving the template into the Core library), it allows some use-cases such as having virtual Weekly Note pages that summarize each week, having index pages for a prefix (like a Person/ index), or interesting pages like Journal/Tasks/2024-02-29:

---
description: "Index of tasks for a day (inbox + journal)"
tags: template
hooks.newPage:
  forPrefix: "Journal/Tasks/"
  virtual: true
---

```template
template: |
  {{escape "#each ."}}
  * [[{{escape "state"}}]] [[{{escape "ref"}}]] {{escape "name"}}
  {{escape "/each"}}
query: |
  task where page =~ /\Journal\/Day\/{{replace(@page.name, "Journal/Tasks/", "")}}/ or page =~ /^Inbox\/{{replace(@page.name, "Journal/Tasks/", "")}} /
```.

An important aspect of this feature is that the pages are not reified which means that we can alter the template to alter all instances of the virtual page at any point and we don't litter our Space with many copies of essentially the same contents. They also act like the existing tag pages which means they are read-only and the rendered templates aren’t expandable (for easier readability).

henrikx commented 4 months ago

This is a great suggestion.

Would essentially solve my problem: https://github.com/silverbulletmd/silverbullet/issues/743#issuecomment-1961320973

devzero commented 4 months ago

+1 this would be awesome

zefhemel commented 4 months ago

Also see: https://community.silverbullet.md/t/virtual-page-templates/96