Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to not replace selection #17

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ package-lock.json
main.js
*.js.map
.DS_Store

data.json
36 changes: 35 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -35,6 +35,8 @@ export default class AutoLinkTitle extends Plugin {
},
],
});

this.addSettingTab(new AutoLinkTitleSettingTab(this.app, this));
}

addTitleToLink(): void {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}));
}
}
4 changes: 3 additions & 1 deletion settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export interface AutoLinkTitleSettings {
linkRegex: RegExp;
linkLineRegex: RegExp;
imageRegex: RegExp;
shouldReplaceSelection: boolean;
}

export const DEFAULT_SETTINGS: AutoLinkTitleSettings = {
regex: /^(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,
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
}