diff --git a/docs/query-builder.md b/docs/query-builder.md index a017b618..50275699 100644 --- a/docs/query-builder.md +++ b/docs/query-builder.md @@ -79,6 +79,16 @@ You can use a combination of multiple **conditions** to select the data you want | `with text` | The `source` block or page has the exact text `target` somewhere in its block text or page title. | [Link](examples.md#with-text) | | `with text in title` | The `source` page has the exact text `target` somewhere in its page title. | [Link](examples.md#with-title-in-text) | +### Target Options + +Some relationships have a `target` field that supports the following options: + +- `{date}` - matches any Daily Note Page +- `{date:today}` - matches the current date +- `{current}` - matches the page currently open in the main window +- `{this page}` - matches the page in which the query is run +- `{current user}` - matches the current user + ## Selections **Selections** specify what data from the blocks that match your conditions get returned. They determine the **columns** of the table. By default, the block text or page title is always returned and hyperlinked. Every selection is made up of two parts: the `label` and the `data`: diff --git a/src/utils/conditionToDatalog.ts b/src/utils/conditionToDatalog.ts index 28156288..f76bdfdf 100644 --- a/src/utils/conditionToDatalog.ts +++ b/src/utils/conditionToDatalog.ts @@ -17,6 +17,7 @@ import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageU import getPageTitlesStartingWithPrefix from "roamjs-components/queries/getPageTitlesStartingWithPrefix"; import extractRef from "roamjs-components/util/extractRef"; import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName"; +import getPageTitleByBlockUid from "roamjs-components/queries/getPageTitleByBlockUid"; type ConditionToDatalog = (condition: Condition) => DatalogClause[]; @@ -32,9 +33,11 @@ const regexRePatternValue = (str: string) => { const getTitleDatalog = ({ source, target, + uid, }: { source: string; target: string; + uid?: string; }): DatalogClause[] => { const dateMatch = /^\s*{date(?::([^}]+))?}\s*$/i.exec(target); if (dateMatch) { @@ -93,7 +96,7 @@ const getTitleDatalog = ({ if (currentMatch) { // Can't use this, since it's async // window.roamAlphaAPI.ui.mainWindow.getOpenPageOrBlockUid(); - const uid = getCurrentPageUid(); + const mainWindowUid = getCurrentPageUid(); return [ { type: "data-pattern", @@ -102,7 +105,23 @@ const getTitleDatalog = ({ { type: "constant", value: ":node/title" }, { type: "constant", - value: `"${getPageTitleByPageUid(uid)}"`, + value: `"${getPageTitleByPageUid(mainWindowUid)}"`, + }, + ], + }, + ]; + } + const thisPageMatch = /^\s*{this page}\s*$/i.test(target); + if (thisPageMatch && uid) { + return [ + { + type: "data-pattern", + arguments: [ + { type: "variable", value: source }, + { type: "constant", value: ":node/title" }, + { + type: "constant", + value: `"${getPageTitleByBlockUid(uid)}"`, }, ], }, @@ -233,7 +252,7 @@ const translator: Record = { isVariable: true, }, "is referenced by block in page with title": { - callback: ({ source, target }) => [ + callback: ({ source, target, uid }) => [ { type: "data-pattern", arguments: [ @@ -250,7 +269,7 @@ const translator: Record = { { type: "variable", value: target }, ], }, - ...getTitleDatalog({ source: target, target }), + ...getTitleDatalog({ source: target, target, uid }), ], placeholder: "Enter any placeholder for the node", targetOptions: () => @@ -259,6 +278,7 @@ const translator: Record = { "{date:today}", "{current}", "{current user}", + "{this page}", ]), }, "is in page": { @@ -283,6 +303,7 @@ const translator: Record = { "{date:today}", "{current}", "{current user}", + "{this page}", ]), placeholder: "Enter a page name or {date} for any DNP", }, @@ -585,7 +606,7 @@ const translator: Record = { placeholder: "Enter the display name of any user with access to this graph", }, "references title": { - callback: ({ source, target }) => [ + callback: ({ source, target, uid }) => [ { type: "data-pattern", arguments: [ @@ -594,7 +615,7 @@ const translator: Record = { { type: "variable", value: `${target}-Ref` }, ], }, - ...getTitleDatalog({ source: `${target}-Ref`, target }), + ...getTitleDatalog({ source: `${target}-Ref`, target, uid }), ], targetOptions: () => getAllPageNames().concat([ @@ -602,6 +623,7 @@ const translator: Record = { "{date:today}", "{current}", "{current user}", + "{this page}", ]), placeholder: "Enter a page name or {date} for any DNP", }, @@ -620,7 +642,7 @@ const translator: Record = { placeholder: "Enter a heading value (0, 1, 2, 3)", }, "is in page with title": { - callback: ({ source, target }) => [ + callback: ({ source, target, uid }) => [ { type: "data-pattern", arguments: [ @@ -629,7 +651,7 @@ const translator: Record = { { type: "variable", value: target }, ], }, - ...getTitleDatalog({ source: target, target }), + ...getTitleDatalog({ source: target, target, uid }), ], targetOptions: () => getAllPageNames().concat([ @@ -637,6 +659,7 @@ const translator: Record = { "{date:today}", "{current}", "{current user}", + "{this page}", ]), placeholder: "Enter a page name or {date} for any DNP", },