diff --git a/.gitignore b/.gitignore index a3c2562..a18ed80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ package-lock.json main.js *.js.map .DS_Store + +data.json \ No newline at end of file diff --git a/main.ts b/main.ts index 94b89b9..711764c 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { EditorExtensions } from "editor-enhancements"; -import { Plugin, MarkdownView, Editor } from "obsidian"; +import { Plugin, MarkdownView, Editor, PluginSettingTab, App, Setting } from "obsidian"; import { AutoLinkTitleSettings, DEFAULT_SETTINGS } from './settings' import { CheckIf } from "checkif"; import getPageTitle from "scraper"; @@ -35,6 +35,8 @@ export default class AutoLinkTitle extends Plugin { }, ], }); + + this.addSettingTab(new AutoLinkTitleSettingTab(this.app, this)); } addTitleToLink(): void { @@ -72,6 +74,12 @@ export default class AutoLinkTitle extends Plugin { return; } + let selectedText = (EditorExtensions.getSelectedText(editor) || "").trim(); + if (selectedText && !this.settings.shouldReplaceSelection) { + // If there is selected text and shouldReplaceSelection is false, do not fetch title + return + } + // We've decided to handle the paste, stop propagation to the default handler. clipboard.stopPropagation(); clipboard.preventDefault(); @@ -159,4 +167,30 @@ export default class AutoLinkTitle extends Plugin { async saveSettings() { await this.saveData(this.settings); } +} + +class AutoLinkTitleSettingTab extends PluginSettingTab { + plugin: AutoLinkTitle; + + constructor(app: App, plugin: AutoLinkTitle) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + let {containerEl} = this; + + containerEl.empty(); + + new Setting(containerEl) + .setName('Replace Selection') + .setDesc('Whether to replace a text selection with link and fetched title') + .addToggle(val => val + .setValue(this.plugin.settings.shouldReplaceSelection) + .onChange(async (value) => { + console.log(value); + this.plugin.settings.shouldReplaceSelection = value; + await this.plugin.saveSettings(); + })); + } } \ No newline at end of file diff --git a/settings.ts b/settings.ts index f0b9e65..4fc2b64 100644 --- a/settings.ts +++ b/settings.ts @@ -4,6 +4,7 @@ export interface AutoLinkTitleSettings { linkRegex: RegExp; linkLineRegex: RegExp; imageRegex: RegExp; + shouldReplaceSelection: boolean; } export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { @@ -11,5 +12,6 @@ export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { lineRegex: /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/ig, linkRegex: /^\[([^\[\]]*)\]\((https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})\)$/i, linkLineRegex: /\[([^\[\]]*)\]\((https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})\)/ig, - imageRegex: /\.(gif|jpe?g|tiff?|png|webp|bmp|tga|psd|ai)/i + imageRegex: /\.(gif|jpe?g|tiff?|png|webp|bmp|tga|psd|ai)/i, + shouldReplaceSelection: true } \ No newline at end of file