| 1 | import { patchNestedMarkdown } from "./patchNestedMarkdown"; |
| 2 | |
| 3 | describe("patchNestedMarkdown", () => { |
| 4 | it("should return unchanged content when no markdown codeblocks are present", () => { |
| 5 | const source = 'Regular text\n```javascript\nconsole.log("hello");\n```'; |
| 6 | expect(patchNestedMarkdown(source)).toBe(source); |
| 7 | }); |
| 8 | |
| 9 | it("should handle nested blocks without a type", () => { |
| 10 | const source = |
| 11 | "```markdown README.md\n# Project Structure\n\n```\n.\n├── AdvancedPage.tsx\n├── Calculator.java\n├── Dockerfile\n├── calculator_test/\n│ ├── Calculator.java\n│ └── Main.java\n├── data.json\n├── example.ipynb\n├── logs/\n├── nested-folder/\n│ ├── helloNested.py\n│ ├── package.json\n│ └── rules.md\n├── program.cs\n├── query.sql\n├── react-calculator/\n├── readme.md\n├── requirements.txt\n├── test.css\n├── test.html\n├── test.js\n├── test.kt\n├── test.php\n├── test.py\n├── test.rb\n├── test.rs\n├── test.sh\n└── test.ts\n```\n```"; |
| 12 | const expected = |
| 13 | "~~~markdown README.md\n# Project Structure\n\n```\n.\n├── AdvancedPage.tsx\n├── Calculator.java\n├── Dockerfile\n├── calculator_test/\n│ ├── Calculator.java\n│ └── Main.java\n├── data.json\n├── example.ipynb\n├── logs/\n├── nested-folder/\n│ ├── helloNested.py\n│ ├── package.json\n│ └── rules.md\n├── program.cs\n├── query.sql\n├── react-calculator/\n├── readme.md\n├── requirements.txt\n├── test.css\n├── test.html\n├── test.js\n├── test.kt\n├── test.php\n├── test.py\n├── test.rb\n├── test.rs\n├── test.sh\n└── test.ts\n```\n~~~"; |
| 14 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 15 | }); |
| 16 | |
| 17 | it("should replace backticks with tildes for markdown codeblocks", () => { |
| 18 | const source = "```markdown\n# Header\nSome text\n```"; |
| 19 | const expected = "~~~markdown\n# Header\nSome text\n~~~"; |
| 20 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 21 | }); |
| 22 | |
| 23 | it("should handle nested codeblocks within markdown blocks", () => { |
| 24 | const source = |
| 25 | '```markdown\n# Example\n```js\nconsole.log("nested");\n```\n```'; |
| 26 | const expected = |
| 27 | '~~~markdown\n# Example\n```js\nconsole.log("nested");\n```\n~~~'; |
| 28 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 29 | }); |
| 30 | |
| 31 | it("should handle .md file extension", () => { |
| 32 | const source = "```test.md\nContent\n```"; |
| 33 | const expected = "~~~test.md\nContent\n~~~"; |
| 34 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 35 | }); |
| 36 | |
| 37 | it("should handle multiple levels of nesting", () => { |
| 38 | const source = |
| 39 | '```markdown\n# Doc\n```js\nlet x = "```nested```";\n```\n```'; |
| 40 | const expected = |
| 41 | '~~~markdown\n# Doc\n```js\nlet x = "```nested```";\n```\n~~~'; |
| 42 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 43 | }); |
| 44 | |
| 45 | it("should handle blocks that start with md", () => { |
| 46 | const source = "```md\n# Header\nSome text\n```"; |
| 47 | const expected = "~~~md\n# Header\nSome text\n~~~"; |
| 48 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 49 | }); |
| 50 | |
| 51 | it("should handle blocks with language specifier followed by md", () => { |
| 52 | const source = "```lang md\n# Header\nSome text\n```"; |
| 53 | const expected = "~~~lang md\n# Header\nSome text\n~~~"; |
| 54 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 55 | }); |
| 56 | |
| 57 | it("should handle blocks with language specifier followed by markdown", () => { |
| 58 | const source = "```lang markdown\n# Header\nSome text\n```"; |
| 59 | const expected = "~~~lang markdown\n# Header\nSome text\n~~~"; |
| 60 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 61 | }); |
| 62 | |
| 63 | it("should not replace backticks for non-markdown file extensions", () => { |
| 64 | const source = "```test.js\nContent\n```"; |
| 65 | expect(patchNestedMarkdown(source)).toBe(source); |
| 66 | }); |
| 67 | |
| 68 | it("should check the file extension branch when extension is not md/markdown/gfm", () => { |
| 69 | // This tests the extension check branch with an unrecognized extension |
| 70 | const source = "```test.txt\nContent with md keyword\n```"; |
| 71 | expect(patchNestedMarkdown(source)).toBe(source); |
| 72 | }); |
| 73 | |
| 74 | it("should handle empty file name in code block header", () => { |
| 75 | // This covers the branch where file is empty or undefined |
| 76 | const source = "``` \nSome content\n```"; |
| 77 | expect(patchNestedMarkdown(source)).toBe(source); |
| 78 | }); |
| 79 | |
| 80 | it("should handle file names with no extension", () => { |
| 81 | // This covers the branch where ext might be undefined |
| 82 | const source = "```filename\nContent\n```"; |
| 83 | expect(patchNestedMarkdown(source)).toBe(source); |
| 84 | }); |
| 85 | |
| 86 | it("should correctly identify .markdown extension", () => { |
| 87 | // This specifically tests the ext === "markdown" condition in the extension check |
| 88 | const source = "```example.markdown\n# Some markdown content\n```"; |
| 89 | const expected = "~~~example.markdown\n# Some markdown content\n~~~"; |
| 90 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 91 | }); |
| 92 | |
| 93 | // GitHub-specific tests |
| 94 | it("should handle gfm language identifier", () => { |
| 95 | const source = "```gfm\n# Header\nSome text\n```"; |
| 96 | const expected = "~~~gfm\n# Header\nSome text\n~~~"; |
| 97 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 98 | }); |
| 99 | |
| 100 | it("should handle github-markdown language identifier", () => { |
| 101 | const source = "```github-markdown\n# Header\nSome text\n```"; |
| 102 | const expected = "~~~github-markdown\n# Header\nSome text\n~~~"; |
| 103 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 104 | }); |
| 105 | |
| 106 | it("should handle language specifier followed by gfm", () => { |
| 107 | const source = "```lang gfm\n# Header\nSome text\n```"; |
| 108 | const expected = "~~~lang gfm\n# Header\nSome text\n~~~"; |
| 109 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 110 | }); |
| 111 | |
| 112 | it("should handle .gfm file extension", () => { |
| 113 | const source = "```example.gfm\n# Some GitHub markdown content\n```"; |
| 114 | const expected = "~~~example.gfm\n# Some GitHub markdown content\n~~~"; |
| 115 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 116 | }); |
| 117 | }); |