generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
121 lines (101 loc) · 2.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
import { App, Editor, EventRef, MarkdownPostProcessorContext, MarkdownView, Modal, Notice, Platform, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
import {
EditorView,
PluginValue,
ViewPlugin,
ViewUpdate,
DecorationSet,
Decoration,
} from "@codemirror/view";
interface PluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: PluginSettings = {
mySetting: 'default'
}
class VerticalLinesPluginValue implements PluginValue {
constructor(
private path: string,
private view: EditorView,
private callback: Function,
) {
// console.log(this.view);
}
update(update: ViewUpdate) {
// console.log("update", update);
this.callback(this.view.dom, this.path);
// if (
// update.docChanged ||
// update.viewportChanged ||
// update.geometryChanged ||
// update.transactions.some((tr) => tr.reconfigured)
// ) {
// this.scheduleRecalculate();
// }
}
}
export default class HtmlLocalImgPlugin extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
console.log("Running on ---------------onload")
this.registerMarkdownPostProcessor((element, ctx) => {
this.processElement(element, ctx.sourcePath);
})
let activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
this.registerEditorExtension(
ViewPlugin.define(
(view) =>
new VerticalLinesPluginValue(
activeFile?.path,
view,
this.processElement.bind(this)
)),
);
}
}
processElement(element: HTMLElement, sourcePath: string) {
let targetLinks = Array.from(element.getElementsByTagName("img"));
// console.log('targetLinks: ', targetLinks)
if (this.app?.metadataCache == null) {
return;
}
for (const link of targetLinks) {
// console.log('link.src: ', link.src);
if (link.src == "" || link.src.includes("https://")) {
continue;
}
let clean_link = link.src.replace('app://obsidian.md/', '')
// console.log('clean_link: ' + clean_link)
let imageFile = this.app.metadataCache.getFirstLinkpathDest(clean_link, sourcePath);
if (imageFile == null) {
// console.log('null clean_link: ' + clean_link)
// console.log('imageFile is null')
continue;
}
// let active_path0 = this.app.vault.adapter.getResourcePath(imageFile.path);
let active_path = this.app.vault.getResourcePath(imageFile)
// console.log('active_path0: ' + active_path0)
// For iOS
clean_link = clean_link.replace('capacitor://localhost/', '')
// console.log('clean_link: ' + clean_link)
let full_link = active_path + '/' + clean_link
// console.log('full_link: ' + full_link)
link.src = full_link
if (Platform.isMobile) {
console.log("Running on mobile platform - setting object fit and height of img")
link.style.objectFit = "contain"
link.height = 100
}
}
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}