-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
44 lines (35 loc) · 1.23 KB
/
main.js
File metadata and controls
44 lines (35 loc) · 1.23 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
const { Plugin } = require("obsidian");
module.exports = class KbdRenderPlugin extends Plugin {
async onload() {
console.log("Loading KBD Hot-swap Plugin (with cursor fix)");
this.registerEvent(
this.app.workspace.on("editor-change", (editor, view) => {
const cursor = editor.getCursor();
const line = editor.getLine(cursor.line);
const kbdRegex = /´([^´]+)´/g;
const updatedLine = line.replace(kbdRegex, "<kbd>$1</kbd>");
if (line !== updatedLine) {
const originalPrefix = line.substring(0, cursor.ch);
const updatedPrefix = originalPrefix.replace(
kbdRegex,
"<kbd>$1</kbd>"
);
const cursorOffset = updatedPrefix.length - originalPrefix.length;
const newCursor = {
line: cursor.line,
ch: cursor.ch + cursorOffset,
};
const lineRange = {
from: { line: cursor.line, ch: 0 },
to: { line: cursor.line, ch: line.length },
};
editor.replaceRange(updatedLine, lineRange.from, lineRange.to);
editor.setCursor(newCursor);
}
})
);
}
onunload() {
console.log("Unloading KBD Hot-swap Plugin");
}
};