-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathfile.ts
More file actions
204 lines (177 loc) · 7.41 KB
/
file.ts
File metadata and controls
204 lines (177 loc) · 7.41 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import moment, { Moment } from "moment";
import {
FileManager,
FrontMatterCache,
getAllTags as ObsidianGetAllTags,
HeadingCache,
MetadataCache,
TagCache,
TFile,
Vault,
} from "obsidian";
import { RepItemScheduleInfo } from "src/algorithms/base/rep-item-schedule-info";
import { RepItemScheduleInfoOsr } from "src/algorithms/osr/rep-item-schedule-info-osr";
import { ALLOWED_DATE_FORMATS } from "src/constants";
import { formatDateYYYYMMDD } from "src/utils/dates";
import { parseObsidianFrontmatterTag, TextDirection } from "src/utils/strings";
// NOTE: Line numbers are zero based
export interface ISRFile {
get path(): string;
get basename(): string;
get tfile(): TFile;
setNoteSchedule(repItemScheduleInfo: RepItemScheduleInfo): Promise<void>;
getNoteSchedule(): Promise<RepItemScheduleInfo>;
getFrontmatter(): Promise<Map<string, string>>;
getAllTagsFromCache(): string[];
getAllTagsFromText(): TagCache[];
getQuestionContext(cardLine: number): string[];
getTextDirection(): TextDirection;
read(): Promise<string>;
write(content: string): Promise<void>;
}
// The Obsidian frontmatter cache doesn't include the line number for the specific tag.
// We define as -1 so that we can differentiate tags within the frontmatter and tags within the content
export const frontmatterTagPseudoLineNum: number = -1;
// NOTE: Line numbers are zero based
export class SrTFile implements ISRFile {
file: TFile;
fileManager: FileManager;
vault: Vault;
metadataCache: MetadataCache;
constructor(vault: Vault, metadataCache: MetadataCache, fileManager: FileManager, file: TFile) {
this.vault = vault;
this.metadataCache = metadataCache;
this.file = file;
this.fileManager = fileManager;
}
get path(): string {
return this.file.path;
}
get basename(): string {
return this.file.basename;
}
get tfile(): TFile {
return this.file;
}
async getNoteSchedule(): Promise<RepItemScheduleInfo> {
let result: RepItemScheduleInfo = null;
const frontmatter: Map<string, string> = await this.getFrontmatter();
if (
frontmatter &&
frontmatter.has("sr-due") &&
frontmatter.has("sr-interval") &&
frontmatter.has("sr-ease")
) {
const dueDate: Moment = moment(frontmatter.get("sr-due"), ALLOWED_DATE_FORMATS);
const interval: number = parseFloat(frontmatter.get("sr-interval"));
const ease: number = parseFloat(frontmatter.get("sr-ease"));
result = new RepItemScheduleInfoOsr(dueDate, interval, ease);
}
return result;
}
async setNoteSchedule(repItemScheduleInfo: RepItemScheduleInfo): Promise<void> {
const schedInfo: RepItemScheduleInfoOsr = repItemScheduleInfo as RepItemScheduleInfoOsr;
const dueString: string = formatDateYYYYMMDD(schedInfo.dueDate);
const interval: number = schedInfo.interval;
const ease: number = schedInfo.latestEase;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await this.fileManager.processFrontMatter(this.tfile, (frontmatter: any) => {
frontmatter["sr-due"] = dueString;
frontmatter["sr-interval"] = interval;
frontmatter["sr-ease"] = ease;
});
}
async getFrontmatter(): Promise<Map<string, string>> {
const fileCachedData = this.metadataCache.getFileCache(this.file) || {};
const frontmatter: FrontMatterCache = fileCachedData.frontmatter || {};
const result: Map<string, string> = new Map<string, string>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const [key, value] of Object.entries(frontmatter) as [string, any][]) {
const v = Array.isArray(value) && value.length > 0 ? value[0] : value;
const vStr: string = v + "";
result.set(key, vStr);
}
return result;
}
getAllTagsFromCache(): string[] {
const fileCachedData = this.metadataCache.getFileCache(this.file) || {};
const result: string[] = ObsidianGetAllTags(fileCachedData) || [];
return result;
}
getAllTagsFromText(): TagCache[] {
const result: TagCache[] = [] as TagCache[];
const fileCachedData = this.metadataCache.getFileCache(this.file) || {};
if (fileCachedData.tags?.length > 0) {
result.push(...fileCachedData.tags);
}
// RZ: 2024-01-28 fileCachedData.tags doesn't include the tags within the frontmatter, need to access those separately
// This is different to the Obsidian function getAllTags() which does return all tags including those within the
// frontmatter.
result.push(...this.getFrontmatterTags(fileCachedData.frontmatter));
return result;
}
private getFrontmatterTags(frontmatter: FrontMatterCache): TagCache[] {
const result: TagCache[] = [] as TagCache[];
const frontmatterTags: string =
frontmatter !== null && frontmatter !== undefined ? frontmatter["tags"] + "" : null;
if (frontmatterTags) {
// Parse the frontmatter tag string into a list, each entry including the leading "#"
const tagStrList: string[] = parseObsidianFrontmatterTag(frontmatterTags);
for (const str of tagStrList) {
const tag: TagCache = {
tag: str,
position: {
start: {
line: frontmatterTagPseudoLineNum,
col: null,
offset: null,
},
end: {
line: frontmatterTagPseudoLineNum,
col: null,
offset: null,
},
},
};
result.push(tag);
}
}
return result;
}
getQuestionContext(cardLine: number): string[] {
const fileCachedData = this.metadataCache.getFileCache(this.file) || {};
const headings: HeadingCache[] = fileCachedData.headings || [];
const stack: HeadingCache[] = [];
for (const heading of headings) {
if (heading.position.start.line > cardLine) {
break;
}
while (stack.length > 0 && stack[stack.length - 1].level >= heading.level) {
stack.pop();
}
stack.push(heading);
}
const result = [];
for (const headingObj of stack) {
headingObj.heading = headingObj.heading.replace(/\[\^\d+\]/gm, "").trim();
result.push(headingObj.heading);
}
return result;
}
getTextDirection(): TextDirection {
let result: TextDirection = TextDirection.Unspecified;
const fileCache = this.metadataCache.getFileCache(this.file);
const frontMatter = fileCache?.frontmatter;
if (frontMatter && frontMatter?.direction) {
const str: string = (frontMatter.direction + "").toLowerCase();
result = str === "rtl" ? TextDirection.Rtl : TextDirection.Ltr;
}
return result;
}
async read(): Promise<string> {
return await this.vault.read(this.file);
}
async write(content: string): Promise<void> {
await this.vault.modify(this.file, content);
}
}