generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
125 lines (114 loc) · 2.89 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 {
addIcon,
App,
ButtonComponent,
MarkdownView,
Modal,
Notice,
Plugin,
requestUrl,
TextComponent,
} from "obsidian";
// @ts-ignore
import smortLogo from "./SmortLogo.svg";
// @ts-ignore
import { version as uuidVersion } from "uuid";
// @ts-ignore
import { validate as uuidValidate } from "uuid";
function uuidValidateV4(uuid: string) {
return uuidValidate(uuid) && uuidVersion(uuid) === 4;
}
export default class Smort extends Plugin {
async onload() {
this.addCommand({
id: "smort-modal-command",
name: "Get Smort.io article as Markdown",
callback: () => {
new URLModal(this.app, this).open();
},
});
}
async processURL(url: string) {
const activeView = this.getActiveView();
if (!activeView) {
console.error("[Smort] No active view to insert into.");
return;
}
let urlObj;
try {
urlObj = new URL(url);
} catch (e) {
new Notice("[Smort] Error: Invalid URL provided", 5000);
return;
}
const articleId = urlObj.pathname.slice(1);
if (!uuidValidateV4(articleId)) {
new Notice("[Smort] Error: Invalid article ID", 5000);
return;
}
const apiURL = `https://www.smort.io/api/article?uuid=${articleId}&markup=markdown`;
requestUrl({
url: apiURL,
method: "GET",
headers: { Accept: "application/json" },
})
.then((response) => {
if (response.status !== 200) {
new Notice(`[Smort] Error: ${response.status}`, 5000);
return;
}
this.addMarkdown(response.text);
})
.catch((error) => new Notice("[Smort] Error: " + error, 5000));
}
async addMarkdown(md: string) {
const activeView = this.getActiveView();
if (!activeView) {
console.error("[Smort] No active view to insert into.");
return;
}
activeView.editor.replaceSelection(md);
}
getActiveView(): MarkdownView {
return this.app.workspace.getActiveViewOfType(MarkdownView);
}
}
class URLModal extends Modal {
plugin: Smort;
constructor(app: App, plugin: Smort) {
super(app);
this.plugin = plugin;
this.modalEl.id = "smort-modal";
}
onOpen() {
const { contentEl } = this;
addIcon("smort-logo", smortLogo);
contentEl.createEl("h1", {
text: "Smort.io",
cls: "smort-title",
});
const urlField = new TextComponent(contentEl).setPlaceholder(
"https://smort.io/[...]"
);
urlField.inputEl.id = "smort-input";
const getSmort = () => {
const url = urlField.getValue();
this.plugin.processURL(url);
this.close();
};
const smortButton = new ButtonComponent(contentEl)
.setButtonText("Get Markdown")
.onClick(getSmort);
smortButton.buttonEl.id = "smort-button";
urlField.inputEl.focus();
urlField.inputEl.addEventListener("keypress", function (keypressed) {
if (keypressed.key === "Enter") {
getSmort();
}
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}