| 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 replace backticks with tildes for markdown codeblocks', () => { |
| 10 | const source = '```markdown\n# Header\nSome text\n```'; |
| 11 | const expected = '~~~markdown\n# Header\nSome text\n~~~'; |
| 12 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 13 | }); |
| 14 | |
| 15 | it('should handle nested codeblocks within markdown blocks', () => { |
| 16 | const source = '```markdown\n# Example\n```js\nconsole.log("nested");\n```\n```'; |
| 17 | const expected = '~~~markdown\n# Example\n```js\nconsole.log("nested");\n```\n~~~'; |
| 18 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 19 | }); |
| 20 | |
| 21 | it('should handle .md file extension', () => { |
| 22 | const source = '```test.md\nContent\n```'; |
| 23 | const expected = '~~~test.md\nContent\n~~~'; |
| 24 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 25 | }); |
| 26 | |
| 27 | it('should handle multiple levels of nesting', () => { |
| 28 | const source = '```markdown\n# Doc\n```js\nlet x = "```nested```";\n```\n```'; |
| 29 | const expected = '~~~markdown\n# Doc\n```js\nlet x = "```nested```";\n```\n~~~'; |
| 30 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 31 | }); |
| 32 | |
| 33 | it('should handle blocks that start with md', () => { |
| 34 | const source = '```md\n# Header\nSome text\n```'; |
| 35 | const expected = '~~~md\n# Header\nSome text\n~~~'; |
| 36 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 37 | }); |
| 38 | |
| 39 | it('should handle blocks with language specifier followed by md', () => { |
| 40 | const source = '```lang md\n# Header\nSome text\n```'; |
| 41 | const expected = '~~~lang md\n# Header\nSome text\n~~~'; |
| 42 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 43 | }); |
| 44 | |
| 45 | it('should handle blocks with language specifier followed by markdown', () => { |
| 46 | const source = '```lang markdown\n# Header\nSome text\n```'; |
| 47 | const expected = '~~~lang markdown\n# Header\nSome text\n~~~'; |
| 48 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 49 | }); |
| 50 | |
| 51 | it('should not replace backticks for non-markdown file extensions', () => { |
| 52 | const source = '```test.js\nContent\n```'; |
| 53 | expect(patchNestedMarkdown(source)).toBe(source); |
| 54 | }); |
| 55 | |
| 56 | it('should check the file extension branch when extension is not md/markdown/gfm', () => { |
| 57 | // This tests the extension check branch with an unrecognized extension |
| 58 | const source = '```test.txt\nContent with md keyword\n```'; |
| 59 | expect(patchNestedMarkdown(source)).toBe(source); |
| 60 | }); |
| 61 | |
| 62 | it('should handle empty file name in code block header', () => { |
| 63 | // This covers the branch where file is empty or undefined |
| 64 | const source = '``` \nSome content\n```'; |
| 65 | expect(patchNestedMarkdown(source)).toBe(source); |
| 66 | }); |
| 67 | |
| 68 | it('should handle file names with no extension', () => { |
| 69 | // This covers the branch where ext might be undefined |
| 70 | const source = '```filename\nContent\n```'; |
| 71 | expect(patchNestedMarkdown(source)).toBe(source); |
| 72 | }); |
| 73 | |
| 74 | it('should correctly identify .markdown extension', () => { |
| 75 | // This specifically tests the ext === "markdown" condition in the extension check |
| 76 | const source = '```example.markdown\n# Some markdown content\n```'; |
| 77 | const expected = '~~~example.markdown\n# Some markdown content\n~~~'; |
| 78 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 79 | }); |
| 80 | |
| 81 | // GitHub-specific tests |
| 82 | it('should handle gfm language identifier', () => { |
| 83 | const source = '```gfm\n# Header\nSome text\n```'; |
| 84 | const expected = '~~~gfm\n# Header\nSome text\n~~~'; |
| 85 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 86 | }); |
| 87 | |
| 88 | it('should handle github-markdown language identifier', () => { |
| 89 | const source = '```github-markdown\n# Header\nSome text\n```'; |
| 90 | const expected = '~~~github-markdown\n# Header\nSome text\n~~~'; |
| 91 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 92 | }); |
| 93 | |
| 94 | it('should handle language specifier followed by gfm', () => { |
| 95 | const source = '```lang gfm\n# Header\nSome text\n```'; |
| 96 | const expected = '~~~lang gfm\n# Header\nSome text\n~~~'; |
| 97 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 98 | }); |
| 99 | |
| 100 | it('should handle .gfm file extension', () => { |
| 101 | const source = '```example.gfm\n# Some GitHub markdown content\n```'; |
| 102 | const expected = '~~~example.gfm\n# Some GitHub markdown content\n~~~'; |
| 103 | expect(patchNestedMarkdown(source)).toBe(expected); |
| 104 | }); |
| 105 | }); |