generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
125 lines (92 loc) · 4.12 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
import { MarkdownView, Notice, Plugin, request } from "obsidian";
import { waybackSaveUrl, waybackUrl } from "./constants";
import { defaultSettings as DEFAULT_SETTINGS, LinkArchivePluginSettings as LinkArchivePluginSettings, LinkArchiveSettingTab } from "./settings";
const urlRegex =/(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
export default class ObsidianLinkArchivePlugin extends Plugin {
settings: LinkArchivePluginSettings;
async onload() {
console.log("Loading Link Archive plugin...");
await this.loadSettings();
this.addRibbonIcon("restore-file-glyph", "Archive Links", async () => {
// test save
//new Notice('Archive option: ' + this.settings.archiveOption.toString());
const archiveText = `[${this.settings.archiveText}]`;
const dateLinkPart = new Date().toISOString().slice(0, 10).replace(/-/g, "");
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if(view) {
const viewData = view.getViewData();
const reverseArray: Array<[string, number]> = [];
let linkArray: any;
while ((linkArray = urlRegex.exec(viewData)) !== null) {
console.log(`Found ${linkArray[0]}. Next starts at ${urlRegex.lastIndex}.`);
//if(linkArray[0].startsWith(waybackUrl)) continue;
//if(viewData.substring(urlRegex.lastIndex, urlRegex.lastIndex + 14).contains(archiveText)) continue;
// replace clean logic with
// IF next link is the same except with archiveorg in front of it, skip it
reverseArray.unshift([linkArray[0], urlRegex.lastIndex]);
}
console.log(reverseArray);
// ReSharper marks the "some" call as an error, but it's actually correct...
const cleanedList = reverseArray.filter(x =>
!x[0].startsWith(waybackUrl)
&& !reverseArray.some(y => y[0].startsWith(waybackUrl) && y[0].endsWith(x[0])));
console.log(cleanedList);
if (cleanedList.length === 0) {
this.popNotice("No (new) links to archive.");
return;
}
const processingNotice = new Notice(`Archiving ${cleanedList.length} link(s), this might take a while - please be patient...`, 0);
let i = 1;
const totalLinks = cleanedList.length;
for (const tuple of cleanedList) {
const currentLink = tuple[0];
const saveLink = `${waybackSaveUrl}${currentLink}`;
const archiveLink = ` ${archiveText}(${waybackUrl}${dateLinkPart}/${currentLink})`;
const extraOffset = viewData.charAt(tuple[1]) === ")" ? 1 : 0;
const offset = view.editor.offsetToPos(tuple[1] + extraOffset);
const message = `(${i}/${totalLinks}) Successfully archived ${currentLink}!`;
const failMessage = `(${i}/${totalLinks}) Failed to archive ${currentLink}!`;
i += 1;
await this.delay(400);
try {
await request({
url: saveLink
});
view.editor.replaceRange(archiveLink, offset);
console.log(message);
this.popNotice(message);
}
catch (exception) {
this.popNotice(failMessage);
}
}
this.popNotice("Link archiving done!");
processingNotice.hide();
} else {
this.popNotice("Link archiving only works if you have a note open.");
}
});
this.addSettingTab(new LinkArchiveSettingTab(this.app, this));
}
popNotice(message: string, timeInSeconds?: number) {
// ReSharper disable WrongExpressionStatement
if (arguments.length === 1) {
new Notice(message);
} else {
new Notice(message, timeInSeconds * 1000);
}
// ReSharper restore WrongExpressionStatement
}
delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
onunload() {
console.log("Unloading Link Archive plugin...");
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}