/* eslint-disable no-useless-escape */ /* eslint-disable no-irregular-whitespace */ import { ClipboardHelper } from "zotero-plugin-toolkit"; import { getAddon } from "../utils/global"; import { resetAll } from "../utils/status"; import { getNoteContent, parseTemplateString } from "../utils/note"; describe("Template", function () { const addon = getAddon(); this.beforeAll(async function () { await resetAll(); }); this.afterEach(async function () {}); it("hooks.onImportTemplateFromClipboard", async function () { const key = importTemplate(); assert.isNotEmpty(key); addon.api.template.removeTemplate(key); }); it("api.template.getTemplateText", async function () { const key = importTemplate(); assert.isNotEmpty(addon.api.template.getTemplateText(key!)); addon.api.template.removeTemplate(key); }); it("api.template.getTemplateText", async function () { const key = importTemplate(); assert.isTrue(addon.api.template.getTemplateKeys().includes(key!)); addon.api.template.removeTemplate(key); }); it("api.template.removeTemplate", async function () { const key = importTemplate(); assert.isNotEmpty(key); addon.api.template.removeTemplate(key!); assert.isFalse(addon.api.template.getTemplateKeys().includes(key!)); }); it("api.template.renderTemplatePreview", async function () { const key = importTemplate(); const preview = await addon.api.template.renderTemplatePreview(key!); const expected = `
This text is italicized. This text is also italicized.
This text is bold. This text is also bold.
This text is bold and italicized. This text is also bold and italicized.
Link with title Link without title
This is a blockquote.
Nested blockquote.
Back to the outer blockquote.
Item 1
Subitem 1.1
First item
Subitem 1.1
Here is some inline code.
def hello_world():
print("Hello, world!")
This is text between horizontal rules
|
Header 1 |
Header 2 |
Header 3 |
|---|---|---|
|
Row 1 |
Data 1.2 |
Data 1.3 |
|
Row 2 |
Data 2.2 |
Data 2.3 |
This is an inline math equation: .
Below is a block math equation:
Solve the quadratic equation:
Ordered list item
Unordered subitem
console.log("Nested code block");
This is a nested math
This is a line table
|
1 |
2 |
3 |
|
4 |
5 |
6 |
|
7 |
8 |
9 |
Escape sequences for special characters: * _ \` [ ] ( ) # + - .
This is a HTML block inside Markdown.
Highlight text is here
Colored text is here
This text is strikethrough.
Bold and nested italic within bold.
IMAGE_PLACEHOLDER
CITATION_PLACEHOLDER
Item 1
Item 2
Continuation of item 2 without proper indentation.
This is a very long paragraph that does not have any line breaks and is intended to test how the Markdown engine handles text wrapping when there are no explicit line breaks within the text.
This document contains a wide range of Markdown elements, including headers, lists, blockquotes, inline and block code, tables, images, links, math, and special characters. It also tests recursive and edge cases to ensure the Markdown engine is robust.
${targetNoteItem.id}
", }); const note = new Zotero.Item("note"); await note.saveTx(); const html = await addon.api.template.runTextTemplate("[text]Test", { targetNoteId: note.id, }); assert.equal(html, `${note.id}
`); await Zotero.Items.erase(note.id); addon.api.template.removeTemplate("[text]Test"); }); it("api.template.runItemTemplate", async function () { // Also test the use of Markdown pragma addon.api.template.setTemplate({ name: "[item]Test", text: ` // @beforeloop-begin // @use-markdown # Hi! This only renders once // @beforeloop-end // @default-beginTitle: ]\${topItem.getField("title")}
\${{ const note = Zotero.Items.get(targetNoteItem.id); return "" + note.id + "
"; }}$ // @default-end // @afterloop-begin > Done! But Markdown is not rendered correctly. Try to add \`// @use-markdown\` pragma before this line. // @afterloop-end `, }); const items = []; for (let i = 0; i < 3; i++) { const item = new Zotero.Item("book"); item.setField("title", `Title ${i}`); await item.saveTx(); items.push(item); } const note = new Zotero.Item("note"); await note.saveTx(); const html = await addon.api.template.runItemTemplate("[item]Test", { itemIds: items.map((item) => item.id), targetNoteId: note.id, }); // new ClipboardHelper().addText(parseTemplateString(html)).copy(); const expected = `Title: ]Title 0
6
Title: ]Title 1
6
Title: ]Title 2
6
> Done! But Markdown is not rendered correctly. Try to add `; assert.equal(html, expected); for (const item of items) { await Zotero.Items.erase(item.id); } await Zotero.Items.erase(note.id); addon.api.template.removeTemplate("[item]Test"); }); }); function importTemplate() { const shareCode = ` # This template is specifically for importing/sharing, using better # notes 'import from clipboard': copy the content and # goto Zotero menu bar, click Tools->New Template from Clipboard. # Do not copy-paste this to better notes template editor directly. name: "[text]TestGen" zoteroVersion: "7.0.12-beta.1+31bbf2acf" pluginVersion: "2.2.3-beta.2" savedAt: "2025-01-06T09:12:14.939Z" content: |- ${getNoteContent() .split("\n") .map((line) => ` ${line}`) .join("\n")} `; return getAddon().hooks.onImportTemplateFromClipboard(shareCode, { quiet: true, }); }