diff --git a/test/tests/export.spec.ts b/test/tests/export.spec.ts new file mode 100644 index 0000000..db4bf26 --- /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.$export.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 = - '

Test Document

\n

\n

Headers

\n

H1 Header

\n

H2 Header

\n

H3 Header

\n

H4 Header

\n
H5 Header
\n
H6 Header
\n

Emphasis

\n

This text is italicized. This text is also italicized.

\n

This text is bold. This text is also bold.

\n

This text is bold and italicized. This text is also bold and italicized.

\n

Links

\n

Link with title Link without title

\n

Images

\n

\n

Blockquotes

\n
\n

This is a blockquote.

\n
\n

Nested blockquote.

\n
\n

Back to the outer blockquote.

\n
\n

Lists

\n

Unordered List

\n\n

Ordered List

\n
    \n
  1. \n

    First item

    \n
      \n
    1. \n

      Subitem 1.1

      \n
        \n
      1. \nSubitem 1.1.1\n
      2. \n
      \n
    2. \n
    \n
  2. \n
  3. \nSecond item\n
  4. \n
\n

Code

\n

Inline Code

\n

Here is some inline code.

\n

Code Block

\n
def hello_world():\n    print("Hello, world!")\n
\n

Horizontal Rules

\n
\n

This is text between horizontal rules

\n
\n

Tables

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n

Header 1

\n
\n

Header 2

\n
\n

Header 3

\n
\n

Row 1

\n
\n

Data 1.2

\n
\n

Data 1.3

\n
\n

Row 2

\n
\n

Data 2.2

\n
\n

Data 2.3

\n
\n

Math

\n

Inline Math

\n

This is an inline math equation: E=mc2E = mc^2.

\n

Block Math

\n

Below is a block math equation:

\n
abf(x)dx=F(b)F(a)\\int_a^b f(x) \\, dx = F(b) - F(a)
\n

Complex Math

\n

Solve the quadratic equation:

\n
x=b±b24ac2ax = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}
\n

Nested Elements

\n

Nested Lists and Blockquotes

\n\n

Nested Code and Lists

\n
    \n
  1. \n

    Ordered list item

    \n\n
  2. \n
\n

Special Characters

\n

Escape sequences for special characters: * _ ` [ ] ( ) # + - .

\n

HTML in Markdown

\n

This is a HTML block inside Markdown.

\n

Highlight Text

\n

Highlight text is here

\n

Colored Text

\n

Colored text is here

\n

Task Lists

\n\n

Strikethrough

\n

This text is strikethrough.

\n

Recursive Elements

\n

Recursive Links and Emphasis

\n

Bold link

\n

Recursive Emphasis

\n

Bold and nested italic within bold.

\n

Edge Cases

\n

Empty Link

\n

\n

Lone Asterisk

\n\n

Broken Lists

\n\n

Long Text Wrapping

\n

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.

\n
\n

Conclusion

\n

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.

'; + 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 Link without title

+

Images

+

+

Blockquotes

+
+

This is a blockquote.

+
+

Nested blockquote.

+
+

Back to the outer blockquote.

+
+

Lists

+

Unordered List

+ +

Ordered List

+
    +
  1. +

    First item

    +
      +
    1. +

      Subitem 1.1

      +
        +
      1. +Subitem 1.1.1 +
      2. +
      +
    2. +
    +
  2. +
  3. +Second item +
  4. +
+

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=mc2E = mc^2.

+

Block Math

+

Below is a block math equation:

+
abf(x)dx=F(b)F(a)\\int_a^b f(x) \\, dx = F(b) - F(a)
+

Complex Math

+

Solve the quadratic equation:

+
x=b±b24ac2ax = \\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. +
  3. +

    This is a nested math

    +
    y=x2y=x^2
    +
  4. +
  5. +This is a inline math123123 +
  6. +
  7. +

    This is a line table

    + + + + + + + + + + + + + + + + + + +
    +

    1

    +
    +

    2

    +
    +

    3

    +
    +

    4

    +
    +

    5

    +
    +

    6

    +
    +

    7

    +
    +

    8

    +
    +

    9

    +
    +
  8. +
+

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

+ +

Strikethrough

+

This text is strikethrough.

+

Recursive Elements

+

Recursive Links and Emphasis

+

Bold link

+

Recursive Emphasis

+

Bold and nested italic within bold.

+

Image

+

IMAGE_PLACEHOLDER

+

Citation

+

CITATION_PLACEHOLDER

+

Edge Cases

+

Empty Link

+

Link

+

Zotero Link

+

Zotero Link

+

Lone Asterisk

+ +

Broken Lists

+ +

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.

+
`; // debug(preview); - // new ClipboardHelper().addText(expected).copy(); + // If the template changes, update the expected value by copying the expected value + // new ClipboardHelper().addText(parseTemplateString(preview)).copy(); assert.equal(preview, expected); addon.api.template.removeTemplate(key); }); @@ -96,9 +348,16 @@ describe("Template", function () { itemIds: items.map((item) => item.id), targetNoteId: note.id, }); - // new ClipboardHelper().addText(html).copy(); - const expected = - '

