-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
511 lines (505 loc) · 16.6 KB
/
Copy pathmain.js
File metadata and controls
511 lines (505 loc) · 16.6 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
default: () => BidirectionalPropertiesPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian3 = require("obsidian");
// src/sync-engine.ts
var import_obsidian = require("obsidian");
// src/relation-map.ts
var RELATION_MAP = {
up: "down",
down: "up",
same: "same",
next: "prev",
prev: "next"
};
var RELATION_FIELDS = Object.keys(RELATION_MAP);
function getReverseField(field) {
return RELATION_MAP[field];
}
// src/sync-engine.ts
var SyncEngine = class {
constructor(app, settings) {
this.processingFiles = /* @__PURE__ */ new Set();
this.cache = /* @__PURE__ */ new Map();
this.app = app;
this.settings = settings;
}
/**
* Update the settings reference
*/
updateSettings(settings) {
this.settings = settings;
}
/**
* Check whether a field is enabled
*/
isFieldEnabled(field) {
if (field === "up" || field === "down") {
return this.settings.enableUpDown;
}
if (field === "same") {
return this.settings.enableSame;
}
if (field === "next" || field === "prev") {
return this.settings.enableNextPrev;
}
return false;
}
/**
* Check whether a file should be excluded
*/
isExcluded(file) {
if (!this.settings.excludeFolders.trim()) {
return false;
}
const excludedFolders = this.settings.excludeFolders.split(",").map((f) => f.trim().toLowerCase()).filter((f) => f);
const filePath = file.path.toLowerCase();
return excludedFolders.some((folder) => filePath.startsWith(folder + "/"));
}
/**
* Parse wikilinks in frontmatter
*/
parseLinks(value) {
if (!value)
return [];
const items = Array.isArray(value) ? value : [value];
return items.map((item) => {
if (typeof item !== "string")
return null;
const match = item.match(/\[\[([^\]|]+)(?:\|[^\]]+)?\]\]/);
return match ? match[1] : null;
}).filter((x) => x !== null);
}
/**
* Extract relation data from a file
*/
extractRelations(file) {
const cache = this.app.metadataCache.getFileCache(file);
const frontmatter = cache == null ? void 0 : cache.frontmatter;
if (!frontmatter)
return {};
const relations = {};
for (const field of RELATION_FIELDS) {
if (frontmatter[field]) {
relations[field] = this.parseLinks(frontmatter[field]);
}
}
return relations;
}
/**
* Compute the diff between two sets of relation data
*/
diffRelations(current, previous) {
const added = {};
const removed = {};
for (const field of RELATION_FIELDS) {
const currentLinks = current[field] || [];
const previousLinks = previous[field] || [];
const addedLinks = currentLinks.filter((l) => !previousLinks.includes(l));
const removedLinks = previousLinks.filter((l) => !currentLinks.includes(l));
if (addedLinks.length > 0)
added[field] = addedLinks;
if (removedLinks.length > 0)
removed[field] = removedLinks;
}
return { added, removed };
}
/**
* Find the file corresponding to a note name
*/
findFile(noteName) {
const files = this.app.vault.getMarkdownFiles();
let file = files.find((f) => f.basename === noteName);
if (file)
return file;
file = files.find((f) => f.name === noteName + ".md");
if (file)
return file;
return null;
}
/**
* Add a link to the specified field in the target file
*/
async addToField(targetNoteName, field, linkToAdd) {
const targetFile = this.findFile(targetNoteName);
if (!targetFile) {
console.log(`[Bidirectional] Target file not found: ${targetNoteName}`);
return;
}
if (this.isExcluded(targetFile))
return;
if (this.processingFiles.has(targetFile.path))
return;
const content = await this.app.vault.read(targetFile);
const linkValue = `"[[${linkToAdd}]]"`;
const currentRelations = this.extractRelations(targetFile);
const existingLinks = currentRelations[field] || [];
if (existingLinks.includes(linkToAdd))
return;
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) {
const newFrontmatter = `---
${field}:
- ${linkValue}
---
`;
await this.app.vault.modify(targetFile, newFrontmatter + content);
return;
}
const frontmatterContent = frontmatterMatch[1];
const restContent = content.slice(frontmatterMatch[0].length);
const fieldRegex = new RegExp(`^${field}:`, "m");
if (fieldRegex.test(frontmatterContent)) {
const updatedFrontmatter = frontmatterContent.replace(
new RegExp(`(${field}:.*?)(
(?=[a-zA-Z]|---)|$)`, "s"),
(match, fieldPart, ending) => {
if (fieldPart.includes("\n -")) {
return fieldPart + `
- ${linkValue}` + ending;
} else {
const existingValue = fieldPart.replace(`${field}:`, "").trim();
if (existingValue) {
return `${field}:
- ${existingValue}
- ${linkValue}` + ending;
} else {
return `${field}:
- ${linkValue}` + ending;
}
}
}
);
await this.app.vault.modify(
targetFile,
`---
${updatedFrontmatter}
---${restContent}`
);
} else {
const updatedFrontmatter = frontmatterContent + `
${field}:
- ${linkValue}`;
await this.app.vault.modify(
targetFile,
`---
${updatedFrontmatter}
---${restContent}`
);
}
}
/**
* Remove a link from the specified field in the target file
*/
async removeFromField(targetNoteName, field, linkToRemove) {
const targetFile = this.findFile(targetNoteName);
if (!targetFile)
return;
if (this.isExcluded(targetFile))
return;
if (this.processingFiles.has(targetFile.path))
return;
const content = await this.app.vault.read(targetFile);
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
if (!frontmatterMatch)
return;
const frontmatterContent = frontmatterMatch[1];
const restContent = content.slice(frontmatterMatch[0].length);
const patterns = [
new RegExp(`\\n\\s*-\\s*"?\\[\\[${linkToRemove}(\\|[^\\]]+)?\\]\\]"?`, "g"),
new RegExp(`\\n\\s*-\\s*"?${linkToRemove}"?(?=\\n|$)`, "g")
];
let updatedFrontmatter = frontmatterContent;
for (const pattern of patterns) {
updatedFrontmatter = updatedFrontmatter.replace(pattern, "");
}
updatedFrontmatter = updatedFrontmatter.replace(
new RegExp(`${field}:\\s*\\n(?=\\S|$)`, "g"),
""
);
if (updatedFrontmatter !== frontmatterContent) {
await this.app.vault.modify(
targetFile,
`---
${updatedFrontmatter}
---${restContent}`
);
}
}
/**
* Handle the file modify event
*/
async onFileModify(file) {
if (!file.path.endsWith(".md"))
return;
if (this.isExcluded(file))
return;
if (this.processingFiles.has(file.path))
return;
this.processingFiles.add(file.path);
try {
const currentRelations = this.extractRelations(file);
const previousRelations = this.cache.get(file.path) || {};
const { added, removed } = this.diffRelations(currentRelations, previousRelations);
let syncCount = 0;
for (const [field, targets] of Object.entries(added)) {
if (!this.isFieldEnabled(field))
continue;
const reverseField = getReverseField(field);
if (!reverseField)
continue;
for (const target of targets) {
await this.addToField(target, reverseField, file.basename);
syncCount++;
}
}
for (const [field, targets] of Object.entries(removed)) {
if (!this.isFieldEnabled(field))
continue;
const reverseField = getReverseField(field);
if (!reverseField)
continue;
for (const target of targets) {
await this.removeFromField(target, reverseField, file.basename);
syncCount++;
}
}
this.cache.set(file.path, currentRelations);
if (syncCount > 0 && this.settings.showNotifications) {
new import_obsidian.Notice(`Synced ${syncCount} bidirectional relation(s)`);
}
} finally {
setTimeout(() => {
this.processingFiles.delete(file.path);
}, 100);
}
}
/**
* Initialize the cache (load relation data for all files)
*/
async initializeCache() {
const files = this.app.vault.getMarkdownFiles();
for (const file of files) {
if (!this.isExcluded(file)) {
const relations = this.extractRelations(file);
this.cache.set(file.path, relations);
}
}
}
/**
* Full scan and fix inconsistencies
*/
async scanAndFix() {
const files = this.app.vault.getMarkdownFiles();
let fixCount = 0;
for (const file of files) {
if (this.isExcluded(file))
continue;
const relations = this.extractRelations(file);
for (const [field, targets] of Object.entries(relations)) {
if (!this.isFieldEnabled(field))
continue;
const reverseField = getReverseField(field);
if (!reverseField)
continue;
for (const target of targets) {
const targetFile = this.findFile(target);
if (!targetFile || this.isExcluded(targetFile))
continue;
const targetRelations = this.extractRelations(targetFile);
const reverseLinks = targetRelations[reverseField] || [];
if (!reverseLinks.includes(file.basename)) {
await this.addToField(target, reverseField, file.basename);
fixCount++;
}
}
}
}
return fixCount;
}
/**
* Handle the file delete event
*/
async onFileDelete(file) {
if (!file.path.endsWith(".md"))
return;
const cachedRelations = this.cache.get(file.path);
if (!cachedRelations)
return;
for (const [field, targets] of Object.entries(cachedRelations)) {
const reverseField = getReverseField(field);
if (!reverseField)
continue;
for (const target of targets) {
await this.removeFromField(target, reverseField, file.basename);
}
}
this.cache.delete(file.path);
}
/**
* Handle the file rename event
*/
async onFileRename(file, oldPath) {
var _a;
if (!file.path.endsWith(".md"))
return;
const oldBasename = ((_a = oldPath.split("/").pop()) == null ? void 0 : _a.replace(".md", "")) || "";
const newBasename = file.basename;
if (oldBasename === newBasename)
return;
const cachedRelations = this.cache.get(oldPath);
if (!cachedRelations)
return;
for (const [field, targets] of Object.entries(cachedRelations)) {
const reverseField = getReverseField(field);
if (!reverseField)
continue;
for (const target of targets) {
await this.removeFromField(target, reverseField, oldBasename);
await this.addToField(target, reverseField, newBasename);
}
}
this.cache.delete(oldPath);
this.cache.set(file.path, this.extractRelations(file));
}
};
// src/settings.ts
var import_obsidian2 = require("obsidian");
var DEFAULT_SETTINGS = {
enableUpDown: true,
enableSame: true,
enableNextPrev: true,
syncOnStartup: false,
showNotifications: true,
excludeFolders: ""
};
var BidirectionalPropertiesSettingTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Bidirectional Properties Settings" });
containerEl.createEl("h3", { text: "Enabled Fields" });
new import_obsidian2.Setting(containerEl).setName("up \u2194 down").setDesc("Sync parent-child relationships").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.enableUpDown).onChange(async (value) => {
this.plugin.settings.enableUpDown = value;
await this.plugin.saveSettings();
})
);
new import_obsidian2.Setting(containerEl).setName("same \u2194 same").setDesc("Sync sibling relationships (symmetric)").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.enableSame).onChange(async (value) => {
this.plugin.settings.enableSame = value;
await this.plugin.saveSettings();
})
);
new import_obsidian2.Setting(containerEl).setName("next \u2194 prev").setDesc("Sync sequential relationships").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.enableNextPrev).onChange(async (value) => {
this.plugin.settings.enableNextPrev = value;
await this.plugin.saveSettings();
})
);
containerEl.createEl("h3", { text: "Behavior" });
new import_obsidian2.Setting(containerEl).setName("Sync on startup").setDesc("Scan and fix all inconsistencies when Obsidian starts").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.syncOnStartup).onChange(async (value) => {
this.plugin.settings.syncOnStartup = value;
await this.plugin.saveSettings();
})
);
new import_obsidian2.Setting(containerEl).setName("Show notifications").setDesc("Show notices when syncing properties").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.showNotifications).onChange(async (value) => {
this.plugin.settings.showNotifications = value;
await this.plugin.saveSettings();
})
);
containerEl.createEl("h3", { text: "Exclusions" });
new import_obsidian2.Setting(containerEl).setName("Exclude folders").setDesc("Comma-separated list of folders to exclude (e.g., Templates, Archive)").addText(
(text) => text.setPlaceholder("Templates, Archive").setValue(this.plugin.settings.excludeFolders).onChange(async (value) => {
this.plugin.settings.excludeFolders = value;
await this.plugin.saveSettings();
})
);
}
};
// src/main.ts
var BidirectionalPropertiesPlugin = class extends import_obsidian3.Plugin {
async onload() {
console.log("Loading Bidirectional Properties plugin");
await this.loadSettings();
this.syncEngine = new SyncEngine(this.app, this.settings);
this.addSettingTab(new BidirectionalPropertiesSettingTab(this.app, this));
this.registerEvent(
this.app.vault.on("modify", (file) => {
if (file instanceof import_obsidian3.TFile) {
this.syncEngine.onFileModify(file);
}
})
);
this.registerEvent(
this.app.vault.on("delete", (file) => {
if (file instanceof import_obsidian3.TFile) {
this.syncEngine.onFileDelete(file);
}
})
);
this.registerEvent(
this.app.vault.on("rename", (file, oldPath) => {
if (file instanceof import_obsidian3.TFile) {
this.syncEngine.onFileRename(file, oldPath);
}
})
);
this.addCommand({
id: "sync-all-bidirectional-properties",
name: "Sync all bidirectional properties",
callback: async () => {
new import_obsidian3.Notice("Scanning for inconsistencies...");
const fixCount = await this.syncEngine.scanAndFix();
new import_obsidian3.Notice(`Fixed ${fixCount} inconsistent relation(s)`);
}
});
this.app.workspace.onLayoutReady(async () => {
await this.syncEngine.initializeCache();
if (this.settings.syncOnStartup) {
const fixCount = await this.syncEngine.scanAndFix();
if (fixCount > 0 && this.settings.showNotifications) {
new import_obsidian3.Notice(`Bidirectional Properties: Fixed ${fixCount} inconsistencies`);
}
}
});
}
onunload() {
console.log("Unloading Bidirectional Properties plugin");
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
if (this.syncEngine) {
this.syncEngine.updateSettings(this.settings);
}
}
};