generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
279 lines (247 loc) · 6.95 KB
/
main.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
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
import moment from "moment";
import {
App,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
TFolder,
} from "obsidian";
import * as path from "path";
interface LinkedUniqueNoteSettings {
dateFormat: string;
headerFormat: string;
// I'm too lazy to change this to a proper setting
template: string;
}
const DEFAULT_SETTINGS: LinkedUniqueNoteSettings = {
dateFormat: "YYYY-MM-DD \\at HHːmm",
headerFormat:
'<span style="display: block;text-align: left;">←{prev}</span> <span style="display: block;text-align: right;">{next}→</span>\n\n',
template: "#zettelkasten \n\n",
};
export default class LinkedUniqueNote extends Plugin {
settings: LinkedUniqueNoteSettings;
async jumpToLast(folder: TFolder) {
const last = folder.children
.filter<TFile>((v): v is TFile => v instanceof TFile)
.map((file) => {
const t: [moment.Moment, TFile] = [
moment(file.name, this.settings.dateFormat),
file,
];
return t;
})
.filter((v) => v[0].isValid())
.sort((a, b) => a[0].valueOf() - b[0].valueOf())
.last();
if (last) {
await this.app.workspace.getLeaf().openFile(last[1]);
}
}
async newUniqueNote(folder: TFolder) {
const newName = moment().format(this.settings.dateFormat);
const prev_ = folder.children
.filter<TFile>((v): v is TFile => v instanceof TFile)
.map((file) => {
const t: [moment.Moment, TFile] = [
moment(file.name, this.settings.dateFormat),
file,
];
return t;
})
.filter((v) => v[0].isValid())
.sort((a, b) => a[0].valueOf() - b[0].valueOf())
.last();
const prevHandle = prev_ === undefined ? undefined : prev_[1];
const prevName = prevHandle?.name ?? newName;
const nextName = newName;
const adapter = this.app.vault.adapter;
const newPath = path.join(folder.path, newName + ".md");
if (await adapter.exists(newPath)) {
new Notice(`File ${newPath} already exists`);
}
const header = this.settings.headerFormat
.replace(
new RegExp("\\{prev\\}", "gi"),
`[[${prevName.replace(".md", "")}]]`
)
.replace(new RegExp("\\{next\\}", "gi"), `[[${nextName}]]`);
const data = this.settings.template + header;
const newFile = await this.app.vault.create(newPath, data);
await this.app.workspace.getLeaf().openFile(newFile);
// update previous note
if (!prevHandle) return;
const text = (await adapter.read(prevHandle.path)).replace(
prevHandle.name.replace(".md", ""),
newName
);
const stat = await adapter.stat(prevHandle.path);
const writeOptions = {
ctime: stat?.ctime,
mtime: stat?.mtime,
};
await adapter.write(prevHandle.path, text, writeOptions);
}
async conformFolder(folder: TFolder) {
const children = folder.children
.filter<TFile>((v): v is TFile => v instanceof TFile)
.map((file) => {
const t: [moment.Moment, TFile] = [
moment(file.name, this.settings.dateFormat),
file,
];
return t;
})
.filter((v) => v[0].isValid())
.sort((a, b) => a[0].valueOf() - b[0].valueOf());
let prev = children[0];
let nextIdx = 1;
for (const child of children) {
const next = children[nextIdx];
const prevHandle = prev[1];
const nextHandle = next[1];
const prevName = prevHandle.name;
const nextName = nextHandle.name;
const adapter = this.app.vault.adapter;
const header = this.settings.headerFormat
.replace(
new RegExp("\\{prev\\}", "gi"),
`[[${prevName.replace(".md", "")}]]`
)
.replace(
new RegExp("\\{next\\}", "gi"),
`[[${nextName.replace(".md", "")}]]`
);
const data = this.settings.template + header;
const childPath = child[1].path;
// update previous note
const text = (await adapter.read(childPath)).replace(
/#zettelkasten(.|\n)*span>/,
data
);
const stat = await adapter.stat(childPath);
const writeOptions = {
ctime: stat?.ctime,
mtime: stat?.mtime,
};
await adapter.write(childPath, text, writeOptions);
prev = child;
nextIdx += 1;
if (nextIdx >= children.length) {
nextIdx = children.length - 1;
}
}
}
async onload() {
await this.loadSettings();
this.registerEvent(
app.workspace.on("file-menu", (menu, file, source, leaf) => {
if (file instanceof TFolder) {
menu.addItem((item) => {
item.setTitle("New unique note")
// .setIcon(kanbanIcon)
.onClick(() => this.newUniqueNote(file));
});
return;
}
if (file instanceof TFile) {
const parent = file.parent;
menu.addItem((item) => {
item.setTitle("New unique note")
// .setIcon(kanbanIcon)
.onClick(() => this.newUniqueNote(parent));
});
}
})
);
this.registerEvent(
app.workspace.on("file-menu", (menu, file, source, leaf) => {
if (file instanceof TFolder) {
menu.addItem((item) => {
item.setTitle("Jump to last unique note")
// .setIcon(kanbanIcon)
.onClick(() => this.jumpToLast(file));
});
return;
}
if (file instanceof TFile) {
const parent = file.parent;
menu.addItem((item) => {
item.setTitle("Jump to last unique note")
// .setIcon(kanbanIcon)
.onClick(() => this.jumpToLast(parent));
});
}
})
);
// Vestigial code for my own purposes
// Enable if you know what you want, this links *all* files with a proper
// name format chronologically
// It is NOT idempotent at all
// eslint-disable-next-line no-constant-condition
if (false) {
this.registerEvent(
app.workspace.on("file-menu", (menu, file, source, leaf) => {
if (file instanceof TFolder) {
menu.addItem((item) => {
item.setTitle(
"Conform folder to linked unique note"
).onClick(() => this.conformFolder(file));
});
return;
}
})
);
}
this.addSettingTab(new LinkedUniqueSettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class LinkedUniqueSettingTab extends PluginSettingTab {
plugin: LinkedUniqueNote;
constructor(app: App, plugin: LinkedUniqueNote) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Unique linked note - settings" });
new Setting(containerEl)
.setName("Date format")
.setDesc("Date format used for ordering your notes")
.addText((text) =>
text
.setPlaceholder("")
.setValue(this.plugin.settings.dateFormat)
.onChange(async (value) => {
this.plugin.settings.dateFormat = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Header format format")
.setDesc("Header format used for ordering your notes")
.addText((text) =>
text
.setPlaceholder("")
.setValue(this.plugin.settings.headerFormat)
.onChange(async (value) => {
this.plugin.settings.headerFormat = value;
await this.plugin.saveSettings();
})
);
}
}