-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
202 lines (169 loc) · 5.62 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { App, Editor, MarkdownPostProcessorContext, MarkdownView, parseFrontMatterTags, Plugin, getAllTags, View, TFile, WorkspaceLeaf, TextFileView } from 'obsidian';
export default class TagStylesPlugin extends Plugin {
appliedClasses = new WeakMap<MarkdownView, string[]>();
async onload() {
// Processor that adds CSS class to a DIV with a tag in it
this.registerMarkdownPostProcessor(this.applyClass);
// Processor that replaces <img> with <svg> when applicable
this.registerMarkdownPostProcessor((node, ctx) => {
const nodeEl = node as HTMLElement;
nodeEl.findAll("p").forEach((pEl) => {
pEl.findAll("span").forEach(async (spanEl) => {
if (spanEl.hasAttribute("src") && spanEl?.getAttr("src")?.contains(".svg")) {
const file = this.app.vault.getFiles().find((element) => element.name == spanEl.getAttr("src"));
if (!file)
return;
const content = await this.app.vault.cachedRead(file);
const el = createEl("div");
el.innerHTML = content;
el.addClass("svg-div-embed");
if (spanEl.hasAttribute("alt")) {
pEl.parentElement?.setAttr("alt", spanEl.getAttr("alt"));
}
pEl.parentElement?.addClass("svg-container");
pEl.appendChild(el);
pEl.addClass("svg-p-embed");
pEl.removeChild(spanEl);
}
});
});
});
// Handler that adds CSS class to an entire page based on its tags
this.registerEvent(this.app.workspace.on('layout-change', this.handleLayoutChange.bind(this)));
this.registerMarkdownPostProcessor((element, context) => {
});
}
onunload() { }
/**
* Applies a CSS class to all tag elements in a markdown view.
*/
applyClass(node: Node, ctx: MarkdownPostProcessorContext) {
const nodeEl = node as HTMLElement;
// Apply a CSS class to all DIVs containing a tag
nodeEl.findAll("a.tag").forEach((tagEl) => {
const tag = (tagEl as HTMLAnchorElement).innerText
.slice(1)
.replace("/", "-");
nodeEl.addClass(`tag-${tag}`);
});
}
/**
* On opening a file, apply a CSS class to it based on its tags and top-level folder.
* Removes all the added classes when the file is closed.
*/
private handleLayoutChange() {
// Stop if there are no active views
const activeViews = this.getAllActiveViews();
if (!activeViews) {
return;
}
// Iterate each open markdown view
activeViews.forEach((view) => {
this.removePreviousClasses(view);
let container: Element | null;
// Make list of CSS classes to add
var classes: string[] = [];
// Add class for each tag
if (!view.file)
return;
const fileCache = this.app.metadataCache.getFileCache(view.file);
if (!fileCache)
return;
const fileTags = getAllTags(fileCache);
if (!fileTags)
return;
fileTags.forEach(function (element) {
classes.push("tag-"+element.replace("#",""))
});
// Add class for this file's current folder, if any
const filePath = view.file.path;
if (filePath.contains("/")) {
classes.push("folder-"+filePath.replace(" ", "").split("/")[0]);
}
if (this.isReadMode(view)) {
container = this.getPreviewContainer(view);
}
else if (this.isEditMode(view)) {
container = this.getEditContainer(view);
} else {
container = null;
}
if (!container)
return;
this.applyClasses(classes, view, container);
})
}
/**
* Get the active markdown view and any linked panes.
*/
private getAllActiveViews(): MarkdownView[] | null {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
// Get any linked views
let activeViews: MarkdownView[] = [activeView];
const leafGroup = this.app.workspace.getGroupLeaves((activeView.leaf as any).group);
if (leafGroup && leafGroup.length > 0) {
activeViews = leafGroup
.map((leaf) => leaf.view)
.filter((view) => view instanceof MarkdownView) as MarkdownView[];
}
return activeViews;
}
return null;
}
/**
* Given a view, remove any extra classes this plugin
* has applied to that view.
*/
private removePreviousClasses(view: MarkdownView): void {
const previewContainer = this.getPreviewContainer(view);
const editContainer = this.getEditContainer(view);
const classes = this.appliedClasses.get(view);
if (classes && previewContainer) {
previewContainer.removeClasses(classes);
}
if (classes && editContainer) {
editContainer.removeClasses(classes);
}
this.appliedClasses.delete(view);
}
/**
* Get the element that preview classes are applied to
*/
private getPreviewContainer(view: MarkdownView) {
return view.contentEl.querySelector('.markdown-preview-view');
}
/**
* Get the element that edit/source classes are applied to
*/
private getEditContainer(view: MarkdownView) {
return view.contentEl.querySelector('.markdown-source-view');
}
/**
* Get the inline title element
*/
private getTitleElement(view: MarkdownView) {
return view.contentEl.querySelector('.inline-title');
}
/**
* Returns true if a view is in preview mode
*/
private isReadMode(view: MarkdownView): boolean {
return view.getMode() === 'preview';
}
/**
* Returns true if a view is in edit/source mode
*/
private isEditMode(view: MarkdownView): boolean {
return view.getMode() === 'source';
}
/**
* Add classes to an html element and store the
* applied classes along with a reference the view
* they were added to for removal later.
*/
private applyClasses(classes: string[], view: MarkdownView, container: Element): void {
container.addClasses(classes);
this.appliedClasses.set(view, classes);
}
}