forked from dhamaniasad/obsidian-rich-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
99 lines (82 loc) · 2.57 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
import {
Notice,
Plugin,
Editor,
MarkdownView
} from "obsidian";
interface ObsidianRichLinksPluginSettings {
}
const DEFAULT_SETTINGS: ObsidianRichLinksPluginSettings = {
};
export default class ObsidianRichLinksPlugin extends Plugin {
settings: ObsidianRichLinksPlugin;
async onload() {
console.log("loading plugin");
await this.loadSettings();
this.addRibbonIcon("link", "Rich Links", () => {
let activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
let editor = activeView.editor;
this.urlToIframe(editor);
}
});
this.addCommand({
id: "create-rich-links",
name: "Create Rich Link",
editorCheckCallback: (checking: boolean, editor: Editor) => {
if (!checking) {
this.urlToIframe(editor);
}
return true;
},
});
}
onunload() {
console.log("unloading plugin");
}
isUrl(text: string): boolean {
const urlRegex = new RegExp(
"^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$"
);
return urlRegex.test(text);
}
urlToIframe(editor: Editor): void {
let selectedText = editor.somethingSelected()
? editor.getSelection().trim()
: false;
if (selectedText && this.isUrl(selectedText)) {
const url = selectedText;
ajaxPromise({
url: `http://iframely.server.crestify.com/iframely?url=${url}`,
}).then((res) => {
const data = JSON.parse(res);
const imageLink = data.links.find((value: { type: string; }) => value.type.startsWith("image/")).href || '';
editor.replaceSelection(`
<div class="rich-link-card-container"><a class="rich-link-card" href="${url}" target="_blank">
<div class="rich-link-image-container">
<div class="rich-link-image" style="background-image: url('${imageLink}')">
</div>
</div>
<div class="rich-link-card-text">
<h1 class="rich-link-card-title">${(data.meta.title || "").replace(/\s{3,}/g, ' ').trim()}</h1>
<p class="rich-link-card-description">
${(data.meta.description || "").replace(/\s{3,}/g, ' ').trim()}
</p>
<p class="rich-link-href">
${url}
</p>
</div>
</a></div>
`);
});
} else {
new Notice("Select a URL to convert to rich link.");
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}