From f2ebe3407c6184076580251d25383d06e7c8d342 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Mon, 30 Dec 2024 14:39:11 -0600 Subject: [PATCH] handle attribute URLs --- src/components/ResultsTable.tsx | 2 +- src/utils/predefinedSelections.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/components/ResultsTable.tsx b/src/components/ResultsTable.tsx index 69ea52a2..0da2ce9b 100644 --- a/src/components/ResultsTable.tsx +++ b/src/components/ResultsTable.tsx @@ -307,7 +307,7 @@ const ResultRow = ({ { if (e.shiftKey) { openBlockInSidebar(uid); diff --git a/src/utils/predefinedSelections.ts b/src/utils/predefinedSelections.ts index 355b7b08..719e9ffd 100644 --- a/src/utils/predefinedSelections.ts +++ b/src/utils/predefinedSelections.ts @@ -117,6 +117,15 @@ const formatDate = ({ const flatten = (blocks: PullBlock[] = []): PullBlock[] => blocks.flatMap((b) => [b, ...flatten(b[":block/children"])]); +const isValidUrl = (value: string): boolean => { + try { + new URL(value); + return true; + } catch { + return false; + } +}; + const getBlockAttribute = (key: string, r: PullBlock) => { const blocks = flatten( window.roamAlphaAPI.pull( @@ -127,9 +136,12 @@ const getBlockAttribute = (key: string, r: PullBlock) => { const block = blocks.find((blk) => (blk[":block/string"] || "").startsWith(key + "::") ); + const value = (block?.[":block/string"] || "").slice(key.length + 2).trim(); + return { - "": (block?.[":block/string"] || "").slice(key.length + 2).trim(), + "": value, "-uid": block?.[":block/uid"] || "", + ...(isValidUrl(value) && { "-url": value }), }; };