From 3e7902c26d9775ffedce4daf7c0dcaa231f2e5d9 Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Tue, 4 Feb 2025 10:54:37 -0800 Subject: [PATCH] better handling for missing references --- main.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/main.ts b/main.ts index f881ed1..d6fa3b0 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { App, Editor, MarkdownView, Plugin, PluginSettingTab, TFile } from 'obsidian'; -import { parseQuote, replaceDoubleQuotes, scoreRefMatches } from 'src/quotes'; +import { CslReference, parseQuote, replaceDoubleQuotes, scoreRefMatches } from 'src/quotes'; // Remember to rename these classes and interfaces! @@ -48,7 +48,14 @@ export default class PasteQuotePlugin extends Plugin { return ""; } - const id = this.citeIdForQuote(quote, file) || 'TODO'; + const fileCache = this.app.metadataCache.getFileCache(file); + const refs = fileCache?.frontmatter?.references || []; + if (refs.length == 0) { + // TODO make this configurable + return ` (*${quote.title}*, p. ${quote.page})`; + } + + const id = this.citeIdForQuote(quote, refs) || `TODO ${quote.title}`; let citation = `[@${id}`; if (quote.page) { citation += ', p. ' + quote.page; @@ -57,13 +64,7 @@ export default class PasteQuotePlugin extends Plugin { return citation; } - citeIdForQuote(quote: Quote, file?: TFile): string | null { - if (!file) { - return null; - } - - const fileCache = this.app.metadataCache.getFileCache(file); - const refs = fileCache?.frontmatter?.references || []; + citeIdForQuote(quote: Quote, refs: CslReference[]): string | null { const results = scoreRefMatches(quote, refs); if (results.length == 0) { return null;