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

Added an option to shorten the header #76

Merged
merged 4 commits into from
Jan 4, 2024
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
16 changes: 14 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}

Expand All @@ -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)}...`;
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