add: ExportMDFileContent

This commit is contained in:
windingwind 2023-08-12 00:21:39 +08:00
parent 6c05c3fd00
commit d0b6ef9107
4 changed files with 74 additions and 44 deletions

View File

@ -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

View File

@ -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 = {};

View File

@ -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 = <NoteTemplate[]>[
{
name: "[QuickInsertV2]",
text: `<p>
<a href="\${link}">
\${linkText}
</a>
</p>`,
<a href="\${link}">
\${linkText}
</a>
</p>`,
},
{
name: "[QuickBackLinkV2]",
text: `<p>
Referred in
<a href="\${link}">
\${linkText}
</a>
</p>`,
Referred in
<a href="\${link}">
\${linkText}
</a>
</p>`,
},
{
name: "[QuickImportV2]",
text: `<blockquote>
\${await new Promise(async (r) => {
r(await Zotero.BetterNotes.api.convert.link2html(link, {noteItem, dryRun: _env.dryRun}));
})}
</blockquote>`,
\${{
return await Zotero.BetterNotes.api.convert.link2html(link, {noteItem, dryRun: _env.dryRun}));
}}$
</blockquote>`,
},
{
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 = <NoteTemplate[]>[
},
{
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;
}}$`,
},
];

View File

@ -57,6 +57,18 @@ async function renderTemplatePreview(
});
html = `<pre>${YAML.stringify(header, 10)}</pre>`;
}
} else if (templateName.includes("ExportMDFileContent")) {
// noteItem
const data = inputItems?.find((item) => item.isNote());
if (!data) {
html = "<p>No note item selected</p>";
} else {
html = `<pre>${await addon.api.convert.note2md(
data,
Zotero.getTempDirectory().path,
{ withYAMLHeader: false, skipSavingImages: true, keepNoteLink: true },
)}</pre>`;
}
} else if (templateName.includes("QuickInsert")) {
// link, linkText, subNoteItem, noteItem
const data = inputItems?.find((item) => item.isNote());