From 7820e7ffd0097f71ca21d61bee6fee67fce63992 Mon Sep 17 00:00:00 2001 From: Enzo Davico Date: Fri, 22 Mar 2024 13:07:56 -0300 Subject: [PATCH] feat: add filename to "format_note_with_url" This changes the way the function adds the link to obsidian. Now the file name is added after "Obsidian". e.g. "Obsidian - Tests". --- src/format.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/format.ts b/src/format.ts index 0e81dfad..a24ca9d8 100644 --- a/src/format.ts +++ b/src/format.ts @@ -58,7 +58,30 @@ export class FormatConverter { } format_note_with_url(note: AnkiConnectNote, url: string, field: string): void { - note.fields[field] += '
Obsidian' + let mdFilename: string = "" + + // We separate the string by "&file=". + const splitUrl: string[] = url.split("&file="); + + // Check if the split resulted in two parts + if (splitUrl.length === 2) { + // Extract the part after "&file=" + const encodedPathWithExtension: string = splitUrl[1]; // Extracted part containing path with extension + const decodedPathWithExtension: string = decodeURIComponent(encodedPathWithExtension); + + // Split the filename from its directory path + const pathSegments: string[] = decodedPathWithExtension.split('/'); + const filenameWithExtension: string | undefined = pathSegments.pop()?.trim(); + + // Check if the filename with extension is not empty + if (filenameWithExtension) { + // Split the filename from its extension + const filenameSegments: string[] = filenameWithExtension.split('.'); + mdFilename = filenameSegments[0]?.trim(); + } + } + + note.fields[field] += '
Obsidian${mdFilename ? " - " + mdFilename : ""}`; } format_note_with_frozen_fields(note: AnkiConnectNote, frozen_fields_dict: Record>): void {