Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "roamjs-components",
"description": "Expansive toolset, utilities, & components for developing RoamJS extensions.",
"version": "0.85.7",
"version": "0.86.0",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/components/FormDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import MenuItemSelect from "./MenuItemSelect";
import PageInput from "./PageInput";
import nanoid from "nanoid";
import { getUids } from "../dom";
import { InputTextNode, PullBlock } from "../types";
import { InputTextNode } from "../types";
import getFullTreeByParentUid from "../queries/getFullTreeByParentUid";
import createPage from "../writes/createPage";
import { createBlock } from "../writes";
Expand Down Expand Up @@ -174,7 +174,7 @@ const EmbedInput = ({
window.roamAlphaAPI.pull(
"[:block/order {:block/parents [:block/uid]}]",
[":block/uid", blockUid]
) as PullBlock;
);
if (
!(
order === 0 &&
Expand Down
4 changes: 2 additions & 2 deletions src/queries/getAllBlockUids.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getAllBlockUids = (): string[] =>
window.roamAlphaAPI
.q(`[:find ?u :where [?e :block/uid ?u] [?e :block/string]]`)
.map((f) => f[0] as string);
.q<[string]>(`[:find ?u :where [?e :block/uid ?u] [?e :block/string]]`)
.map((f) => f[0]);

export default getAllBlockUids;
6 changes: 4 additions & 2 deletions src/queries/getAllBlockUidsAndTexts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const getAllBlockUidsAndTexts = (): { uid: string; text: string }[] =>
window.roamAlphaAPI
.q(`[:find ?u ?s :where [?e :block/uid ?u] [?e :block/string ?s]]`)
.map((f) => ({ uid: f[0] as string, text: f[1] as string }));
.q<[string, string]>(
`[:find ?u ?s :where [?e :block/uid ?u] [?e :block/string ?s]]`
)
.map((f) => ({ uid: f[0], text: f[1] }));

export default getAllBlockUidsAndTexts;
4 changes: 2 additions & 2 deletions src/queries/getAllPageNames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getAllPageNames = (): string[] =>
window.roamAlphaAPI
.q("[:find ?s :where [?e :node/title ?s]]")
.map((b) => b[0] as string);
.q<[string]>("[:find ?s :where [?e :node/title ?s]]")
.map((b) => b[0]);

export default getAllPageNames;
14 changes: 6 additions & 8 deletions src/queries/getAttributeValueByBlockAndName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PullBlock } from "../types";
import type { PullBlock } from "../types";
import normalizePageTitle from "./normalizePageTitle";

const getAttributeValueByBlockAndName = ({
Expand All @@ -9,13 +9,11 @@ const getAttributeValueByBlockAndName = ({
uid: string;
}) =>
(
(
window.roamAlphaAPI.data.fast.q(
`[:find (pull ?b [:block/string]) :where [?a :node/title "${normalizePageTitle(
name
)}"] [?p :block/uid "${uid}"] [?b :block/refs ?a] [?b :block/parents ?p]]`
)?.[0]?.[0] as PullBlock
)?.[":block/string"] || ""
window.roamAlphaAPI.data.fast.q<[PullBlock]>(
`[:find (pull ?b [:block/string]) :where [?a :node/title "${normalizePageTitle(
name
)}"] [?p :block/uid "${uid}"] [?b :block/refs ?a] [?b :block/parents ?p]]`
)?.[0]?.[0]?.[":block/string"] || ""
)
.slice(name.length + 2)
.trim();
Expand Down
4 changes: 2 additions & 2 deletions src/queries/getBasicTreeByParentUid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const sortBasicNodes = (c: PullBlock[]): RoamBasicNode[] =>
const getBasicTreeByParentUid = (uid: string): RoamBasicNode[] =>
sortBasicNodes(
window.roamAlphaAPI.data.fast
.q(
.q<[PullBlock]>(
`[:find (pull ?c [:block/string :block/uid :block/order {:block/children ...}]) :where [?b :block/uid "${uid}"] [?b :block/children ?c]]`
)
.map((a) => a[0] as PullBlock)
.map((a) => a[0])
);

export default getBasicTreeByParentUid;
14 changes: 7 additions & 7 deletions src/queries/getBlockUidAndTextIncludingText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { PullBlock } from "../types";
const getBlockUidAndTextIncludingText = (
t: string
): { uid: string; text: string }[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?block [:block/uid :block/string]) :where [?block :block/string ?contents] [(clojure.string/includes? ?contents "${t}")]]`
) as [PullBlock][]
).map(([block]) => ({
uid: block[":block/uid"] || "",
text: block[":block/string"] || "",
}));
)
.map(([block]) => ({
uid: block[":block/uid"] || "",
text: block[":block/string"] || "",
}));

export default getBlockUidAndTextIncludingText;
4 changes: 2 additions & 2 deletions src/queries/getBlockUidByTextOnPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const getBlockUidByTextOnPage = ({
text: string;
title: string;
}): string =>
(window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[string]>(
`[:find ?u :where [?b :block/page ?p] [?b :block/uid ?u] [?b :block/string "${text}"] [?p :node/title "${title}"]]`
)?.[0]?.[0] as string) || "";
)?.[0]?.[0] || "";

export default getBlockUidByTextOnPage;
14 changes: 7 additions & 7 deletions src/queries/getBlockUidsAndTextsReferencingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import normalizePageTitle from "./normalizePageTitle";
const getBlockUidsAndTextsReferencingPage = (
title: string
): { uid: string; text: string }[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?r [:block/uid :block/string]) :where [?p :node/title "${normalizePageTitle(
title
)}"] [?r :block/refs ?p]]`
) as [PullBlock][]
).map(([node]) => ({
uid: node[":block/uid"] || "",
text: node[":block/string"] || "",
}));
)
.map(([node]) => ({
uid: node[":block/uid"] || "",
text: node[":block/string"] || "",
}));

export default getBlockUidsAndTextsReferencingPage;
4 changes: 2 additions & 2 deletions src/queries/getBlockUidsByPageTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import normalizePageTitle from "./normalizePageTitle";

const getBlockUidsByPageTitle = (title: string): string[] =>
window.roamAlphaAPI
.q(
.q<[string]>(
`[:find ?u :where [?b :block/uid ?u] [?b :block/page ?e] [?e :node/title "${normalizePageTitle(
title
)}"]]`
)
.map((b) => b[0] as string);
.map((b) => b[0]);

export default getBlockUidsByPageTitle;
8 changes: 4 additions & 4 deletions src/queries/getBlockUidsReferencingBlock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PullBlock } from "../types";

const getBlockUidsReferencingBlock = (uid: string): string[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?r [:block/uid]) :where [?b :block/uid "${uid}"] [?r :block/refs ?b]]`
) as [PullBlock][]
).map((s) => s[0][":block/uid"] || "");
)
.map((s) => s[0][":block/uid"] || "");

export default getBlockUidsReferencingBlock;
8 changes: 4 additions & 4 deletions src/queries/getBlockUidsReferencingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { PullBlock } from "../types";
import normalizePageTitle from "./normalizePageTitle";

const getBlockUidsReferencingPage = (title: string): string[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?r [:block/uid]) :where [?p :node/title "${normalizePageTitle(
title
)}"] [?r :block/refs ?p]]`
) as [PullBlock][]
).map((s) => s[0][":block/uid"] || "");
)
.map((s) => s[0][":block/uid"] || "");

