-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathisCanvasPage.ts
More file actions
68 lines (63 loc) · 1.69 KB
/
Copy pathisCanvasPage.ts
File metadata and controls
68 lines (63 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { DEFAULT_CANVAS_PAGE_FORMAT } from "..";
import {
getGlobalSetting,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";
import { GLOBAL_KEYS } from "~/components/settings/utils/settingKeys";
const getCanvasPageRegex = (snapshot?: SettingsSnapshot): RegExp => {
const format =
(snapshot
? snapshot.globalSettings[GLOBAL_KEYS.canvasPageFormat]
: getGlobalSetting<string>([GLOBAL_KEYS.canvasPageFormat])) ||
DEFAULT_CANVAS_PAGE_FORMAT;
return new RegExp(`^${format}$`.replace(/\*/g, ".+"));
};
export const isCanvasPage = ({
title,
snapshot,
}: {
title: string;
snapshot?: SettingsSnapshot;
}) => {
return getCanvasPageRegex(snapshot).test(title);
};
export const isCurrentPageCanvas = ({
title,
h1,
snapshot,
}: {
title: string;
h1: HTMLHeadingElement;
snapshot: SettingsSnapshot;
}) => {
return isCanvasPage({ title, snapshot }) && !!h1.closest(".roam-article");
};
export const isSidebarCanvas = ({
title,
h1,
snapshot,
}: {
title: string;
h1: HTMLHeadingElement;
snapshot: SettingsSnapshot;
}) => {
return (
isCanvasPage({ title, snapshot }) && !!h1.closest(".rm-sidebar-outline")
);
};
export const getCanvasPageTitles = async (): Promise<string[]> => {
const regex = getCanvasPageRegex();
const escaped = regex.source.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
try {
const results = (await window.roamAlphaAPI.data.async.fast.q(`[
:find ?title
:where
[(re-pattern "${escaped}") ?regex]
[?node :node/title ?title]
[(re-find ?regex ?title)]
]`)) as [string][];
return results.map(([title]) => title).sort((a, b) => a.localeCompare(b));
} catch {
return [];
}
};