Add setting toggle whether to link notes in output

This commit is contained in:
Ben Stuart 2025-02-08 20:30:47 -06:00
parent 410ba866d1
commit b6fb5c96e2
6 changed files with 31 additions and 16 deletions

13
main.ts
View file

@ -16,8 +16,8 @@ export default class OnThisDayPlugin extends Plugin {
this.addCommand({
id: "on-this-day-placeholder",
name: "Add Placeholder at Cursor",
callback: async () => {
await this.addPlaceholder();
callback: () => {
this.addPlaceholder();
},
});
@ -86,7 +86,7 @@ export default class OnThisDayPlugin extends Plugin {
const files = getMarkdownFilesInFolder(folder);
// Build a map: year => concatenated content of matching daily notes
let yearToContent: Record<string, string> = {};
const yearToContent: Record<string, string> = {};
for (const file of files) {
const fileDate = parseDateFromString(
file.basename,
@ -138,7 +138,8 @@ export default class OnThisDayPlugin extends Plugin {
inputDateString,
this.settings.dateFormat,
this.settings.horizontalRules,
this.settings.throughTheYearsHeader
this.settings.throughTheYearsHeader,
this.settings.shouldOutputLinkToNotes
);
// Insert the output block by looking for a placeholder marker defined in settings.
@ -206,10 +207,6 @@ export default class OnThisDayPlugin extends Plugin {
const result = JSON.parse(content);
return result;
} catch (parseError: any) {
console.error(
"Failed to parse JSON from OpenAI response:",
parseError
);
throw new Error(
"Failed to parse JSON from OpenAI response: " +
parseError.message

View file

@ -122,6 +122,18 @@ export default class OnThisDaySettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Link to yearly notes in output")
.setDesc("If enabled, includes [[]] style links to previous years' notes in output block")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.shouldOutputLinkToNotes)
.onChange(async (value: boolean) => {
this.plugin.settings.shouldOutputLinkToNotes = value;
await this.plugin.saveSettings();
})
);
containerEl.createEl("h2", { text: "OpenAI Settings" });
new Setting(containerEl)

View file

@ -29,7 +29,7 @@ function getAllFormats(customFormat: string): string[] {
export function isValidDateString(
dateString: string,
format: string = "MMMM D, YYYY"
format = "MMMM D, YYYY"
): boolean {
// Use strict parsing with the combined list of formats.
return moment(dateString, getAllFormats(format), true).isValid();

View file

@ -1,4 +1,4 @@
import { TFile, TFolder, TAbstractFile } from "obsidian";
import { TFile, TFolder } from "obsidian";
import moment from 'moment';
/**
@ -21,7 +21,8 @@ export function buildOutputBlock(
inputDateString: string,
dateFormat: string,
horizontalRules: string,
throughTheYearsHeader: string
throughTheYearsHeader: string,
shouldOutputLinkToNotes: boolean
): string {
// Start with a heading using the provided header; add a horizontal rule above if specified.
let output = `## ${throughTheYearsHeader}\n`;
@ -32,7 +33,7 @@ export function buildOutputBlock(
// Create a base moment from the input date using the configured date format.
const baseMoment = moment(inputDateString, dateFormat);
// Optionally, sort the years in descending order.
// Sort the years in descending order.
const years = Object.keys(summaries).sort(
(a, b) => parseInt(b) - parseInt(a)
);
@ -41,8 +42,12 @@ export function buildOutputBlock(
for (const year of years) {
const noteMoment = baseMoment.clone().set("year", parseInt(year));
const formattedNoteName = noteMoment.format(dateFormat);
// Create a Markdown link in the form [[formattedNoteName|year]]
output += `- **[[${formattedNoteName}|${year}]]:** ${summaries[year]}\n`;
// Create a Markdown link in the form [[formattedNoteName|year]] if setting is on
if (shouldOutputLinkToNotes) {
output += `- **[[${formattedNoteName}|${year}]]:** ${summaries[year]}\n`;
} else {
output += `- **${year}:** ${summaries[year]}\n`;
}
}
if (horizontalRules.includes("Below")) {

View file

@ -12,7 +12,6 @@ export function constructPrompt(
customPrompt: string
): string {
const promptInstructions = userPrompt + customPrompt;
console.log(promptInstructions);
const dataString = JSON.stringify(yearToContent);
return `${promptInstructions}\n\nData:\n${dataString}`;
}

View file

@ -10,6 +10,7 @@ export interface OnThisDayPluginSettings {
customPrompt: string;
placeholder: string;
throughTheYearsHeader: string;
shouldOutputLinkToNotes: boolean;
}
export const DEFAULT_SETTINGS: OnThisDayPluginSettings = {
@ -20,5 +21,6 @@ export const DEFAULT_SETTINGS: OnThisDayPluginSettings = {
horizontalRules: "",
customPrompt: "",
placeholder: "<!OTDI>",
throughTheYearsHeader: "On This Day"
throughTheYearsHeader: "On This Day",
shouldOutputLinkToNotes: true
};