export default getBlockUidsReferencingPage;
6 changes: 2 additions & 4 deletions src/queries/getBlockUidsWithParentUid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@


const getBlockUidsWithParentUid = (uid: string): string[] =>
window.roamAlphaAPI
.q(
.q<[string]>(
`[:find ?u :where [?c :block/uid ?u] [?c :block/parents ?b] [?b :block/uid "${uid}"]]`
)
.map((r) => r[0] as string);
.map((r) => r[0]);

export default getBlockUidsWithParentUid;
4 changes: 2 additions & 2 deletions src/queries/getDisplayNameByEmail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getDisplayNameByEmail = (email: string): string =>
(window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[string]>(
`[:find ?name :where[?e :user/display-name ?name] [?e :user/email "${email}"]]`
)?.[0]?.[0] as string) || "";
)?.[0]?.[0] || "";

export default getDisplayNameByEmail;
4 changes: 2 additions & 2 deletions src/queries/getDisplayNameByUid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getDisplayNameByUid = (uid: string): string =>
(window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[string]>(
`[:find ?s :where [?e :user/uid "${uid}"] [?e :user/display-page ?p] [?p :node/title ?s]]`
)?.[0]?.[0] as string) || "";
)?.[0]?.[0] || "";

export default getDisplayNameByUid;
4 changes: 2 additions & 2 deletions src/queries/getEditedUserEmailByBlockUid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getEditedUserEmailByBlockUid = (blockUid: string): string =>
(window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[string]>(
`[:find ?e :where [?u :user/email ?e] [?b :edit/user ?u] [?b :block/uid "${blockUid}"]]`
)?.[0]?.[0] as string) || "";
)?.[0]?.[0] || "";

