Skip to content

Commit

Permalink
add: api.template.runQuickInsertTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 13, 2024
1 parent c6d4d93 commit a3b5ac8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
runTemplate,
runTextTemplate,
runItemTemplate,
runQuickInsertTemplate,
} from "./modules/template/api";
import {
getTemplateKeys,
Expand Down Expand Up @@ -124,6 +125,7 @@ const template = {
runTemplate,
runTextTemplate,
runItemTemplate,
runQuickInsertTemplate,
getTemplateKeys,
getTemplateText,
setTemplate,
Expand Down
27 changes: 26 additions & 1 deletion src/modules/template/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a3b5ac8

Please sign in to comment.