-
-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathmarkdown.ts
More file actions
391 lines (335 loc) · 14.2 KB
/
Copy pathmarkdown.ts
File metadata and controls
391 lines (335 loc) · 14.2 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import { extractSubtags, getExtension, getFileTitle, getParentFolder, stripTime } from "util/normalize";
import { DateTime } from "luxon";
import type { FullIndex } from "data-index/index";
import { Literal, Link, Values } from "data-model/value";
import { DataObject } from "index";
import { SListItem, SMarkdownPage } from "data-model/serialized/markdown";
import { Pos } from "obsidian";
/** All extracted markdown file metadata obtained from a file. */
export class PageMetadata {
/** The path this file exists at. */
public path: string;
/** Obsidian-provided date this page was created. */
public ctime: DateTime;
/** Obsidian-provided date this page was modified. */
public mtime: DateTime;
/** Obsidian-provided size of this page in bytes. */
public size: number;
/** The day associated with this page, if relevant. */
public day?: DateTime;
/** The first H1/H2 header in the file. May not exist. */
public title?: string;
/** All of the fields contained in this markdown file - both frontmatter AND in-file links. */
public fields: Map<string, Literal>;
/** All of the exact tags (prefixed with '#') in this file overall. */
public tags: Set<string>;
/** All of the aliases defined for this file. */
public aliases: Set<string>;
/** All OUTGOING links (including embeds, header + block links) in this file. */
public links: Link[];
/** All list items contained within this page. Filter for tasks to get just tasks. */
public lists: ListItem[];
/** The raw frontmatter for this document. */
public frontmatter: Record<string, Literal>;
public tables: TableItem[];
public constructor(path: string, init?: Partial<PageMetadata>) {
this.path = path;
this.fields = new Map<string, Literal>();
this.frontmatter = {};
this.tags = new Set<string>();
this.aliases = new Set<string>();
this.links = [];
this.tables = [];
Object.assign(this, init);
this.lists = (this.lists || []).map(l => new ListItem(l));
}
/** Canonicalize raw links and other data in partial data with normalizers, returning a completed object. */
public static canonicalize(data: Partial<PageMetadata>, linkNormalizer: (link: Link) => Link): PageMetadata {
// Mutate the data for now, which is probably a bad idea but... all well.
if (data.frontmatter) {
data.frontmatter = Values.mapLeaves(data.frontmatter, t =>
Values.isLink(t) ? linkNormalizer(t) : t
) as DataObject;
}
if (data.fields) {
for (let [key, value] of data.fields.entries()) {
data.fields.set(
key,
Values.mapLeaves(value, t => (Values.isLink(t) ? linkNormalizer(t) : t))
);
}
}
if (data.lists) {
for (let item of data.lists) {
for (let [key, value] of item.fields.entries()) {
item.fields.set(
key,
value.map(x => Values.mapLeaves(x, t => (Values.isLink(t) ? linkNormalizer(t) : t)))
);
}
}
}
if (data.links) {
data.links = data.links.map(l => linkNormalizer(l));
}
// This is pretty ugly, but it's not possible to normalize on the worker thread that does parsing.
// The best way to improve this is to instead just canonicalize the entire data object; I can try to
// optimize `Values.mapLeaves` to only mutate if it actually changes things.
return new PageMetadata(data.path!!, data);
}
/** The name (based on path) of this file. */
public name(): string {
return getFileTitle(this.path);
}
/** The containing folder (based on path) of this file. */
public folder(): string {
return getParentFolder(this.path);
}
/** The extension of this file (likely 'md'). */
public extension(): string {
return getExtension(this.path);
}
/** Return a set of tags AND all of their parent tags (so #hello/yes would become #hello, #hello/yes). */
public fullTags(): Set<string> {
let result = new Set<string>();
for (let tag of this.tags) {
for (let subtag of extractSubtags(tag)) result.add(subtag);
}
return result;
}
/** Convert all links in this file to file links. */
public fileLinks(): Link[] {
// We want to make them distinct, but where links are not raw links we
// now keep the additional metadata.
let distinctLinks = new Set<Link>(this.links);
return Array.from(distinctLinks);
}
/** Map this metadata to a full object; uses the index for additional data lookups. */
public serialize(index: FullIndex, cache?: ListSerializationCache): SMarkdownPage {
// Convert list items via the canonicalization cache.
let realCache = cache ?? new ListSerializationCache(this.lists);
let result: any = {
file: {
path: this.path,
folder: this.folder(),
name: this.name(),
link: Link.file(this.path),
outlinks: this.fileLinks(),
inlinks: Array.from(index.links.getInverse(this.path)).map(l => Link.file(l)),
etags: Array.from(this.tags),
tags: Array.from(this.fullTags()),
aliases: Array.from(this.aliases),
lists: this.lists.map(l => realCache.get(l.line)),
tasks: this.lists.filter(l => !!l.task).map(l => realCache.get(l.line)),
tables: this.tables,
ctime: this.ctime,
cday: stripTime(this.ctime),
mtime: this.mtime,
mday: stripTime(this.mtime),
size: this.size,
starred: index.starred.starred(this.path),
frontmatter: Values.deepCopy(this.frontmatter),
ext: this.extension(),
},
};
// Add the current day if present.
if (this.day) result.file.day = this.day;
// Then append the computed fields.
for (let [key, value] of this.fields.entries()) {
if (key in result) continue; // Don't allow fields to override existing keys.
result[key] = value;
}
return result;
}
}
/** A list item inside of a list. */
export class ListItem {
/** The symbol ('*', '-', '1.') used to define this list item. */
symbol: string;
/** A link which points to this task, or to the closest block that this task is contained in. */
link: Link;
/** A link to the section that contains this list element; could be a file if this is not in a section. */
section: Link;
/** The text of this list item. This may be multiple lines of markdown. */
text: string;
/** The line that this list item starts on in the file. */
line: number;
/** The number of lines that define this list item. */
lineCount: number;
/** The line number for the first list item in the list this item belongs to. */
list: number;
/** Any links contained within this list item. */
links: Link[];
/** The tags contained within this list item. */
tags: Set<string>;
/** The raw Obsidian-provided position for where this task is. */
position: Pos;
/** The line number of the parent list item, if present; if this is undefined, this is a root item. */
parent?: number;
/** The line numbers of children of this list item. */
children: number[];
/** The block ID for this item, if one is present. */
blockId?: string;
/** Any fields defined in this list item. For tasks, this includes fields underneath the task. */
fields: Map<string, Literal[]>;
task?: {
/** The text in between the brackets of the '[ ]' task indicator ('[X]' would yield 'X', for example.) */
status: string;
/** Whether or not this task has been checked in any way (it's status is not empty/space). */
checked: boolean;
/** Whether or not this task was completed; derived from 'status' by checking if the field 'X' or 'x'. */
completed: boolean;
/** Whether or not this task and all of it's subtasks are completed. */
fullyCompleted: boolean;
};
public constructor(init?: Partial<ListItem>) {
Object.assign(this, init);
this.fields = this.fields || new Map();
this.tags = this.tags || new Set();
this.children = this.children || [];
this.links = this.links || [];
}
public id(): string {
return `${this.file().path}-${this.line}`;
}
public file(): Link {
return this.link.toFile();
}
public markdown(): string {
if (this.task) return `${this.symbol} [${this.task.completed ? "x" : " "}] ${this.text}`;
else return `${this.symbol} ${this.text}`;
}
public created(): Literal | undefined {
return (this.fields.get("created") ?? this.fields.get("ctime") ?? this.fields.get("cday"))?.[0];
}
public due(): Literal | undefined {
return (this.fields.get("due") ?? this.fields.get("duetime") ?? this.fields.get("dueday"))?.[0];
}
public completed(): Literal | undefined {
return (this.fields.get("completed") ??
this.fields.get("completion") ??
this.fields.get("comptime") ??
this.fields.get("compday"))?.[0];
}
public start(): Literal | undefined {
return this.fields.get("start")?.[0];
}
public scheduled(): Literal | undefined {
return this.fields.get("scheduled")?.[0];
}
/** Create an API-friendly copy of this list item. De-duplication is done via the provided cache. */
public serialize(cache: ListSerializationCache): SListItem {
// Map children to their serialized/de-duplicated equivalents right away.
let children = this.children.map(l => cache.get(l)).filter((l): l is SListItem => l !== undefined);
let result: DataObject = {
symbol: this.symbol,
link: this.link,
section: this.section,
text: this.text,
tags: Array.from(this.tags),
line: this.line,
lineCount: this.lineCount,
list: this.list,
outlinks: Array.from(this.links),
path: this.link.path,
children: children,
task: !!this.task,
annotated: this.fields.size > 0,
position: Values.deepCopy(this.position as any),
subtasks: children, // @deprecated, use 'item.children' instead.
real: !!this.task, // @deprecated, use 'item.task' instead.
header: this.section, // @deprecated, use 'item.section' instead.
};
if (this.parent || this.parent === 0) result.parent = this.parent;
if (this.blockId) result.blockId = this.blockId;
addFields(this.fields, result);
if (this.task) {
result.status = this.task.status;
result.checked = this.task.checked;
result.completed = this.task.completed;
result.fullyCompleted = this.task.fullyCompleted;
let created = this.created(),
due = this.due(),
completed = this.completed(),
start = this.start(),
scheduled = this.scheduled();
if (created) result.created = Values.deepCopy(created);
if (due) result.due = Values.deepCopy(due);
if (completed) result.completion = Values.deepCopy(completed);
if (start) result.start = Values.deepCopy(start);
if (scheduled) result.scheduled = Values.deepCopy(scheduled);
}
return result as SListItem;
}
}
/** A table item to represent the table */
export class TableItem {
headers: string[];
rows: any[][];
json: Map<string, any>[];
constructor(init?: Partial<TableItem>) {
Object.assign(this, init);
this.headers = init?.headers || [];
this.rows = init?.rows || [];
this.json = init?.json || [];
}
// make table data into array of object
public static serialize(headers: string[], rows: any[][]): Map<string, any>[] {
// show header to empty value for empty rows.
if (rows.length === 0) {
return [{
headers,
rows,
}] as any;
}
const result: Map<string, any>[] = [];
rows.forEach(row => {
if (row.length === headers.length) {
// only include for row that has the same amount of column
const record = headers.reduce((prev, key, index) => {
prev[key] = row[index];
return prev;
}, {} as any);
result.push(record);
}
});
return result;
}
}
//////////////////////////////////////////
// Conversion / Serialization Utilities //
//////////////////////////////////////////
/** De-duplicates list items across section metadata and page metadata. */
export class ListSerializationCache {
public listItems: Record<number, ListItem>;
public cache: Record<number, SListItem>;
public seen: Set<number>;
public constructor(listItems: ListItem[]) {
this.listItems = {};
this.cache = {};
this.seen = new Set();
for (let item of listItems) this.listItems[item.line] = item;
}
public get(lineno: number): SListItem | undefined {
if (lineno in this.cache) return this.cache[lineno];
else if (this.seen.has(lineno)) {
console.log(
`Dataview: Encountered a circular list (line number ${lineno}; children ${this.listItems[
lineno
].children.join(", ")})`
);
return undefined;
}
this.seen.add(lineno);
let result = this.listItems[lineno].serialize(this);
this.cache[lineno] = result;
return result;
}
}
export function addFields(fields: Map<string, Literal[]>, target: DataObject): DataObject {
for (let [key, values] of fields.entries()) {
if (key in target) continue;
target[key] = values.length == 1 ? values[0] : values;
}
return target;
}