diff --git a/test/tests/export.spec.ts b/test/tests/export.spec.ts new file mode 100644 index 0000000..992333d --- /dev/null +++ b/test/tests/export.spec.ts @@ -0,0 +1,255 @@ +/* eslint-disable no-irregular-whitespace */ +/* eslint-disable no-useless-escape */ +import { ClipboardHelper } from "zotero-plugin-toolkit"; +import { getAddon } from "../utils/global"; +import { resetAll } from "../utils/status"; +import { getNoteContent, parseTemplateString } from "../utils/note"; +import { getTempDirectory } from "../utils/io"; + +describe("Export", function () { + const addon = getAddon(); + this.beforeAll(async function () { + await resetAll(); + }); + + this.afterEach(async function () {}); + + it("api.saveMD", async function () { + const note = new Zotero.Item("note"); + note.setNote(getNoteContent()); + await note.saveTx(); + + const tempDir = await getTempDirectory(); + + const filePath = PathUtils.join(tempDir, "test.md"); + + await getAddon().api.$export.saveMD(filePath, note.id, { + keepNoteLink: true, + withYAMLHeader: false, + }); + + debug("Note saved to", filePath); + + const content = await Zotero.File.getContentsAsync(filePath); + + const expected = `# Markdown Test Document + +## Headers + +# H1 Header + +## H2 Header + +### H3 Header + +#### H4 Header + +##### H5 Header + +###### H6 Header + +## Emphasis + +*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.*** + +## Links + +[Link with title](https://example.com "Title") [Link without title](https://example.com) + +## Images + +## Blockquotes + +> This is a blockquote. +> +> > Nested blockquote. +> +> Back to the outer blockquote. + +## Lists + +### Unordered List + +* Item 1 + + * Subitem 1.1 + + * Subitem 1.1.1 + +* Item 2 + +### Ordered List + +1. First item + + 1. Subitem 1.1 + + 1. Subitem 1.1.1 + +2. Second item + +## Code + +### Inline Code + +Here is some \`inline code\`. + +### Code Block + +\`\`\` +def hello_world(): + print("Hello, world!") + +\`\`\` + +## Horizontal Rules + +*** + +This is text between horizontal rules + +*** + +## Tables + +| Header 1 | Header 2 | Header 3 | +| -------- | -------- | -------- | +| Row 1 | Data 1.2 | Data 1.3 | +| Row 2 | Data 2.2 | Data 2.3 | + +## Math + +### Inline Math + +This is an inline math equation: \$E = mc^2\$. + +### Block Math + +Below is a block math equation: + +\$\$ +\\\\int_a^b f(x) \\\\, dx = F(b) - F(a) +\$\$ + +### Complex Math + +Solve the quadratic equation: + +\$\$ +x = \\\\frac\{-b \\\\pm \\\\sqrt\{b^2 - 4ac\}\}\{2a\} +\$\$ + +## Nested Elements + +### Nested Code and Lists + +1. Ordered list item + + * Unordered subitem + + \`\`\` + console.log("Nested code block"); + + \`\`\` + +2. This is a nested math + + \$\$ + y=x^2 + \$\$ + +3. This is a inline math\$123\$ + +4. This is a line table + + | | | | + | - | - | - | + | 1 | 2 | 3 | + | 4 | 5 | 6 | + | 7 | 8 | 9 | + +## Special Characters + +Escape sequences for special characters: \\* \\_ \\\\\\\` \\[ ] ( ) # + - . + +## HTML in Markdown + +This is a HTML block inside Markdown. + +## Highlight Text + +Highlight text is here + +## Colored Text + +Colored text is here + +## Task Lists + +* Completed item +* Incomplete item + +## Strikethrough + +~~This text is strikethrough.~~ + +## Recursive Elements + +### Recursive Links and Emphasis + +**[Bold link](https://example.com)** + +### Recursive Emphasis + +***Bold and nested italic within bold.*** + +## Image + +IMAGE\\_PLACEHOLDER + +## Citation + +CITATION\\_PLACEHOLDER + +## Edge Cases + +### Empty Link + +[Link](https://) + +### Zotero Link + +[Zotero Link](zotero://note/u/123456) + +### Lone Asterisk + +* This should not be italic. + +### Broken Lists + +* Item 1 + + * Item 2Continuation of item 2 without proper indentation. + +### Long Text Wrapping + +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. + +*** + +## Conclusion + +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. +`; + + // new ClipboardHelper() + // .addText(parseTemplateString(content as string)) + // .copy(); + + assert.equal(content, expected); + }); +}); diff --git a/test/tests/template.spec.ts b/test/tests/template.spec.ts index a060ae8..7d5351a 100644 --- a/test/tests/template.spec.ts +++ b/test/tests/template.spec.ts @@ -1,6 +1,9 @@ +/* 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(); @@ -38,10 +41,259 @@ describe("Template", function () { it("api.template.renderTemplatePreview", async function () { const key = importTemplate(); const preview = await addon.api.template.renderTemplatePreview(key!); - const expected = - '
\n
This text is italicized. This text is also italicized.
\nThis text is bold. This text is also bold.
\nThis text is bold and italicized. This text is also bold and italicized.
\nLink with title Link without title
\n\n\nThis is a blockquote.
\n\n\nNested blockquote.
\nBack to the outer blockquote.
\n
Item 1
\nSubitem 1.1
\nFirst item
\nSubitem 1.1
\nHere is some inline code.
def hello_world():\n print("Hello, world!")\n\nThis is text between horizontal rules
\n| \n Header 1 \n | \n\n Header 2 \n | \n\n Header 3 \n | \n
|---|---|---|
| \n Row 1 \n | \n\n Data 1.2 \n | \n\n Data 1.3 \n | \n
| \n Row 2 \n | \n\n Data 2.2 \n | \n\n Data 2.3 \n | \n
This is an inline math equation: .
\nBelow is a block math equation:
\n\nSolve the quadratic equation:
\n\nList item
\nNested list item
\n\n\nNested blockquote within a list
\n
Ordered list item
\nUnordered subitem
\nconsole.log("Nested code block");\n\nEscape sequences for special characters: * _ ` [ ] ( ) # + - .
\nThis is a HTML block inside Markdown.
\nHighlight text is here
\nColored text is here
\nThis text is strikethrough.
\nBold and nested italic within bold.
\nItem 1
\nItem 2
\nContinuation of item 2 without proper indentation.
\nThis 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.
\nThis 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.
'; + 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.
+Title: ]Title 0
\n5
\nTitle: ]Title 1
\n5
\nTitle: ]Title 2
\n5
\n> Done! But Markdown is not rendered correctly. Try to add \n'; + // 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); @@ -119,206 +378,10 @@ zoteroVersion: "7.0.12-beta.1+31bbf2acf" pluginVersion: "2.2.3-beta.2" savedAt: "2025-01-06T09:12:14.939Z" content: |- --
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: $E = mc^2$.
-Below is a block math equation:
-$$\\\\int_a^b f(x) \\\\, dx = F(b) - F(a)$$-
Solve the quadratic equation:
-$$x = \\\\frac{-b \\\\pm \\\\sqrt{b^2 - 4ac}}{2a}$$
- List item
-Nested list item
---Nested blockquote within a list
-
Ordered list item
-Unordered subitem
-console.log("Nested code block");
-
- 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.
-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.
+${getNoteContent() + .split("\n") + .map((line) => ` ${line}`) + .join("\n")} `; return getAddon().hooks.onImportTemplateFromClipboard(shareCode, { quiet: true, diff --git a/test/utils/io.ts b/test/utils/io.ts new file mode 100644 index 0000000..b484a27 --- /dev/null +++ b/test/utils/io.ts @@ -0,0 +1,16 @@ +export async function getTempDirectory() { + let path = ""; + let attempts = 3; + const zoteroTmpDirPath = Zotero.getTempDirectory().path; + while (attempts--) { + path = PathUtils.join(zoteroTmpDirPath, Zotero.Utilities.randomString()); + try { + await IOUtils.makeDirectory(path, { ignoreExisting: false }); + break; + } catch (e) { + if (!attempts) throw e; // Throw on last attempt + } + } + + return path; +} diff --git a/test/utils/note.ts b/test/utils/note.ts new file mode 100644 index 0000000..23f258a --- /dev/null +++ b/test/utils/note.ts @@ -0,0 +1,250 @@ +export function getNoteContent() { + return `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: $E = mc^2$.
+Below is a block math equation:
+$$\\\\int_a^b f(x) \\\\, dx = F(b) - F(a)$$+
Solve the quadratic equation:
+$$x = \\\\frac{-b \\\\pm \\\\sqrt{b^2 - 4ac}}{2a}$$
+Ordered list item
+Unordered subitem
+console.log("Nested code block");
+
+This is a nested math
+$$y=x^2$$+
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.
+