Skip to content

Commit

Permalink
handle attribute URLs (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdroidian authored Dec 31, 2024
1 parent ad962dd commit 63c609c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/components/ResultsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ const ResultRow = ({
<a
className={"rm-page-ref"}
data-link-title={getPageTitleByPageUid(uid) || ""}
href={getRoamUrl(uid)}
href={(r[`${key}-url`] as string) || getRoamUrl(uid)}
onMouseDown={(e) => {
if (e.shiftKey) {
openBlockInSidebar(uid);
Expand Down
14 changes: 13 additions & 1 deletion src/utils/predefinedSelections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 }),
};
};

Expand Down

0 comments on commit 63c609c

Please sign in to comment.