kxxt / obsidian-advanced-paste

Advanced pasting functionality for obsidian

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to use the default pasting behavior or Turndown in a cusotm transform?

YousufSSyed opened this issue · comments

I want to extend the default pasting behavior instead of outright replacing it in my custom transforms. And ideally without importing all of TurndownJS into my script (if that's possible, I haven't quite looked at it).

The default paste contains some dirty hacks and uses obsidian's private APIs. It can't serve as a stable API so I won't expose it to custom transforms.

async defaultPasteCommand(
evt: ClipboardEvent | null,
editor: Editor,
info: MarkdownView | MarkdownFileInfo
) {
const isManuallyTriggered = evt == null; // Not triggered by Ctrl+V
if (
!isManuallyTriggered &&
(evt.clipboardData?.getData("application/x-advpaste-tag") ==
"tag" ||
AUTO_LINK_TITLE_REGEX.test(
evt.clipboardData?.getData("text/plain") ?? ""
))
) {
// 1. Event was triggered by us, don't handle it again
// 2. url, let obsidian-auto-link-title handle it
return;
}
let html;
if (isManuallyTriggered) {
const items = await navigator.clipboard.read();
if (info.file) {
// Try to handle image paste first
const res = await this.handleImagePaste(items[0], info.file);
if (res != null) {
if (res.kind === "ok") {
editor.replaceSelection(res.value);
} else {
new Notice(res.value);
return;
}
}
}
if (items.length == 0 || !items[0].types.includes("text/html"))
return;
const blob = await items[0].getType("text/html");
html = await blob.text();
} else {
// Let obsidian handle image paste, do not handle it ourselves
if (
evt.clipboardData?.types.some(
(x) => x == "Files" || x.startsWith("image/")
)
)
return;
html = evt.clipboardData?.getData("text/html");
}
if (html) {
evt?.preventDefault();
evt?.stopPropagation();
const md = this.utils.turndown.turndown(html);
const processed = await remarkCtor(this.settings).process(md);
const dat = new DataTransfer();
dat.setData("text/html", `<pre>${processed}</pre>`);
dat.setData("application/x-advpaste-tag", "tag");
const e = new ClipboardEvent("paste", {
clipboardData: dat,
});
// console.log(info);
const clipboardMgr = (this.app.workspace.activeEditor as any)
._children[0].clipboardManager;
// console.log(clipboardMgr);
clipboardMgr.handlePaste(e, editor, info);
}
}

But if your goal is to extend the default pasting behavior, you can fork this repo and modify it directly.

Can you still expose it and add a disclaimer that it "contains some dirty hacks and uses obsidian's private APIs" and that it can change at any time without notice?

I'd still like to use it even if that's the case, maybe other users too. I want to extend whatever the default pasting behavior is with this plugin.

Either way, how could I use Turndown without including my own copy of it?

Either way, how could I use Turndown without including my own copy of it?

It's documented in the README: https://github.com/kxxt/obsidian-advanced-paste#advanced-utilities

Can you still expose it and add a disclaimer that it "contains some dirty hacks and uses obsidian's private APIs" and that it can change at any time without notice?
I'd still like to use it even if that's the case, maybe other users too. I want to extend whatever the default pasting behavior is with this plugin.

Can you still expose it and add a disclaimer that it "contains some dirty hacks and uses obsidian's private APIs" and that it can change at any time without notice?

I'd still like to use it even if that's the case, maybe other users too. I want to extend whatever the default pasting behavior is with this plugin.

And because of the above reason, the default pasting is tightly coupled to the Plugin class itself. Exposing it through a sane API will be difficult. It even uses a different clipboard API which takes ClipboardEvent instead of ClipboardItem.

@kxxt How hard would it be to make a function that could be called one from a custom transform which uses the plugin's default pasting behavior? Wouldn't it be a wrapper or whatnot? I haven't looked at the source code but I don't think it would be that hard, unless it is.

@kxxt How hard would it be to make a function that could be called one from a custom transform which uses the plugin's default pasting behavior? Wouldn't it be a wrapper or whatnot? I haven't looked at the source code but I don't think it would be that hard, unless it is.

It's not exposing the function directly is hard. It's exposing it through a sane API is hard. If you really want it you should fork this repo and modify it directly.

@kxxt Alright, thanks for answering though.