generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
169 lines (154 loc) · 4.33 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
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
Vault,
requestUrl,
} from "obsidian";
import PatternRecognizer from "autoliter/patternRecognizer";
import { getReplaceDict } from "autoliter/utils";
import { AutoLiterSettingTab } from "autoliter/settings";
import { downloadPdf } from "autoliter/pdfManager";
interface AutoLiterSettings {
outputFormat: string;
regExp: string;
autoDownloadPDF: boolean;
pdfDownloadPathBase: string;
pdfDownloadPath: string;
pdfNameFormat: "title" | "id" | "custom";
customPdfNameFormat: string;
}
const DEFAULT_SETTINGS: AutoLiterSettings = {
// example: "------\n**${title}** ([link](${url}))\n\t- *${author} et.al.*\n\t- ${journal}\n\t- ${pubDate}\n\n------\n"
outputFormat:
"- **${title}** ([link](${url}))\n\t- *${author} et.al.*\n\t- ${journal}\n\t- ${pubDate}",
regExp: "- {.{3,}}",
autoDownloadPDF: false,
pdfDownloadPathBase: "vault",
pdfDownloadPath: "pdfs",
pdfNameFormat: "title",
customPdfNameFormat: "${title}",
};
export default class AutoLiter extends Plugin {
settings: AutoLiterSettings;
paperRecognizer: PatternRecognizer;
async onload() {
await this.loadSettings();
this.addSettingTab(new AutoLiterSettingTab(this.app, this));
this.paperRecognizer = new PatternRecognizer(`${this.settings.regExp}`); // g is must
// Icon in the left ribbon: update active file
const ribbonIconEl = this.addRibbonIcon(
"book-down",
"AutoLiter",
async (evt: MouseEvent) => {
new Notice("start updating vault.");
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
await this.updateFile(activeFile, this.paperRecognizer);
}
new Notice("finish updating vault.");
}
);
// Command: update the whole vault
this.addCommand({
id: "update-vault",
name: "Update the whole vault",
editorCallback: async (editor: Editor) => {
new Notice("start updating vault.");
await Promise.all(
this.app.vault
.getMarkdownFiles()
.map((file) =>
this.updateFile(file, this.paperRecognizer)
)
);
new Notice("finish updating vault.");
},
});
// Command: update selected text
this.addCommand({
id: "update-selected",
name: "Update selected text",
editorCallback: async (editor: Editor) => {
new Notice("start updating selection.");
const selection = editor.getSelection();
editor.replaceSelection(
await this.updateSelected(selection, this.paperRecognizer)
);
new Notice("finish updating selection.");
},
});
}
onunload() {}
async updateFile(file: TFile, paperRecognizer: PatternRecognizer) {
const content = await this.app.vault.read(file);
const m = paperRecognizer.findAll(content);
console.log(this.app.workspace.getActiveFile());
if (m && m.length != 0) {
const progressNotice = new Notice(`Updating ${file.path}...`);
const [replaceDict, pdfUrls, paperInfo] = await getReplaceDict(
m,
this.settings.outputFormat
);
console.log(replaceDict);
this.app.vault.process(file, (data) => {
Object.keys(replaceDict).forEach((key, idx) => {
data = data.replace(key, replaceDict[key] as string);
try {
this.settings.autoDownloadPDF &&
downloadPdf(
this.app,
pdfUrls[idx],
paperInfo[idx],
this.settings.pdfNameFormat,
this.settings.customPdfNameFormat,
this.settings.pdfDownloadPathBase,
this.settings.pdfDownloadPath
);
} catch (error) {}
});
return data;
});
}
}
async updateSelected(
selection: string,
paperRecognizer: PatternRecognizer
) {
const m = paperRecognizer.findAll(selection);
if (m && m.length != 0) {
const progressNotice = new Notice(`Updating selection...`);
const [replaceDict, pdfUrls, paperInfo] = await getReplaceDict(
m,
this.settings.outputFormat
);
Object.keys(replaceDict).forEach((key) => {
selection = selection.replace(key, replaceDict[key] as string);
});
}
return selection;
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
joinPaths(basePath: string, relativePath: string) {
if (relativePath.startsWith("./")) {
return basePath + relativePath.slice(1);
} else {
return basePath + "/" + relativePath;
}
}
}