export default getEditedUserEmailByBlockUid;
6 changes: 2 additions & 4 deletions src/queries/getFirstChildTextByBlockUid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@


const getFirstChildTextByBlockUid = (blockUid: string): string =>
window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[string]>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return undefined.

`[:find ?s :where [?c :block/string ?s] [?c :block/order 0] [?p :block/children ?c] [?p :block/uid "${blockUid}"]]`
)?.[0]?.[0] as string;
)?.[0]?.[0];

export default getFirstChildTextByBlockUid;
4 changes: 2 additions & 2 deletions src/queries/getLinkedPageTitlesUnderUid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const getLinkedPageTitlesUnderUid = (uid: string): string[] =>
window.roamAlphaAPI
.q(
.q<[string]>(
`[:find ?t :where [?r :node/title ?t] [?c :block/refs ?r] [?c :block/parents ?b] [?b :block/uid "${uid}"]]`
)
.map((r) => r[0] as string);
.map((r) => r[0]);

export default getLinkedPageTitlesUnderUid;
8 changes: 3 additions & 5 deletions src/queries/getNthChildUidByBlockUid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const getNthChildUidByBlockUid = ({
blockUid: string;
order: number;
}): string =>
(
window.roamAlphaAPI.data.fast.q(
`[:find (pull ?c [:block/uid]) :where [?p :block/uid "${blockUid}"] [?p :block/children ?c] [?c :block/order ${order}] ]`
)?.[0]?.[0] as PullBlock
)?.[":block/uid"] || "";
window.roamAlphaAPI.data.fast.q<[PullBlock]>(
`[:find (pull ?c [:block/uid]) :where [?p :block/uid "${blockUid}"] [?p :block/children ?c] [?c :block/order ${order}] ]`
)?.[0]?.[0]?.[":block/uid"] || "";

export default getNthChildUidByBlockUid;
4 changes: 2 additions & 2 deletions src/queries/getOrderByBlockUid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getOrderByBlockUid = (blockUid: string): number =>
window.roamAlphaAPI.q(
window.roamAlphaAPI.q<[number]>(
`[:find ?o :where [?r :block/order ?o] [?r :block/uid "${blockUid}"]]`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return undefined.

)?.[0]?.[0] as number;
)?.[0]?.[0];

export default getOrderByBlockUid;
8 changes: 3 additions & 5 deletions src/queries/getPageTitleByBlockUid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const getPageTitleByBlockUid = (blockUid: string): string =>
(
window.roamAlphaAPI.q(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
)?.[0]?.[0] as { title?: string }
)?.title || "";
window.roamAlphaAPI.q<[{ title: string }]>(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
)?.[0]?.[0]?.title || "";
Comment on lines +2 to +4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Type mismatch: pull result uses :node/title, not title.

The Roam pull expression (pull ?p [:node/title]) returns { ":node/title": string }, but the generic type specifies { title: string }. The access .title will always be undefined, causing the function to always return "".

🔎 Proposed fix
 const getPageTitleByBlockUid = (blockUid: string): string =>
-  window.roamAlphaAPI.q<[{ title: string }]>(
+  window.roamAlphaAPI.q<[{ ":node/title": string }]>(
     `[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
-  )?.[0]?.[0]?.title || "";
+  )?.[0]?.[0]?.[":node/title"] || "";

Alternatively, use PullBlock for consistency with other query files:

+import type { PullBlock } from "../types";
+
 const getPageTitleByBlockUid = (blockUid: string): string =>
-  window.roamAlphaAPI.q<[{ title: string }]>(
+  window.roamAlphaAPI.q<[PullBlock]>(
     `[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
-  )?.[0]?.[0]?.title || "";
+  )?.[0]?.[0]?.[":node/title"] || "";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
window.roamAlphaAPI.q<[{ title: string }]>(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
)?.[0]?.[0]?.title || "";
window.roamAlphaAPI.q<[{ ":node/title": string }]>(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
)?.[0]?.[0]?.[":node/title"] || "";
🤖 Prompt for AI Agents
In src/queries/getPageTitleByBlockUid.ts around lines 2 to 4, the Roam pull
returns an object with key ":node/title" but the code types and accesses it as
`title`, causing undefined; update the TypeScript generic to match the actual
pull shape (e.g. `{ ":node/title": string }`) and read that property, or replace
the generic with the existing `PullBlock` type used elsewhere and access the
correct field from the pulled object before returning the string; ensure the
fallback remains "" if the value is missing.


export default getPageTitleByBlockUid;
11 changes: 3 additions & 8 deletions src/queries/getPageTitleByPageUid.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { PullBlock } from "../types";

export const getPageTitleByPageUid = (pageUid: string): string =>
(
window.roamAlphaAPI.pull(`[:node/title]`, [
":block/uid",
pageUid,
]) as PullBlock
)?.[":node/title"] || "";
window.roamAlphaAPI.pull(`[:node/title]`, [":block/uid", pageUid])?.[
":node/title"
] || "";

export default getPageTitleByPageUid;
10 changes: 5 additions & 5 deletions src/queries/getPageTitleReferencesByPageTitle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PullBlock } from "../types";
import type { PullBlock } from "../types";
import normalizePageTitle from "./normalizePageTitle";

const getPageTitleReferencesByPageTitle = (title: string): string[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?b [:node/title]) :where [?r :node/title "${normalizePageTitle(
title
)}"] [?c :block/refs ?r] [?c :block/page ?b]]`
) as [PullBlock][]
).map((p) => p[0][":node/title"] as string);
)
.map((p) => p[0][":node/title"] || "");

