-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
36 lines (30 loc) · 1.18 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
import { Plugin } from "obsidian";
export default class PasteTeXReplacer extends Plugin {
onload() {
console.log("Loading Paste TeX Replacer Plugin");
// Listen for the paste event globally
this.registerEvent(
this.app.workspace.on("editor-paste", (evt: ClipboardEvent, editor) => {
// Get the pasted text
let clipboardData = evt.clipboardData;
if (!clipboardData) {
return;
}
let pastedText = clipboardData.getData("text");
// Replace \[ with $$, \] with $$, \( and \) with $, and remove extra spaces
pastedText = pastedText
.replace(/\\\(\s*/g, "$") // Replace \( and remove spaces after it
.replace(/\s*\\\)/g, "$") // Replace \) and remove spaces before it
.replace(/\\\[\s*/g, "$$") // Replace \[ and remove spaces after it
.replace(/\s*\\\]/g, "$$") // Replace \] and remove spaces before it
// Prevent the default paste action
evt.preventDefault();
// Insert the modified text at the cursor position
editor.replaceSelection(pastedText);
})
);
}
onunload() {
console.log("Unloading Paste TeX Replacer Plugin");
}
}