-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathparser.ts
More file actions
79 lines (58 loc) · 2.02 KB
/
Copy pathparser.ts
File metadata and controls
79 lines (58 loc) · 2.02 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
69
70
71
72
73
74
75
76
77
78
79
import { Stat } from 'obsidian';
import { Item } from 'src/components/types';
export interface FileAccessor {
isEmbed: boolean;
target: string;
stats?: Stat;
}
export function markRangeForDeletion(str: string, range: { start: number; end: number }): string {
const len = str.length;
let start = range.start;
while (start > 0 && str[start - 1] === ' ') start--;
let end = range.end;
while (end < len - 1 && str[end + 1] === ' ') end++;
return str.slice(0, start) + '\u0000'.repeat(end - start) + str.slice(end);
}
export function executeDeletion(str: string) {
return str.replace(/ *\0+ */g, ' ').trim();
}
export function replaceNewLines(str: string) {
return str.trim().replace(/(?:\r\n|\n)/g, '<br>');
}
export function replaceBrs(str: string) {
return str.replace(/<br>/g, '\n').trim();
}
export function indentNewLines(str: string) {
const useTab = (app.vault as any).getConfig('useTab');
return str.trim().replace(/(?:\r\n|\n)/g, useTab ? '\n\t' : '\n ');
}
export function addBlockId(str: string, item: Item) {
if (!item.data.blockId) return str;
const lines = str.split(/(?:\r\n|\n)/g);
lines[0] += ' ^' + item.data.blockId;
return lines.join('\n');
}
export function removeBlockId(str: string) {
const lines = str.split(/(?:\r\n|\n)/g);
lines[0] = lines[0].replace(/\s+\^([a-zA-Z0-9-]+)$/, '');
return lines.join('\n');
}
export function dedentNewLines(str: string) {
return str.trim().replace(/(?:\r\n|\n)(?: {4}|\t)/g, '\n');
}
export function parseLaneTitle(str: string) {
str = replaceBrs(str);
// Check for tasks query block first
const queryMatch = str.match(/```tasks\n([\s\S]*?)\n```/);
let query: string | undefined;
if (queryMatch) {
query = queryMatch[1].trim();
// Remove the query block from the title
str = str.replace(/```tasks\n[\s\S]*?\n```/, '').trim();
}
const match = str.match(/^(.*?)\s*\((\d+)\)$/);
if (match == null) {
return { title: str, maxItems: 0, query };
}
return { title: match[1], maxItems: Number(match[2]), query };
}