export default getPageTitleReferencesByPageTitle;
16 changes: 8 additions & 8 deletions src/queries/getPageTitlesAndBlockUidsReferencingPage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { PullBlock } from "../types";
import type { PullBlock } from "../types";
import normalizePageTitle from "./normalizePageTitle";

const getPageTitlesAndBlockUidsReferencingPage = (
pageName: string
): { title: string; uid: string }[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock, PullBlock]>(
`[:find (pull ?pr [:node/title]) (pull ?r [:block/uid]) :where [?p :node/title "${normalizePageTitle(
pageName
)}"] [?r :block/refs ?p] [?r :block/page ?pr]]`
) as [PullBlock, PullBlock][]
).map(([pr, r]) => ({
title: pr?.[":node/title"] || "",
uid: r?.[":block/uid"] || "",
}));
)
.map(([pr, r]) => ({
title: pr[":node/title"] || "",
uid: r[":block/uid"] || "",
}));

export default getPageTitlesAndBlockUidsReferencingPage;
14 changes: 7 additions & 7 deletions src/queries/getPageTitlesAndUidsDirectlyReferencingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import normalizePageTitle from "./normalizePageTitle";
const getPageTitlesAndUidsDirectlyReferencingPage = (
pageName: string
): { title: string; uid: string }[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?r [:node/title :block/uid]) :where [?p :node/title "${normalizePageTitle(
pageName
)}"] [?r :block/refs ?p]]`
) as [PullBlock][]
).map(([block]) => ({
title: block[":node/title"] || "",
uid: block[":block/uid"] || "",
}));
)
.map(([block]) => ({
title: block[":node/title"] || "",
uid: block[":block/uid"] || "",
}));

export default getPageTitlesAndUidsDirectlyReferencingPage;
10 changes: 5 additions & 5 deletions src/queries/getPageTitlesReferencingBlockUid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PullBlock } from "../types";
import type { PullBlock } from "../types";

const getPageTitlesReferencingBlockUid = (uid: string): string[] =>
(
window.roamAlphaAPI.data.fast.q(
window.roamAlphaAPI.data.fast
.q<[PullBlock]>(
`[:find (pull ?p [:node/title]) :where [?r :block/uid "${uid}"] [?b :block/refs ?r] [?b :block/page ?p]]`
) as [PullBlock][]
).map((s) => s[0]?.[":node/title"] || "");
)
.map((s) => s[0]?.[":node/title"] || "");

export default getPageTitlesReferencingBlockUid;
Loading