From 2a697332e8790c9ff6c2c6a3d3653f4fe2ba9ee6 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Mon, 24 Jul 2023 11:09:17 +0200 Subject: [PATCH] Added `replaceRegexp` helper to templates --- plugs/core/page.ts | 2 +- plugs/directive/complete.ts | 4 +-- plugs/directive/handlebar_helpers.ts | 40 ++++++++++++++++++++++ plugs/directive/util.ts | 38 +-------------------- website/CHANGELOG.md | 1 + website/πŸ”Œ Core/Templates.md | 3 +- website/πŸ”Œ Directive/Query.md | 51 ++++++++++++---------------- website/πŸ”Œ Tasks.md | 12 +++---- 8 files changed, 75 insertions(+), 76 deletions(-) create mode 100644 plugs/directive/handlebar_helpers.ts diff --git a/plugs/core/page.ts b/plugs/core/page.ts index 6a904958..a02be94e 100644 --- a/plugs/core/page.ts +++ b/plugs/core/page.ts @@ -32,7 +32,7 @@ import { isValidPageName } from "$sb/lib/page.ts"; const backlinkPrefix = `l:`; -type BacklinkEntry = { +export type BacklinkEntry = { name: string; alias?: string; inDirective?: boolean; diff --git a/plugs/directive/complete.ts b/plugs/directive/complete.ts index f5a68784..3ac967e9 100644 --- a/plugs/directive/complete.ts +++ b/plugs/directive/complete.ts @@ -1,7 +1,7 @@ import { events } from "$sb/plugos-syscall/mod.ts"; import { CompleteEvent } from "$sb/app_event.ts"; -import { buildHandebarOptions, handlebarHelpers } from "./util.ts"; -import { PageMeta } from "../../web/types.ts"; +import { buildHandebarOptions } from "./util.ts"; +import type { PageMeta } from "../../web/types.ts"; export async function queryComplete(completeEvent: CompleteEvent) { const match = /#query ([\w\-_]+)*$/.exec(completeEvent.linePrefix); diff --git a/plugs/directive/handlebar_helpers.ts b/plugs/directive/handlebar_helpers.ts new file mode 100644 index 00000000..38b22480 --- /dev/null +++ b/plugs/directive/handlebar_helpers.ts @@ -0,0 +1,40 @@ +import { niceDate } from "$sb/lib/dates.ts"; + +export function handlebarHelpers(_pageName: string) { + return { + json: (v: any) => JSON.stringify(v), + niceDate: (ts: any) => niceDate(new Date(ts)), + escapeRegexp: (ts: any) => { + return ts.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); + }, + replaceRegexp: (s: string, regexp: string, replacement: string) => { + return s.replace(new RegExp(regexp, "g"), replacement); + }, + prefixLines: (v: string, prefix: string) => + v.split("\n").map((l) => prefix + l).join("\n"), + substring: (s: string, from: number, to: number, elipsis = "") => + s.length > to - from ? s.substring(from, to) + elipsis : s, + + today: () => niceDate(new Date()), + tomorrow: () => { + const tomorrow = new Date(); + tomorrow.setDate(tomorrow.getDate() + 1); + return niceDate(tomorrow); + }, + yesterday: () => { + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + return niceDate(yesterday); + }, + lastWeek: () => { + const lastWeek = new Date(); + lastWeek.setDate(lastWeek.getDate() - 7); + return niceDate(lastWeek); + }, + nextWeek: () => { + const nextWeek = new Date(); + nextWeek.setDate(nextWeek.getDate() + 7); + return niceDate(nextWeek); + }, + }; +} diff --git a/plugs/directive/util.ts b/plugs/directive/util.ts index ec59f8fc..8c51dd94 100644 --- a/plugs/directive/util.ts +++ b/plugs/directive/util.ts @@ -1,8 +1,8 @@ import Handlebars from "handlebars"; import { space } from "$sb/silverbullet-syscall/mod.ts"; -import { niceDate } from "$sb/lib/dates.ts"; import { PageMeta } from "../../web/types.ts"; +import { handlebarHelpers } from "./handlebar_helpers.ts"; const maxWidth = 70; @@ -96,39 +96,3 @@ export function buildHandebarOptions(pageMeta: PageMeta) { data: { page: pageMeta }, }; } - -export function handlebarHelpers(pageName: string) { - return { - json: (v: any) => JSON.stringify(v), - niceDate: (ts: any) => niceDate(new Date(ts)), - escapeRegexp: (ts: any) => { - return ts.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); - }, - prefixLines: (v: string, prefix: string) => - v.split("\n").map((l) => prefix + l).join("\n"), - substring: (s: string, from: number, to: number, elipsis = "") => - s.length > to - from ? s.substring(from, to) + elipsis : s, - - today: () => niceDate(new Date()), - tomorrow: () => { - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - return niceDate(tomorrow); - }, - yesterday: () => { - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - return niceDate(yesterday); - }, - lastWeek: () => { - const lastWeek = new Date(); - lastWeek.setDate(lastWeek.getDate() - 7); - return niceDate(lastWeek); - }, - nextWeek: () => { - const nextWeek = new Date(); - nextWeek.setDate(nextWeek.getDate() + 7); - return niceDate(nextWeek); - }, - }; -} diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index cc6b9ab8..873fe408 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -9,6 +9,7 @@ release. * **Bug fix**: Renaming of pages now works again on iOS * Backlinks (as queried via the `link` data source) now contains richer data, namely `inDirective` (if the link appears in the context of a directive) and `alias` (if the backlink has an alias name). This also fixes not updating page references inside directives. This introduced a backwards incompatible data. To update your indexes, please run {[Space: Reindex]} on your clients (once). * Added {[Debug: Reset Client]} command that flushes the local databases and caches (and service worker) for debugging purposes. +* New template helper `replaceRegexp`, see [[πŸ”Œ Core/Templates@vars]] * Big internal code refactor --- diff --git a/website/πŸ”Œ Core/Templates.md b/website/πŸ”Œ Core/Templates.md index 602b3a10..4b3c329d 100644 --- a/website/πŸ”Œ Core/Templates.md +++ b/website/πŸ”Œ Core/Templates.md @@ -73,7 +73,8 @@ Currently supported (hardcoded in the code): - `{{yesterday}}`: Yesterday’s date in the usual YYY-MM-DD format - `{{lastWeek}}`: Current date - 7 days - `{{nextWeek}}`: Current date + 7 days -- `{{escapeRegexp "hello/there"}}` to escape a regexp, useful when injecting e.g. a page name into a query β€” think `name =~ /{{escapeRegexp @page.name}}/` +- `{{escapeRegexp "hello/there"}}` to escape a regexp, useful when injecting e.g. a page name into a query β€” think `name =~ /{{escapeRegexp @page.name}}/ +`* `{{replaceRegexp string regexp replacement}}`: replace a regular expression in a string, example use: `{{replaceRegexp name "#[^#\d\s\[\]]+\w+" ""}}` to remove hashtags from a task name - `{{json @page}}` translate any (object) value to JSON, mostly useful for debugging - `{{relativePath @page.name}}` translate a path to a relative one (to the current page), useful when injecting page names, e.g. `{{relativePath name}}`. - `{{substring "my string" 0 3}}` performs a substring operation on the first argument, which in this example would result in `my ` diff --git a/website/πŸ”Œ Directive/Query.md b/website/πŸ”Œ Directive/Query.md index 4f0fa79f..69b6ed29 100644 --- a/website/πŸ”Œ Directive/Query.md +++ b/website/πŸ”Œ Directive/Query.md @@ -51,14 +51,7 @@ The best part about data sources: there is auto-completion. πŸŽ‰ Start writing `` @@ -95,8 +88,8 @@ Example: |name|age|city |country|page |pos | |----|--|-----|-----|------------------|----| -|John|50|Milan|Italy|πŸ”Œ Directive/Query|3279| -|Jane|53|Rome |Italy|πŸ”Œ Directive/Query|3280| +|John|50|Milan|Italy|πŸ”Œ Directive/Query|3293| +|Jane|53|Rome |Italy|πŸ”Œ Directive/Query|3294| #### 4.2 Plugs’ data sources @@ -163,11 +156,11 @@ For the sake of simplicity, we will use the `page` data source and limit the res **Result:** Look at the data. This is more than we need. The query even gives us template pages. Let's try to limit it in the next step. -|name |lastModified |contentType |size |perm|tags| -|--------------|-------------|-------------|-----|--|----| -|Authentication|1686682290943|text/markdown|1730 |rw| | -|BROKEN LINKS |1688066558009|text/markdown|196 |rw| | -|CHANGELOG |1687348511871|text/markdown|27899|rw|tags| +|name |lastModified |contentType |size|perm| +|--------------|-------------|-------------|----|--| +|API |1688987324351|text/markdown|1405|rw| +|Authelia |1688482500313|text/markdown|866 |rw| +|Authentication|1686682290943|text/markdown|1730|rw| @@ -178,13 +171,13 @@ For the sake of simplicity, we will use the `page` data source and limit the res **Result:** Okay, this is what we wanted but there is also information such as `perm`, `type` and `lastModified` that we don't need. -|name |lastModified |contentType |size|perm|type|uri |repo |author |share-support| -|--|--|--|--|--|--|--|--|--|--| -|πŸ”Œ KaTeX |1687099068396|text/markdown|1342|rw|plug|github:silverbulletmd/silverbullet-katex/katex.plug.js |https://github.com/silverbulletmd/silverbullet-katex |Zef Hemel | | -|πŸ”Œ Core |1687094809367|text/markdown|402 |rw|plug| |https://github.com/silverbulletmd/silverbullet | | | -|πŸ”Œ Collab |1686682290959|text/markdown|2969|rw|plug| |https://github.com/silverbulletmd/silverbullet | |true| -|πŸ”Œ Twitter|1685105433212|text/markdown|1266|rw|plug|github:silverbulletmd/silverbullet-twitter/twitter.plug.js|https://github.com/silverbulletmd/silverbullet-twitter|SilverBullet Authors| | -|πŸ”Œ Mermaid|1685105423879|text/markdown|1096|rw|plug|github:silverbulletmd/silverbullet-mermaid/mermaid.plug.js|https://github.com/silverbulletmd/silverbullet-mermaid|Zef Hemel | | +|name |lastModified |contentType |size|perm|type|repo |uri |author | +|--|--|--|--|--|--|--|--|--| +|πŸ”Œ Directive|1688987324365|text/markdown|2607|rw|plug|https://github.com/silverbulletmd/silverbullet | | | +|πŸ”Œ KaTeX |1687099068396|text/markdown|1342|rw|plug|https://github.com/silverbulletmd/silverbullet-katex |github:silverbulletmd/silverbullet-katex/katex.plug.js |Zef Hemel | +|πŸ”Œ Core |1687094809367|text/markdown|402 |rw|plug|https://github.com/silverbulletmd/silverbullet | | | +|πŸ”Œ Twitter |1685105433212|text/markdown|1266|rw|plug|https://github.com/silverbulletmd/silverbullet-twitter|github:silverbulletmd/silverbullet-twitter/twitter.plug.js|SilverBullet Authors| +|πŸ”Œ Mermaid |1685105423879|text/markdown|1096|rw|plug|https://github.com/silverbulletmd/silverbullet-mermaid|github:silverbulletmd/silverbullet-mermaid/mermaid.plug.js|Zef Hemel | #### 6.3 Query to select only certain fields @@ -196,13 +189,13 @@ and `repo` columns and then sort by last modified time. from a visual perspective. -|name |author |repo | +|name |author |repo | |--|--|--| -|πŸ”Œ KaTeX |Zef Hemel |https://github.com/silverbulletmd/silverbullet-katex | -|πŸ”Œ Core | |https://github.com/silverbulletmd/silverbullet | -|πŸ”Œ Collab | |https://github.com/silverbulletmd/silverbullet | -|πŸ”Œ Twitter|SilverBullet Authors|https://github.com/silverbulletmd/silverbullet-twitter| -|πŸ”Œ Mermaid|Zef Hemel |https://github.com/silverbulletmd/silverbullet-mermaid| +|πŸ”Œ Directive| |https://github.com/silverbulletmd/silverbullet | +|πŸ”Œ KaTeX |Zef Hemel |https://github.com/silverbulletmd/silverbullet-katex | +|πŸ”Œ Core | |https://github.com/silverbulletmd/silverbullet | +|πŸ”Œ Twitter |SilverBullet Authors|https://github.com/silverbulletmd/silverbullet-twitter| +|πŸ”Œ Mermaid |Zef Hemel |https://github.com/silverbulletmd/silverbullet-mermaid| #### 6.4 Display the data in a format defined by a template @@ -212,9 +205,9 @@ from a visual perspective. **Result:** Here you go. This is the result we would like to achieve πŸŽ‰. Did you see how I used `render` and `template/plug` in a query? πŸš€ +* [[πŸ”Œ Directive]] * [[πŸ”Œ KaTeX]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-katex)) * [[πŸ”Œ Core]] -* [[πŸ”Œ Collab]] * [[πŸ”Œ Twitter]] by **SilverBullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet-twitter)) * [[πŸ”Œ Mermaid]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-mermaid)) diff --git a/website/πŸ”Œ Tasks.md b/website/πŸ”Œ Tasks.md index 5f9002b3..d928cd34 100644 --- a/website/πŸ”Œ Tasks.md +++ b/website/πŸ”Œ Tasks.md @@ -23,10 +23,10 @@ When the cursor is positioned inside of a due date, the {[Task: Postpone]} comma This metadata is extracted and available via the `task` query source to [[πŸ”Œ Directive/Query]]: - -|name |done |page |pos|tags |deadline | -|---------------------|-----|--------|---|------|----------| -|This is a task |false|πŸ”Œ Tasks|215| | | -|This is a tagged task|false|πŸ”Œ Tasks|289|my-tag| | -|This is due |false|πŸ”Œ Tasks|575| |2022-11-26| + +|name |done |page |pos|tags |deadline | +|-----------------------------|-----|--------|---|------|----------| +|This is a task |false|πŸ”Œ Tasks|213| | | +|This is a tagged task #my-tag|false|πŸ”Œ Tasks|287|my-tag| | +|This is due |false|πŸ”Œ Tasks|573| |2022-11-26|