diff --git a/docs/about-note-template.md b/docs/about-note-template.md index 3598489..0202379 100644 --- a/docs/about-note-template.md +++ b/docs/about-note-template.md @@ -198,14 +198,15 @@ Basic user template. ### Builtin Templates -| Name | Description | Variables | -| ------------------ | -------------------------------------------------------- | ------------------------------------- | -| QuickInsert | For forward link. | link, linkText, subNoteItem, noteItem | -| QuickBackLink | For back link. | link, linkText, subNoteItem, noteItem | -| QuickImport | For importing note link content. | link, noteItem | -| QuickNote | For generating note from annotation. | annotationItem, topItem, noteItem | -| ExportMDFileName | For generating Markdown file name when exporting. | noteItem | -| ExportMDFileHeader | For generating Markdown file yaml header when exporting. | noteItem | +| Name | Description | Variables | +| ------------------- | -------------------------------------------------------- | ------------------------------------- | +| QuickInsert | For forward link. | link, linkText, subNoteItem, noteItem | +| QuickBackLink | For back link. | link, linkText, subNoteItem, noteItem | +| QuickImport | For importing note link content. | link, noteItem | +| QuickNote | For generating note from annotation. | annotationItem, topItem, noteItem | +| ExportMDFileName | For generating Markdown file name when exporting. | noteItem | +| ExportMDFileHeader | For generating Markdown file yaml header when exporting. | noteItem | +| ExportMDFileContent | For processing Markdown file content when exporting. | noteItem, mdContent | ## Style Syntax diff --git a/src/modules/convert/api.ts b/src/modules/convert/api.ts index 4e516c7..698f200 100644 --- a/src/modules/convert/api.ts +++ b/src/modules/convert/api.ts @@ -81,6 +81,16 @@ async function note2md( ); const remark = await rehype2remark(rehype); let md = remark2md(remark); + try { + md = + (await addon.api.template.runTemplate( + "[ExportMDFileContent]", + "noteItem, mdContent", + [noteItem, md], + )) ?? md; + } catch (e) { + ztoolkit.log(e); + } if (options.withYAMLHeader) { let header = {}; diff --git a/src/modules/template/data.ts b/src/modules/template/data.ts index e74469b..b57e25a 100644 --- a/src/modules/template/data.ts +++ b/src/modules/template/data.ts @@ -8,6 +8,7 @@ const SYSTEM_TEMPLATE_NAMES = [ "[QuickNoteV5]", "[ExportMDFileNameV2]", "[ExportMDFileHeaderV2]", + "[ExportMDFileContent]", ]; // Non-system templates are removed from default templates @@ -15,40 +16,40 @@ const DEFAULT_TEMPLATES = [ { name: "[QuickInsertV2]", text: `

- - \${linkText} - -

`, + + \${linkText} + +

`, }, { name: "[QuickBackLinkV2]", text: `

- Referred in - - \${linkText} - -

`, + Referred in + + \${linkText} + +

`, }, { name: "[QuickImportV2]", text: `
- \${await new Promise(async (r) => { - r(await Zotero.BetterNotes.api.convert.link2html(link, {noteItem, dryRun: _env.dryRun})); - })} -
`, +\${{ + return await Zotero.BetterNotes.api.convert.link2html(link, {noteItem, dryRun: _env.dryRun})); +}}$ +`, }, { name: "[QuickNoteV5]", - text: `\${await new Promise(async (r) => { - let res = ""; - if (annotationItem.annotationComment) { - res += await Zotero.BetterNotes.api.convert.md2html( - annotationItem.annotationComment - ); - } - res += await Zotero.BetterNotes.api.convert.annotations2html([annotationItem], {noteItem, ignoreComment: true}); - r(res); - })}`, + text: `\${{ + let res = ""; + if (annotationItem.annotationComment) { + res += await Zotero.BetterNotes.api.convert.md2html( + annotationItem.annotationComment + ); + } + res += await Zotero.BetterNotes.api.convert.annotations2html([annotationItem], {noteItem, ignoreComment: true}); + return res; +}}$`, }, { name: "[ExportMDFileNameV2]", @@ -56,18 +57,24 @@ const DEFAULT_TEMPLATES = [ }, { name: "[ExportMDFileHeaderV2]", - text: `\${await new Promise(async (r) => { - let header = {}; - header.tags = noteItem.getTags().map((_t) => _t.tag); - header.parent = noteItem.parentItem - ? noteItem.parentItem.getField("title") - : ""; - header.collections = ( - await Zotero.Collections.getCollectionsContainingItems([ - (noteItem.parentItem || noteItem).id, - ]) - ).map((c) => c.name); - r(JSON.stringify(header)); - })}`, + text: `\${{ + let header = {}; + header.tags = noteItem.getTags().map((_t) => _t.tag); + header.parent = noteItem.parentItem + ? noteItem.parentItem.getField("title") + : ""; + header.collections = ( + await Zotero.Collections.getCollectionsContainingItems([ + (noteItem.parentItem || noteItem).id, + ]) + ).map((c) => c.name); + return JSON.stringify(header); +}}$`, + }, + { + name: "[ExportMDFileContent]", + text: `\${{ + return mdContent; +}}$`, }, ]; diff --git a/src/modules/template/preview.ts b/src/modules/template/preview.ts index e94261a..b9631b5 100644 --- a/src/modules/template/preview.ts +++ b/src/modules/template/preview.ts @@ -57,6 +57,18 @@ async function renderTemplatePreview( }); html = `
${YAML.stringify(header, 10)}
`; } + } else if (templateName.includes("ExportMDFileContent")) { + // noteItem + const data = inputItems?.find((item) => item.isNote()); + if (!data) { + html = "

No note item selected

"; + } else { + html = `
${await addon.api.convert.note2md(
+        data,
+        Zotero.getTempDirectory().path,
+        { withYAMLHeader: false, skipSavingImages: true, keepNoteLink: true },
+      )}
`; + } } else if (templateName.includes("QuickInsert")) { // link, linkText, subNoteItem, noteItem const data = inputItems?.find((item) => item.isNote());