Hi! This only renders once

\n

Title: ]Title 0

\n

5

\n

Title: ]Title 1

\n

5

\n

Title: ]Title 2

\n

5

\n> Done! But Markdown is not rendered correctly. Try to add \n'; + // new ClipboardHelper().addText(parseTemplateString(html)).copy(); + const expected = `

Hi! This only renders once

+

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

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 Link without title

-

Images

-

-

Blockquotes

-
-

This is a blockquote.

-
-

Nested blockquote.

-
-

Back to the outer blockquote.

-
-

Lists

-

Unordered List

- -

Ordered List

-
    -
  1. -

    First item

    -
      -
    1. -

      Subitem 1.1

      -
        -
      1. - Subitem 1.1.1 -
      2. -
      -
    2. -
    -
  2. -
  3. - Second item -
  4. -
-

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 Lists and Blockquotes

- -

Nested Code and Lists

-
    -
  1. -

    Ordered list item

    - -
  2. -
-

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

- -

Strikethrough

-

This text is strikethrough.

-

Recursive Elements

-

Recursive Links and Emphasis

-

Bold link

-

Recursive Emphasis

-

Bold and nested italic within bold.

-

Edge Cases

-

Empty Link

-

-

Lone Asterisk

- -

Broken Lists

- -

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.

+${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 `

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 Link without title

+

Images

+

+

Blockquotes

+
+

This is a blockquote.

+
+

Nested blockquote.

+
+

Back to the outer blockquote.

+
+

Lists

+

Unordered List

+ +

Ordered List

+
    +
  1. +

    First item

    +
      +
    1. +

      Subitem 1.1

      +
        +
      1. +Subitem 1.1.1 +
      2. +
      +
    2. +
    +
  2. +
  3. +Second item +
  4. +
+

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. +
  3. +

    This is a nested math

    +
    $$y=x^2$$
    +
  4. +
  5. +This is a inline math$123$ +
  6. +
  7. +

    This is a line table

    + + + + + + + + + + + + + + + + + + +
    +

    1

    +
    +

    2

    +
    +

    3

    +
    +

    4

    +
    +

    5

    +
    +

    6

    +
    +

    7

    +
    +

    8

    +
    +

    9

    +
    +
  8. +
+

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

+ +

Strikethrough

+

This text is strikethrough.

+

Recursive Elements

+

Recursive Links and Emphasis

+

Bold link

+

Recursive Emphasis

+

Bold and nested italic within bold.

+

Image

+

IMAGE_PLACEHOLDER

+

Citation

+

CITATION_PLACEHOLDER

+

Edge Cases

+

Empty Link

+

Link

+

Zotero Link

+

Zotero Link

+

Lone Asterisk

+ +

Broken Lists

+ +

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.

+
`; +} + +export function parseTemplateString(input: string): string { + return input + .replace(/\\/g, "\\\\") // Escape backslashes + .replace(/`/g, "\\`") // Escape backticks + .replace(/\$/g, "\\$") // Escape dollar signs + .replace(/{/g, "\\{") // Escape opening braces + .replace(/}/g, "\\}"); // Escape closing braces +}