mirror of
https://github.com/ksawl/obsidian-alchemist.git
synced 2026-07-22 06:49:47 +00:00
68 lines
2.7 KiB
TypeScript
68 lines
2.7 KiB
TypeScript
import { Editor, htmlToMarkdown, Notice } from 'obsidian';
|
|
import { IAlchemistModule, AlchemistContext } from '../../core/IAlchemistModule';
|
|
import { AlchemistSettings } from '../../../settings';
|
|
import { containsTrackers, stripTrackers, purifyHtmlStructure } from './logic';
|
|
|
|
export class SmartPasteModule implements IAlchemistModule {
|
|
public id = 'smart-paste';
|
|
private context!: AlchemistContext;
|
|
|
|
async load(context: AlchemistContext): Promise<void> {
|
|
this.context = context;
|
|
this.registerPasteHandler();
|
|
this.registerCommands();
|
|
}
|
|
|
|
async unload(): Promise<void> {
|
|
// Commands and events are automatically cleaned up if registered via plugin
|
|
}
|
|
|
|
async onSettingsChange(newSettings: AlchemistSettings): Promise<void> {
|
|
this.context.settings = newSettings;
|
|
}
|
|
|
|
private registerCommands() {
|
|
this.context.plugin.addCommand({
|
|
id: 'clean-trackers-in-document',
|
|
name: 'Clean trackers in current document',
|
|
editorCallback: (editor: Editor) => {
|
|
const content = editor.getValue();
|
|
const cleaned = stripTrackers(content);
|
|
if (content !== cleaned) {
|
|
editor.setValue(cleaned);
|
|
new Notice('Alchemist: Document cleaned from trackers');
|
|
} else {
|
|
new Notice('Alchemist: No trackers found');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private registerPasteHandler() {
|
|
this.context.plugin.registerEvent(
|
|
this.context.app.workspace.on('editor-paste', async (evt: ClipboardEvent, editor: Editor) => {
|
|
if (!this.context.settings.enableSmartPaste) return;
|
|
|
|
const clipboardData = evt.clipboardData;
|
|
if (!clipboardData) return;
|
|
|
|
const text = clipboardData.getData('text/plain')?.trim() || '';
|
|
|
|
// SURGICAL INTERCEPTION: Only intercept pure URLs
|
|
const urlRegex = /^(https?:\/\/[^\s"'>]+)$/;
|
|
if (text.match(urlRegex) && this.context.settings.stripTrackingParameters) {
|
|
const cleanUrl = stripTrackers(text);
|
|
if (cleanUrl !== text) {
|
|
evt.preventDefault();
|
|
editor.replaceSelection(cleanUrl);
|
|
new Notice('Alchemist: URL cleaned');
|
|
}
|
|
}
|
|
|
|
// Note: We no longer intercept rich HTML pastes to avoid breaking
|
|
// Obsidian's native formatting (like code blocks or auto-linking).
|
|
// Users can use the "Clean trackers in current document" command instead.
|
|
})
|
|
);
|
|
}
|
|
}
|