generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
40 lines (34 loc) · 1004 Bytes
/
helpers.ts
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
export const debounce = <T extends (...args: any[]) => void>(
fn: T,
delay: number
) => {
let timeoutID: ReturnType<typeof setTimeout> | undefined = undefined;
return (...args: Parameters<T>) => {
clearTimeout(timeoutID);
timeoutID = setTimeout(() => fn(...args), delay);
};
};
const obsidianImageWikiRegex = /!\[\[.+\.(png|jpg|jpeg|webp|gif)\s*\|(.+)\]\]/g;
const arbitaryValueRegex = /^w-(.+)/g;
export const collectSizeArbitraryValues = (texts: string[]) => {
const matchedArbitaryValues: string[] = [];
for (let te = 0; te < texts.length; te++) {
const text = texts[te];
const matchs = obsidianImageWikiRegex.exec(text);
if (matchs) {
const [, , match] = matchs;
match
.split(" ")
.map((m) => m.trim())
.filter(Boolean)
.forEach((m) => {
const arbitaryMatch = arbitaryValueRegex.exec(m);
if (arbitaryMatch) {
const [, value] = arbitaryMatch;
matchedArbitaryValues.push(value);
}
});
}
}
return matchedArbitaryValues;
};