From 9baa696095b1a34e63e7d5141129c4eb390b2186 Mon Sep 17 00:00:00 2001 From: Anton Valkouski Date: Sun, 16 Apr 2023 22:41:25 +0300 Subject: [PATCH 1/4] Added an option to shorten the header if someone prefers to trim the header after insertion --- main.ts | 14 +++++++++++++- settings.ts | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index f55a374..4ef470e 100644 --- a/main.ts +++ b/main.ts @@ -192,6 +192,7 @@ export default class AutoLinkTitle extends Plugin { // Fetch title from site, replace Fetching Title with actual title const title = await this.fetchUrlTitle(url); const escapedTitle = this.escapeMarkdown(title); + const shortenedTitle = this.shortTitle(escapedTitle) const text = editor.getValue(); @@ -205,7 +206,7 @@ export default class AutoLinkTitle extends Plugin { const startPos = EditorExtensions.getEditorPositionFromIndex(text, start); const endPos = EditorExtensions.getEditorPositionFromIndex(text, end); - editor.replaceRange(escapedTitle, startPos, endPos); + editor.replaceRange(shortenedTitle, startPos, endPos); } } @@ -215,6 +216,17 @@ export default class AutoLinkTitle extends Plugin { return escaped; } + public shortTitle = (title:string):string =>{ + if (this.settings.maximumTitleLength === 0){ + return title + } + if (title.length < this.settings.maximumTitleLength + 3){ + return title + } + const shortenedTitle = `${title.slice(0,this.settings.maximumTitleLength+1)}...` + return shortenedTitle + } + async fetchUrlTitle(url: string): Promise { try { const title = await getPageTitle(url); diff --git a/settings.ts b/settings.ts index eeb11b4..74d793d 100644 --- a/settings.ts +++ b/settings.ts @@ -10,6 +10,7 @@ export interface AutoLinkTitleSettings { shouldReplaceSelection: boolean; enhanceDefaultPaste: boolean; websiteBlacklist: string; + maximumTitleLength:number; } export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { @@ -25,6 +26,7 @@ export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { shouldReplaceSelection: true, enhanceDefaultPaste: true, websiteBlacklist: "", + maximumTitleLength:0, }; export class AutoLinkTitleSettingTab extends PluginSettingTab { @@ -55,6 +57,21 @@ export class AutoLinkTitleSettingTab extends PluginSettingTab { }) ); + new Setting(containerEl) + .setName("Maximum title length") + .setDesc( + "Set the maximum length of the header. Set to 0 to disable." + ) + .addText((val) => + val + .setValue(this.plugin.settings.maximumTitleLength.toString(10)) + .onChange(async (value) => { + const titleLength = (Number(value)) + this.plugin.settings.maximumTitleLength = isNaN(titleLength) || titleLength < 0 ? 0 : titleLength; + await this.plugin.saveSettings(); + }) + ) + new Setting(containerEl) .setName("Replace Selection") .setDesc( From 61fdbf8e0f0c1b4f2cfb4a08ed60696c3ad11a0f Mon Sep 17 00:00:00 2001 From: Matt Furden Date: Wed, 3 Jan 2024 23:44:44 -0800 Subject: [PATCH 2/4] Fix off by one error in title length When set to 1 the previous logic was shortening to 2 characters, etc. --- main.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.ts b/main.ts index 4ef470e..e166bcb 100644 --- a/main.ts +++ b/main.ts @@ -216,14 +216,14 @@ export default class AutoLinkTitle extends Plugin { return escaped; } - public shortTitle = (title:string):string =>{ - if (this.settings.maximumTitleLength === 0){ + public shortTitle = (title: string): string =>{ + if (this.settings.maximumTitleLength === 0) { return title } - if (title.length < this.settings.maximumTitleLength + 3){ + if (title.length < this.settings.maximumTitleLength + 3) { return title } - const shortenedTitle = `${title.slice(0,this.settings.maximumTitleLength+1)}...` + const shortenedTitle = `${title.slice(0, this.settings.maximumTitleLength)}...` return shortenedTitle } From f33deb1d852d674f75b8b2aba09084b26ac753d0 Mon Sep 17 00:00:00 2001 From: Matt Furden Date: Wed, 3 Jan 2024 23:45:48 -0800 Subject: [PATCH 3/4] Correct code style in settings --- settings.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/settings.ts b/settings.ts index 74d793d..d3f1838 100644 --- a/settings.ts +++ b/settings.ts @@ -10,7 +10,7 @@ export interface AutoLinkTitleSettings { shouldReplaceSelection: boolean; enhanceDefaultPaste: boolean; websiteBlacklist: string; - maximumTitleLength:number; + maximumTitleLength: number; } export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { @@ -26,7 +26,7 @@ export const DEFAULT_SETTINGS: AutoLinkTitleSettings = { shouldReplaceSelection: true, enhanceDefaultPaste: true, websiteBlacklist: "", - maximumTitleLength:0, + maximumTitleLength: 0, }; export class AutoLinkTitleSettingTab extends PluginSettingTab { @@ -60,7 +60,7 @@ export class AutoLinkTitleSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Maximum title length") .setDesc( - "Set the maximum length of the header. Set to 0 to disable." + "Set the maximum length of the title. Set to 0 to disable." ) .addText((val) => val From 5ae3dc1087916ff6967ffbf721268fac71760a4d Mon Sep 17 00:00:00 2001 From: Matt Furden Date: Wed, 3 Jan 2024 23:47:33 -0800 Subject: [PATCH 4/4] Correct code style in main.ts --- main.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.ts b/main.ts index e166bcb..ee39581 100644 --- a/main.ts +++ b/main.ts @@ -21,7 +21,7 @@ export default class AutoLinkTitle extends Plugin { console.log("loading obsidian-auto-link-title"); await this.loadSettings(); - this.blacklist = this.settings.websiteBlacklist.split(",").map(s => s.trim()).filter(s => s.length > 0) + this.blacklist = this.settings.websiteBlacklist.split(",").map(s => s.trim()).filter(s => s.length > 0); // Listen to paste event this.pasteFunction = this.pasteUrlWithTitle.bind(this); @@ -192,7 +192,7 @@ export default class AutoLinkTitle extends Plugin { // Fetch title from site, replace Fetching Title with actual title const title = await this.fetchUrlTitle(url); const escapedTitle = this.escapeMarkdown(title); - const shortenedTitle = this.shortTitle(escapedTitle) + const shortenedTitle = this.shortTitle(escapedTitle); const text = editor.getValue(); @@ -218,13 +218,13 @@ export default class AutoLinkTitle extends Plugin { public shortTitle = (title: string): string =>{ if (this.settings.maximumTitleLength === 0) { - return title + return title; } if (title.length < this.settings.maximumTitleLength + 3) { - return title + return title; } - const shortenedTitle = `${title.slice(0, this.settings.maximumTitleLength)}...` - return shortenedTitle + const shortenedTitle = `${title.slice(0, this.settings.maximumTitleLength)}...`; + return shortenedTitle; } async fetchUrlTitle(url: string): Promise {