Skip to content

Commit 19c74f8

Browse files
committed
fix: Use Obsidian's generateMarkdownLink for consistent link formatting (Issue #312)
Replace hard-coded wikilink generation with Obsidian's generateMarkdownLink() method to respect user's link format preferences and ensure consistency. Changes: - Fix "Insert tasknote link" command to use generateMarkdownLink with task title as alias - Fix "Convert task to TaskNote" command to use generateMarkdownLink - Update generateLinkText helper to use generateMarkdownLink - Both commands now respect Obsidian's "Files & Links" settings - Consistent link format between both commands - Compatible with external tools when markdown links are preferred Fixes #312
1 parent aad6885 commit 19c74f8

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/main.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,20 +1561,26 @@ private injectCustomStyles(): void {
15611561
// Open task selector modal
15621562
const modal = new TaskSelectorModal(this.app, this, unarchivedTasks, (selectedTask) => {
15631563
if (selectedTask) {
1564-
// Create wikilink using Obsidian's API
1564+
// Create link using Obsidian's generateMarkdownLink (respects user's link format settings)
15651565
const file = this.app.vault.getAbstractFileByPath(selectedTask.path);
15661566
if (file) {
1567-
const linkText = this.app.metadataCache.fileToLinktext(file as any, '');
1568-
const wikilink = `[[${linkText}|${selectedTask.title}]]`;
1567+
const currentFile = this.app.workspace.getActiveFile();
1568+
const sourcePath = currentFile?.path || '';
1569+
const properLink = this.app.fileManager.generateMarkdownLink(
1570+
file as TFile,
1571+
sourcePath,
1572+
'',
1573+
selectedTask.title // Use task title as alias
1574+
);
15691575

15701576
// Insert at cursor position
15711577
const cursor = editor.getCursor();
1572-
editor.replaceRange(wikilink, cursor);
1578+
editor.replaceRange(properLink, cursor);
15731579

15741580
// Move cursor to end of inserted text
15751581
const newCursor = {
15761582
line: cursor.line,
1577-
ch: cursor.ch + wikilink.length
1583+
ch: cursor.ch + properLink.length
15781584
};
15791585
editor.setCursor(newCursor);
15801586
} else {

src/services/InstantTaskConvertService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,11 @@ export class InstantTaskConvertService {
604604
const currentFile = this.plugin.app.workspace.getActiveFile();
605605
const sourcePath = currentFile?.path || '';
606606

607-
// Use Obsidian's native link text generation
608-
const obsidianLinkText = this.plugin.app.metadataCache.fileToLinktext(file, sourcePath, true);
607+
// Use Obsidian's generateMarkdownLink (respects user's link format settings)
608+
const properLink = this.plugin.app.fileManager.generateMarkdownLink(file, sourcePath);
609609

610610
// Create the final line with proper indentation and original list format
611-
const linkText = `${originalIndentation}${listPrefix}[[${obsidianLinkText}]]`;
611+
const linkText = `${originalIndentation}${listPrefix}${properLink}`;
612612

613613
// Validate the generated link text
614614
if (linkText.length > 500) { // Reasonable limit for link text
@@ -911,9 +911,9 @@ export class InstantTaskConvertService {
911911

912912
const currentFile = this.plugin.app.workspace.getActiveFile();
913913
const sourcePath = currentFile?.path || '';
914-
const obsidianLinkText = this.plugin.app.metadataCache.fileToLinktext(file, sourcePath, true);
914+
const properLink = this.plugin.app.fileManager.generateMarkdownLink(file, sourcePath);
915915

916-
return `${originalIndentation}${listPrefix}[[${obsidianLinkText}]]`;
916+
return `${originalIndentation}${listPrefix}${properLink}`;
917917
}
918918

919919
/**

0 commit comments

Comments
 (0)