generated from Ebonsignori/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
133 lines (112 loc) · 3.97 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
import { Plugin, MarkdownView, Platform, Editor, App, Modal, Setting } from "obsidian";
const mobileOnlyCorsProxy = 'https://api.codetabs.com/v1/proxy?quest='
export default class SwissArmyKnifePlugin extends Plugin {
async onload() {
this.addCommand({
id: "replace-multi-empty-lines-with-single",
name: "Replace multi empty lines with single",
editorCallback: (editor: Editor, view: MarkdownView) => this.replaceDoubledEmptyLinesWithSingle(editor),
});
this.addCommand({
id: "remove-empty-lines",
name: "Remove empty lines",
editorCallback: (editor: Editor, view: MarkdownView) => this.removeEmptyLines(editor),
});
this.addCommand({
id: "fetch-plugin-version",
name: "Fetch plugin version",
callback: () => new FetchPluginModal(this.app,(url) => fetchPluginRelease(url, this.app)).open()
});
}
replaceDoubledEmptyLinesWithSingle(editor: Editor) {
const doubledEmptyLinesWithOptionalWhiteSpacesRegex = /^(\s*?\n){2,}/gm;
return replaceRegexInFile(editor, doubledEmptyLinesWithOptionalWhiteSpacesRegex, '\n');
}
removeEmptyLines(editor: Editor) {
const emptyLinesWithOptionalWhitespacesRegex = /(\s*?\n){2,}/gm;
return replaceRegexInFile(editor, emptyLinesWithOptionalWhitespacesRegex, '\n');
}
}
function replaceRegexInFile(editor: Editor, pattern: RegExp | string, replacement: string) {
const currentText = editor.getValue();
const selectedText = editor.getSelection();
if (selectedText) {
const updatedText = selectedText.replace(pattern, replacement)
editor.replaceSelection(updatedText, selectedText);
} else {
const updatedText = currentText.replace(pattern, replacement)
editor.setValue(updatedText);
}
}
async function fetchPluginRelease(ghRepoUrl:string, app: App ){
try {
const { origin, pathname } = new URL(ghRepoUrl);
const [ , username, pluginName, , , release] = pathname.split('/')
const fetchAddr = [origin, username, pluginName, 'releases', 'download', release].join("/")
const toBeFetched = ['main.js', 'manifest.json', 'styles.css']
const fetchedElements = await Promise.all(toBeFetched.map(async e=> ([e, await (await fetchDataIgnoreCorsIfNeeded(fetchAddr + '/' + e)).text()])));
const existingElements = fetchedElements.filter(([, content]) => !content.includes("Not Found"))
const containsRequiredData = existingElements.length >=2;
if (!containsRequiredData) {
throw new Error("Error fetching main.js and/or manifest.json")
}
const pluginsPath = '.obsidian/plugins/'
const fullPluginPath = pluginsPath + pluginName
app.vault.createFolder(fullPluginPath);
existingElements.map(([filename, content ])=> {
app.vault.create(fullPluginPath + "/"+ filename, content)
})
new InfoModal(this.app, "Successfully installed " + pluginName + " release: " + release +". Please restart Obsidian to make changes visible.").open()
} catch (err) {
new InfoModal(this.app, err.message).open();
}
}
async function fetchDataIgnoreCorsIfNeeded(url: string) {
const {isMobile} = Platform;
return await fetch( isMobile ? mobileOnlyCorsProxy + url : url);
}
export class FetchPluginModal extends Modal {
result: string;
onSubmit: (result: string) => void;
constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.onSubmit = onSubmit;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h1", { text: "Fetch plugin release" });
new Setting(contentEl)
.setName("GH Release url")
.addText((text) =>
text.onChange((value) => {
this.result = value
}));
new Setting(contentEl)
.addButton((btn) =>
btn.setButtonText("Process")
.setCta()
.onClick(() => {
this.close();
this.onSubmit(this.result);
}));
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class InfoModal extends Modal {
message: string;
constructor(app: App , msg: string) {
super(app);
this.message = msg;
}
onOpen() {
const {contentEl} = this;
contentEl.setText(this.message);
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}