From 9d7f4183cf39d75e81b424a4b645d8ec2bad0706 Mon Sep 17 00:00:00 2001
From: "Eli W. Hunter" <42009212+elihunter173@users.noreply.github.com>
Date: Wed, 29 Jan 2025 03:45:14 -0500
Subject: [PATCH] feat: Allow #heading as display title setting (#434)
* feat: Allow #heading as display name setting
* Add displayTitle usage to create markdown link
* Tweak description text
* Address comments
---
src/components/ModalVault.svelte | 5 +++--
src/components/ResultItemVault.svelte | 8 +++++---
src/repositories/documents-repository.ts | 8 ++++++--
src/settings.ts | 2 +-
4 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte
index c7b7473..ad55ae0 100644
--- a/src/components/ModalVault.svelte
+++ b/src/components/ModalVault.svelte
@@ -240,9 +240,10 @@
// Generate link
let link: string
if (file && active) {
- link = plugin.app.fileManager.generateMarkdownLink(file, active.path)
+ link = plugin.app.fileManager.generateMarkdownLink(file, active.path, "", selectedNote.displayTitle)
} else {
- link = `[[${selectedNote.basename}.${getExtension(selectedNote.path)}]]`
+ const maybeDisplayTitle = selectedNote.displayTitle === "" ? "" : `|${selectedNote.displayTitle}`
+ link = `[[${selectedNote.basename}.${getExtension(selectedNote.path)}${maybeDisplayTitle}]]`
}
// Inject link
diff --git a/src/components/ResultItemVault.svelte b/src/components/ResultItemVault.svelte
index a411825..cc3dd55 100644
--- a/src/components/ResultItemVault.svelte
+++ b/src/components/ResultItemVault.svelte
@@ -172,9 +172,11 @@
{@html plugin.textProcessor.highlightText(title, matchesTitle)}
-
- .{getExtension(note.path)}
-
+ {#if !note.displayTitle}
+
+ .{getExtension(note.path)}
+
+ {/if}
{#if note.matches.length > 0}
diff --git a/src/repositories/documents-repository.ts b/src/repositories/documents-repository.ts
index 7aac7a1..9d0dbf4 100644
--- a/src/repositories/documents-repository.ts
+++ b/src/repositories/documents-repository.ts
@@ -215,8 +215,12 @@ export class DocumentsRepository {
}
}
}
- const displayTitle =
- metadata?.frontmatter?.[this.plugin.settings.displayTitle] ?? ''
+ let displayTitle: string
+ if (this.plugin.settings.displayTitle === '#heading') {
+ displayTitle = metadata?.headings?.find(h => h.level === 1)?.heading ?? ''
+ } else {
+ displayTitle = metadata?.frontmatter?.[this.plugin.settings.displayTitle] ?? ''
+ }
const tags = getTagsFromMetadata(metadata)
return {
basename: file.basename,
diff --git a/src/settings.ts b/src/settings.ts
index 425adbb..d00b5b0 100644
--- a/src/settings.ts
+++ b/src/settings.ts
@@ -242,7 +242,7 @@ export class SettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Set frontmatter property key as title')
.setDesc(
- htmlDescription(`If you have a custom property in your notes that you want to use as the title in search results.
+ htmlDescription(`If you have a custom property in your notes that you want to use as the title in search results. If you set this to '#heading', then use the first heading from a file as the title.
Leave empty to disable.`)
)
.addText(component => {