Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
39 changes: 31 additions & 8 deletions src/utils/conditionToDatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand All @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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)}"`,
},
],
},
Expand Down Expand Up @@ -233,7 +252,7 @@ const translator: Record<string, Translator> = {
isVariable: true,
},
"is referenced by block in page with title": {
callback: ({ source, target }) => [
callback: ({ source, target, uid }) => [
{
type: "data-pattern",
arguments: [
Expand All @@ -250,7 +269,7 @@ const translator: Record<string, Translator> = {
{ type: "variable", value: target },
],
},
...getTitleDatalog({ source: target, target }),
...getTitleDatalog({ source: target, target, uid }),
],
placeholder: "Enter any placeholder for the node",
targetOptions: () =>
Expand All @@ -259,6 +278,7 @@ const translator: Record<string, Translator> = {
"{date:today}",
"{current}",
"{current user}",
"{this page}",
]),
},
"is in page": {
Expand All @@ -283,6 +303,7 @@ const translator: Record<string, Translator> = {
"{date:today}",
"{current}",
"{current user}",
"{this page}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
Expand Down Expand Up @@ -585,7 +606,7 @@ const translator: Record<string, Translator> = {
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: [
Expand All @@ -594,14 +615,15 @@ const translator: Record<string, Translator> = {
{ type: "variable", value: `${target}-Ref` },
],
},
...getTitleDatalog({ source: `${target}-Ref`, target }),
...getTitleDatalog({ source: `${target}-Ref`, target, uid }),
],
targetOptions: () =>
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
"{this page}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
Expand All @@ -620,7 +642,7 @@ const translator: Record<string, Translator> = {
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: [
Expand All @@ -629,14 +651,15 @@ const translator: Record<string, Translator> = {
{ type: "variable", value: target },
],
},
...getTitleDatalog({ source: target, target }),
...getTitleDatalog({ source: target, target, uid }),
],
targetOptions: () =>
getAllPageNames().concat([
"{date}",
"{date:today}",
"{current}",
"{current user}",
"{this page}",
]),
placeholder: "Enter a page name or {date} for any DNP",
},
Expand Down