Skip to content

Commit

Permalink
Merge pull request #76 from ruimage/main
Browse files Browse the repository at this point in the history
Added an option to shorten the header
  • Loading branch information
zolrath authored Jan 4, 2024
2 parents 0368e49 + 5ae3dc1 commit f133428
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 14 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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);
Expand Down Expand Up @@ -193,6 +193,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();

Expand All @@ -206,7 +207,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);
}
}

Expand All @@ -216,6 +217,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)}...`;
return shortenedTitle;
}

async fetchUrlTitle(url: string): Promise<string> {
try {
const title = await getPageTitle(url);
Expand Down
17 changes: 17 additions & 0 deletions settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface AutoLinkTitleSettings {
shouldReplaceSelection: boolean;
enhanceDefaultPaste: boolean;
websiteBlacklist: string;
maximumTitleLength: number;
}

export const DEFAULT_SETTINGS: AutoLinkTitleSettings = {
Expand All @@ -25,6 +26,7 @@ export const DEFAULT_SETTINGS: AutoLinkTitleSettings = {
shouldReplaceSelection: true,
enhanceDefaultPaste: true,
websiteBlacklist: "",
maximumTitleLength: 0,
};

export class AutoLinkTitleSettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -55,6 +57,21 @@ export class AutoLinkTitleSettingTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Maximum title length")
.setDesc(
"Set the maximum length of the title. 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(
Expand Down

0 comments on commit f133428

Please sign in to comment.