-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.ts
More file actions
88 lines (79 loc) · 2.27 KB
/
Copy pathplugin.ts
File metadata and controls
88 lines (79 loc) · 2.27 KB
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
import { syntaxTree } from "@codemirror/language";
import { RangeSetBuilder } from "@codemirror/state";
import {
Decoration,
DecorationSet,
EditorView,
PluginSpec,
PluginValue,
ViewPlugin,
ViewUpdate,
WidgetType,
} from "@codemirror/view";
import {ThumbnailWidget} from "./widget";
import MyPlugin from "./main";
import {MarkdownView, TFile} from "obsidian";
function buildViewPlugin(plugin: MyPlugin) {
return ViewPlugin.fromClass(
class {
decorations: DecorationSet;
plugin: MyPlugin;
constructor(view: EditorView) {
this.decorations = this.buildDecorations(view);
this.plugin = plugin;
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.decorations = this.buildDecorations(update.view);
}
}
destroy() {}
buildDecorations(view: EditorView): DecorationSet {
const builder = new RangeSetBuilder<Decoration>();
for (let { from, to } of view.visibleRanges) {
syntaxTree(view.state).iterate({
from,
to,
enter(node) {
if (node.type.name.startsWith("hmd-internal-link")) {
// Position of the '-' or the '*'.
const listCharFrom = node.from - 2;
const nodeContent = view.state.doc.sliceString(node.from, node.to)
//console.log('internal link ', nodeContent);
const leafs = plugin.app.workspace.getLeavesOfType('markdown');
//console.log('leafs', leafs);
for (const leaf of leafs) {
if (leaf.view instanceof MarkdownView && leaf.view.file) {
const file: TFile = leaf.view.file;
const linkedNote = plugin.app.metadataCache.getFirstLinkpathDest(nodeContent, file.path);
if (!linkedNote) {
return;
}
const embedImage = plugin.notesWithEmbed.get(linkedNote.path)
if (!embedImage) {
return;
}
builder.add(
listCharFrom,
listCharFrom,
Decoration.widget({
widget: new ThumbnailWidget(plugin, embedImage),
})
);
break;
}
}
}
},
});
}
return builder.finish();
}
}, {
decorations: (value) => value.decorations,
}
);
}
export function BuilderExt(plugin: MyPlugin) {
return [buildViewPlugin(plugin)];
}