add: runTextTemplate

This commit is contained in:
windingwind 2023-06-11 20:02:05 +08:00
parent fd805f7360
commit 23d6616d93
4 changed files with 33 additions and 4 deletions

View File

@ -28,7 +28,11 @@ import {
getMDFileName,
getRelatedNoteIds,
} from "./modules/sync/api";
import { runTemplate, runItemTemplate } from "./modules/template/api";
import {
runTemplate,
runTextTemplate,
runItemTemplate,
} from "./modules/template/api";
import {
getTemplateKeys,
getTemplateText,
@ -90,6 +94,7 @@ const template = {
SYSTEM_TEMPLATE_NAMES,
DEFAULT_TEMPLATES,
runTemplate,
runTextTemplate,
runItemTemplate,
getTemplateKeys,
getTemplateText,

View File

@ -3,7 +3,7 @@ import { getString } from "../../utils/locale";
import { copyEmbeddedImagesInHTML, renderNoteHTML } from "../../utils/note";
import { fill, slice } from "../../utils/str";
export { runTemplate, runItemTemplate };
export { runTemplate, runTextTemplate, runItemTemplate };
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
@ -89,6 +89,26 @@ async function runTemplate(
}
}
async function runTextTemplate(
key: string,
options: {
targetNoteId?: number;
dryRun?: boolean;
}
) {
let { targetNoteId, dryRun } = options;
const targetNoteItem = Zotero.Items.get(targetNoteId || -1);
const sharedObj = {};
return await runTemplate(
key,
"targetNoteItem, sharedObj",
[targetNoteItem, sharedObj],
{
dryRun,
}
);
}
async function runItemTemplate(
key: string,
options: {

View File

@ -85,7 +85,9 @@ async function insertTemplateCallback(name: string) {
targetNoteId: targetNoteItem.id,
});
} else {
html = await addon.api.template.runTemplate(name, "", []);
html = await addon.api.template.runTextTemplate(name, {
targetNoteId: targetNoteItem.id,
});
}
await addLineToNote(
targetNoteItem,

View File

@ -12,7 +12,9 @@ async function renderTemplatePreview(
inputItems = ZoteroPane.getSelectedItems();
}
if (templateName.startsWith("[Text]")) {
html = await addon.api.template.runTemplate(templateName, "", []);
html = await addon.api.template.runTextTemplate(templateName, {
dryRun: true,
});
} else if (templateName.startsWith("[Item]")) {
const data = inputItems?.map((item) => item.id);
html = await addon.api.template.runItemTemplate(templateName, {