From dc22d4c6457043f2784112c502a9b7f3468f3fb2 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:05:33 +0800 Subject: [PATCH] add: api.convert.note2html --- src/api.ts | 2 ++ src/modules/convert/api.ts | 19 +++++++++++++++++++ src/modules/template/api.ts | 17 +++++------------ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/api.ts b/src/api.ts index 7800710..bbdfbf3 100644 --- a/src/api.ts +++ b/src/api.ts @@ -8,6 +8,7 @@ import { md2html, html2md, annotations2html, + note2html, } from "./modules/convert/api"; import { exportNotes } from "./modules/export/api"; import { saveDocx } from "./modules/export/docx"; @@ -90,6 +91,7 @@ const convert = { md2html, html2md, annotations2html, + note2html, }; const template = { diff --git a/src/modules/convert/api.ts b/src/modules/convert/api.ts index 3b579f3..3fcfb5c 100644 --- a/src/modules/convert/api.ts +++ b/src/modules/convert/api.ts @@ -45,6 +45,7 @@ export { md2html, html2md, annotations2html, + note2html, }; async function note2md( @@ -217,6 +218,24 @@ function annotations2html( return parseAnnotationHTML(annotations, options); } +async function note2html( + noteItems: Zotero.Item | Zotero.Item[], + options: { targetNoteItem?: Zotero.Item; html?: string }, +) { + if (!Array.isArray(noteItems)) { + noteItems = [noteItems]; + } + const { targetNoteItem } = options; + let html = options.html; + if (!html) { + html = noteItems.map((item) => item.getNote()).join("\n"); + } + if (targetNoteItem?.isNote()) { + return await copyEmbeddedImagesInHTML(html, targetNoteItem, noteItems); + } + return await renderNoteHTML(html, noteItems); +} + function note2rehype(str: string) { const rehype = unified() .use(remarkGfm) diff --git a/src/modules/template/api.ts b/src/modules/template/api.ts index c5d93a0..8c04071 100644 --- a/src/modules/template/api.ts +++ b/src/modules/template/api.ts @@ -1,6 +1,5 @@ import { itemPicker } from "../../utils/itemPicker"; import { getString } from "../../utils/locale"; -import { copyEmbeddedImagesInHTML, renderNoteHTML } from "../../utils/note"; import { fill, slice } from "../../utils/str"; export { runTemplate, runTextTemplate, runItemTemplate }; @@ -203,17 +202,11 @@ async function runItemTemplate( ), ); - let html = results.join("\n"); - if (targetNoteItem && targetNoteItem.isNote()) { - html = await copyEmbeddedImagesInHTML( - html, - targetNoteItem, - copyImageRefNotes, - ); - } else { - html = await renderNoteHTML(html, copyImageRefNotes); - } - return html; + const html = results.join("\n"); + return await addon.api.convert.note2html(copyImageRefNotes, { + targetNoteItem, + html, + }); } async function getItemTemplateData() {