From a3b5ac80508ff0f75b28aee29f474ff864dfc5be Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:13:24 +0100 Subject: [PATCH] add: api.template.runQuickInsertTemplate --- src/api.ts | 2 ++ src/modules/template/api.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 60b0db1..e337918 100644 --- a/src/api.ts +++ b/src/api.ts @@ -34,6 +34,7 @@ import { runTemplate, runTextTemplate, runItemTemplate, + runQuickInsertTemplate, } from "./modules/template/api"; import { getTemplateKeys, @@ -124,6 +125,7 @@ const template = { runTemplate, runTextTemplate, runItemTemplate, + runQuickInsertTemplate, getTemplateKeys, getTemplateText, setTemplate, diff --git a/src/modules/template/api.ts b/src/modules/template/api.ts index 83d9dc6..64890c5 100644 --- a/src/modules/template/api.ts +++ b/src/modules/template/api.ts @@ -3,7 +3,12 @@ import { itemPicker } from "../../utils/itemPicker"; import { getString } from "../../utils/locale"; import { fill, slice } from "../../utils/str"; -export { runTemplate, runTextTemplate, runItemTemplate }; +export { + runTemplate, + runTextTemplate, + runItemTemplate, + runQuickInsertTemplate, +}; const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; @@ -237,6 +242,26 @@ async function runItemTemplate( return renderedString; } +async function runQuickInsertTemplate( + noteItem: Zotero.Item, + targetNoteItem: Zotero.Item, + options: { + dryRun?: boolean; + } = {}, +) { + if (!noteItem || !targetNoteItem) return ""; + const link = addon.api.convert.note2link(noteItem, {}); + const content = await runTemplate( + "[QuickInsertV2]", + "link, linkText, subNoteItem, noteItem", + [link, noteItem.getNoteTitle().trim() || link, noteItem, targetNoteItem], + { + dryRun: options.dryRun, + }, + ); + return content; +} + async function getItemTemplateData() { // If topItems are pre-defined, use it without asking if (addon.data.template.picker.data.topItemIds?.length > 0) {