forked from MichaBrugger/obsidian-footnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
408 lines (351 loc) · 12.5 KB
/
Copy pathmain.ts
File metadata and controls
408 lines (351 loc) · 12.5 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
import {
addIcon,
Editor,
EditorPosition,
EditorSuggest,
EditorSuggestContext,
EditorSuggestTriggerInfo,
MarkdownView,
Plugin
} from "obsidian";
//Add chevron-up-square icon from lucide for mobile toolbar (temporary until Obsidian updates to Lucide v0.130.0)
addIcon("chevron-up-square", `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-up-square"><rect width="18" height="18" x="3" y="3" rx="2" ry="2"></rect><polyline points="8,14 12,10 16,14"></polyline></svg>`);
export default class MyPlugin extends Plugin {
private AllMarkers = /\[\^([^\[\]]+)\](?!:)/dg;
private AllNumberedMarkers = /\[\^(\d+)\]/gi;
private AllDetailsNameOnly = /\[\^([^\[\]]+)\]:/g;
private DetailInLine = /\[\^([^\[\]]+)\]:/;
private ExtractNameFromFootnote = /(\[\^)([^\[\]]+)(?=\])/;
async onload() {
this.addCommand({
id: "insert-autonumbered-footnote",
name: "Insert / Navigate Auto-Numbered Footnote",
icon: "plus-square",
checkCallback: (checking: boolean) => {
if (checking)
return !!this.app.workspace.getActiveViewOfType(MarkdownView);
this.insertAutonumFootnote();
},
});
this.addCommand({
id: "insert-named-footnote",
name: "Insert / Navigate Named Footnote",
icon: "chevron-up-square",
checkCallback: (checking: boolean) => {
if (checking)
return !!this.app.workspace.getActiveViewOfType(MarkdownView);
this.insertNamedFootnote();
}
});
}
//UNIVERSAL FUNCTIONS
private listExistingFootnoteDetails(
doc: Editor
) {
let FootnoteDetailList: string[] = [];
//search each line for footnote details and add to list
for (let i = 0; i < doc.lineCount(); i++) {
let theLine = doc.getLine(i);
let lineMatch = theLine.match(this.AllDetailsNameOnly);
if (lineMatch) {
let temp = lineMatch[0];
temp = temp.replace("[^","");
temp = temp.replace("]:","");
FootnoteDetailList.push(temp);
}
}
if (FootnoteDetailList.length > 0) {
return FootnoteDetailList;
} else {
return null;
}
}
private listExistingFootnoteMarkersAndLocations(
doc: Editor
) {
type markerEntry = {
footnote: string;
lineNum: number;
startIndex: number;
}
let markerEntry;
let FootnoteMarkerInfo = [];
//search each line for footnote markers
//for each, add their name, line number, and start index to FootnoteMarkerInfo
for (let i = 0; i < doc.lineCount(); i++) {
let theLine = doc.getLine(i);
let lineMatch;
while ((lineMatch = this.AllMarkers.exec(theLine)) != null) {
markerEntry = {
footnote: lineMatch[0],
lineNum: i,
startIndex: lineMatch.index
}
FootnoteMarkerInfo.push(markerEntry);
}
}
return FootnoteMarkerInfo;
}
private shouldJumpFromDetailToMarker(
lineText: string,
cursorPosition: EditorPosition,
doc: Editor
) {
// check if we're in a footnote detail line ("[^1]: footnote")
// if so, jump cursor back to the footnote in the text
// https://github.com/akaalias/obsidian-footnotes#improved-quick-navigation
let match = lineText.match(this.DetailInLine);
if (match) {
let s = match[0];
let index = s.replace("[^", "");
index = index.replace("]:", "");
let footnote = s.replace(":", "");
let returnLineIndex = cursorPosition.line;
// find the FIRST OCCURENCE where this footnote exists in the text
for (let i = 0; i < doc.lineCount(); i++) {
let scanLine = doc.getLine(i);
if (scanLine.contains(footnote)) {
let cursorLocationIndex = scanLine.indexOf(footnote);
returnLineIndex = i;
doc.setCursor({
line: returnLineIndex,
ch: cursorLocationIndex + footnote.length,
});
return true;
}
}
}
return false;
}
private shouldJumpFromMarkerToDetail(
lineText: string,
cursorPosition: EditorPosition,
doc: Editor
) {
// Jump cursor TO detail marker
// does this line have a footnote marker?
// does the cursor overlap with one of them?
// if so, which one?
// find this footnote marker's detail line
// place cursor there
let markerTarget = null;
let FootnoteMarkerInfo = this.listExistingFootnoteMarkersAndLocations(doc);
let currentLine = cursorPosition.line;
let footnotesOnLine = FootnoteMarkerInfo.filter(markerEntry => markerEntry.lineNum === currentLine);
if (footnotesOnLine != null) {
for (let i = 0; i <= footnotesOnLine.length-1; i++) {
if (footnotesOnLine[i].footnote !== null) {
let marker = footnotesOnLine[i].footnote;
let indexOfMarkerInLine = footnotesOnLine[i].startIndex;
if (
cursorPosition.ch >= indexOfMarkerInLine &&
cursorPosition.ch <= indexOfMarkerInLine + marker.length
) {
markerTarget = marker;
break;
}
}
}
}
if (markerTarget !== null) {
// extract name
let match = markerTarget.match(this.ExtractNameFromFootnote);
if (match) {
let footnoteName = match[2];
// find the first line with this detail marker name in it.
for (let i = 0; i < doc.lineCount(); i++) {
let theLine = doc.getLine(i);
let lineMatch = theLine.match(this.DetailInLine);
if (lineMatch) {
// compare to the index
let nameMatch = lineMatch[1];
if (nameMatch == footnoteName) {
doc.setCursor({ line: i, ch: lineMatch[0].length + 1 });
return true;
}
}
}
}
}
return false;
}
//FUNCTIONS FOR AUTONUMBERED FOOTNOTES
insertAutonumFootnote() {
const mdView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!mdView) return false;
if (mdView.editor == undefined) return false;
const doc = mdView.editor;
const cursorPosition = doc.getCursor();
const lineText = doc.getLine(cursorPosition.line);
const markdownText = mdView.data;
if (this.shouldJumpFromDetailToMarker(lineText, cursorPosition, doc))
return;
if (this.shouldJumpFromMarkerToDetail(lineText, cursorPosition, doc))
return;
return this.shouldCreateAutonumFootnote(
lineText,
cursorPosition,
doc,
markdownText
);
}
private shouldCreateAutonumFootnote(
lineText: string,
cursorPosition: EditorPosition,
doc: Editor,
markdownText: string
) {
// create new footnote with the next numerical index
let matches = markdownText.match(this.AllNumberedMarkers);
let numbers: Array<number> = [];
let currentMax = 1;
if (matches != null) {
for (let i = 0; i <= matches.length - 1; i++) {
let match = matches[i];
match = match.replace("[^", "");
match = match.replace("]", "");
let matchNumber = Number(match);
numbers[i] = matchNumber;
if (matchNumber + 1 > currentMax) {
currentMax = matchNumber + 1;
}
}
}
let footNoteId = currentMax;
let footnoteMarker = `[^${footNoteId}]`;
let linePart1 = lineText.substr(0, cursorPosition.ch);
let linePart2 = lineText.substr(cursorPosition.ch);
let newLine = linePart1 + footnoteMarker + linePart2;
doc.replaceRange(
newLine,
{ line: cursorPosition.line, ch: 0 },
{ line: cursorPosition.line, ch: lineText.length }
);
let lastLineIndex = doc.lastLine();
let lastLine = doc.getLine(lastLineIndex);
while (lastLineIndex > 0) {
lastLine = doc.getLine(lastLineIndex);
if (lastLine.length > 0) {
doc.replaceRange(
"",
{ line: lastLineIndex, ch: 0 },
{ line: doc.lastLine(), ch: 0 }
);
break;
}
lastLineIndex--;
}
let footnoteDetail = `\n[^${footNoteId}]: `;
let list = this.listExistingFootnoteDetails(doc);
if (list===null && currentMax == 1) {
footnoteDetail = "\n" + footnoteDetail;
doc.setLine(doc.lastLine(), lastLine + footnoteDetail);
doc.setCursor(doc.lastLine() - 1, footnoteDetail.length - 1);
} else {
doc.setLine(doc.lastLine(), lastLine + footnoteDetail);
doc.setCursor(doc.lastLine(), footnoteDetail.length - 1);
}
}
//FUNCTIONS FOR NAMED FOOTNOTES
insertNamedFootnote() {
const mdView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!mdView) return false;
if (mdView.editor == undefined) return false;
const doc = mdView.editor;
const cursorPosition = doc.getCursor();
const lineText = doc.getLine(cursorPosition.line);
const markdownText = mdView.data;
if (this.shouldJumpFromDetailToMarker(lineText, cursorPosition, doc))
return;
if (this.shouldJumpFromMarkerToDetail(lineText, cursorPosition, doc))
return;
if (this.shouldCreateMatchingFootnoteDetail(lineText, cursorPosition, doc))
return;
return this.shouldCreateFootnoteMarker(
lineText,
cursorPosition,
doc,
markdownText
);
}
private shouldCreateMatchingFootnoteDetail(
lineText: string,
cursorPosition: EditorPosition,
doc: Editor
) {
// Create matching footnote detail for footnote marker
// does this line have a footnote marker?
// does the cursor overlap with one of them?
// if so, which one?
// does this footnote marker have a detail line?
// if not, create it and place cursor there
let reOnlyMarkersMatches = lineText.match(this.AllMarkers);
let markerTarget = null;
if (reOnlyMarkersMatches){
for (let i = 0; i <= reOnlyMarkersMatches.length; i++) {
let marker = reOnlyMarkersMatches[i];
if (marker != undefined) {
let indexOfMarkerInLine = lineText.indexOf(marker);
if (
cursorPosition.ch >= indexOfMarkerInLine &&
cursorPosition.ch <= indexOfMarkerInLine + marker.length
) {
markerTarget = marker;
break;
}
}
}
}
if (markerTarget != null) {
//extract footnote
let match = markerTarget.match(this.ExtractNameFromFootnote)
//find if this footnote exists by listing existing footnote details
if (match) {
let footnoteId = match[2];
let list: string[] = this.listExistingFootnoteDetails(doc);
// Check if the list is empty OR if the list doesn't include current footnote
// if so, add detail for the current footnote
if(list === null || !list.includes(footnoteId)) {
let lastLineIndex = doc.lastLine();
let lastLine = doc.getLine(lastLineIndex);
while (lastLineIndex > 0) {
lastLine = doc.getLine(lastLineIndex);
if (lastLine.length > 0) {
doc.replaceRange(
"",
{ line: lastLineIndex, ch: 0 },
{ line: doc.lastLine(), ch: 0 }
);
break;
}
lastLineIndex--;
}
let footnoteDetail = `\n[^${footnoteId}]: `;
if (list===null || list.length < 1) {
footnoteDetail = "\n" + footnoteDetail;
doc.setLine(doc.lastLine(), lastLine + footnoteDetail);
doc.setCursor(doc.lastLine() - 1, footnoteDetail.length - 1);
} else {
doc.setLine(doc.lastLine(), lastLine + footnoteDetail);
doc.setCursor(doc.lastLine(), footnoteDetail.length - 1);
}
return true;
}
return;
}
}
}
private shouldCreateFootnoteMarker(
lineText: string,
cursorPosition: EditorPosition,
doc: Editor,
markdownText: string
) {
//create empty footnote marker for name input
let emptyMarker = `[^]`;
doc.replaceRange(emptyMarker,doc.getCursor());
//move cursor in between [^ and ]
doc.setCursor(cursorPosition.line, cursorPosition.ch+2);
//open footnotePicker popup
}
}