Hi, I’m new to programming (self-taught) and this is my first time making an Obsidian plugin
I’m working on a small plugin that will help me format/clean up text (e.g. you’ve copied a paragraph from a pdf and the line breaks are all messed up)
so far I’ve copied the sample plugin (works fine), edited the main.ts, and recompiled it, made a manifest.json, and successfully loaded it into Obsidian and made sure it’s active. The problem is that it doesn’t work.
error message is as follows:
Uncaught TypeError: import_obsidian.Editor.getselection is not a function
at Object.editorCallback (main.ts:28:49)
at e.mobileOnly.zt.isMobile.e.checkCallback (app.js:1:1988281)
at oK (app.js:1:1987579)
at t.onChooseItem (app.js:1:2590183)
at t.onChooseSuggestion (app.js:1:1564140)
at t.selectSuggestion (app.js:1:1563690)
at e.useSelectedItem (app.js:1:1561971)
at Object.func (app.js:1:1559370)
at e.handleKey (app.js:1:757237)
at e.onKeyEvent (app.js:1:758493)
editorCallback @ main.ts:28
e.mobileOnly.zt.isMobile.e.checkCallback @ app.js:1
oK @ app.js:1
t.onChooseItem @ app.js:1
t.onChooseSuggestion @ app.js:1
t.selectSuggestion @ app.js:1
e.useSelectedItem @ app.js:1
(anonymous) @ app.js:1
e.handleKey @ app.js:1
e.onKeyEvent @ app.js:1
and the code itself is as follows (with console.log() to try and see if it’s working or not. if you know about better debugging methods in obsidian i’m happy to hear them)
import { Editor, MarkdownEditView, Plugin } from "obsidian";
export default class CleanerPlugin extends Plugin {
async onload() {
// paragraph cleaner
function paragraph_cleaner(s) { return s.replace(/(?<!\n)\n(?!\n)/gm, " "); }
// subtitle paragraph cleaner. doesn't replace newlines that start with a hyphen since that's probably a different character talking
function subtitle_paragraph_cleaner(s) { return s.replace(/\n(?!-)/gm, " "); }
// srt subtitles cleaner
// remove SRT data, and group paragraphs using subtitle paragraph cleaner
function srt_cleaner(s) { return subtitle_paragraph_cleaner(s.replace(/\d+\n\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n/gm, "")); }
this.addCommand({
id: "srt-cleaner",
name: "clean SRT subtitles",
editorCallback: (editor: Editor, view: MarkdownView) => {
const selection = editor.getSelection();
console.log("selection: " + selection);
console.log("replacement: " + srt_cleaner(selection));
Editor.replaceSelection(srt_cleaner(selection));
}}),
this.addCommand({
id: "p-cleaner",
name: "clean paragraph (e.g. copied from a pdf)",
editorCallback: (editor: Editor, view: MarkdownView) => {
const selection = editor.getSelection();
console.log("selection: " + selection);
console.log("replacement: " + paragraph_cleaner(selection));
Editor.replaceSelection(paragraph_cleaner(selection));
}
})
}
}
I did try both of:
Editor.getSelection()
editor.getSelection
as well but neither worked