|
1 | | -import { describe, it, expect } from 'vitest'; |
2 | | -import { renderMarkdown } from '../src/render'; |
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { renderMarkdown, renderMermaid, renderKatex } from '../src/render'; |
| 3 | + |
| 4 | +vi.mock('markedit-api', () => { |
| 5 | + const markEdit: Record<string, unknown> = {}; |
| 6 | + return { MarkEdit: markEdit }; |
| 7 | +}); |
| 8 | + |
| 9 | +// Access the mocked MarkEdit to configure editorView per test |
| 10 | +async function mockDocLines(lines: number) { |
| 11 | + const { MarkEdit } = await import('markedit-api'); |
| 12 | + (MarkEdit as Record<string, unknown>).editorView = { state: { doc: { lines } } }; |
| 13 | +} |
3 | 14 |
|
4 | 15 | describe('renderMarkdown', () => { |
5 | 16 | describe('code blocks without language specifier', () => { |
@@ -57,3 +68,122 @@ describe('renderMarkdown', () => { |
57 | 68 | }); |
58 | 69 | }); |
59 | 70 | }); |
| 71 | + |
| 72 | +describe('renderMermaid', () => { |
| 73 | + it('should wrap content in a mermaid div', async () => { |
| 74 | + await mockDocLines(2); |
| 75 | + const content = 'graph TD\n A --> B'; |
| 76 | + const html = await renderMermaid(content); |
| 77 | + expect(html).toContain('<div class="mermaid">'); |
| 78 | + expect(html).toContain('</div>'); |
| 79 | + expect(html).toContain('graph TD'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should escape HTML in mermaid content', async () => { |
| 83 | + await mockDocLines(1); |
| 84 | + const content = '<script>alert("xss")</script>'; |
| 85 | + const html = await renderMermaid(content); |
| 86 | + expect(html).not.toContain('<script>'); |
| 87 | + expect(html).toContain('<script>'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should trim whitespace from content', async () => { |
| 91 | + await mockDocLines(2); |
| 92 | + const content = ' graph TD\n A --> B \n'; |
| 93 | + const html = await renderMermaid(content); |
| 94 | + expect(html).toBe('<div class="mermaid">graph TD\n A --> B</div>'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should include line info attributes when lineInfo is true', async () => { |
| 98 | + await mockDocLines(3); |
| 99 | + const content = 'graph TD\n A --> B\n B --> C'; |
| 100 | + const html = await renderMermaid(content, true); |
| 101 | + expect(html).toContain('data-line-from="0"'); |
| 102 | + expect(html).toContain('data-line-to="2"'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not include line info attributes by default', async () => { |
| 106 | + await mockDocLines(2); |
| 107 | + const content = 'graph TD\n A --> B'; |
| 108 | + const html = await renderMermaid(content); |
| 109 | + expect(html).not.toContain('data-line-from'); |
| 110 | + expect(html).not.toContain('data-line-to'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle single-line content with lineInfo', async () => { |
| 114 | + await mockDocLines(1); |
| 115 | + const content = 'graph TD'; |
| 116 | + const html = await renderMermaid(content, true); |
| 117 | + expect(html).toContain('data-line-from="0"'); |
| 118 | + expect(html).toContain('data-line-to="0"'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not affect markdown mermaid rendering', async () => { |
| 122 | + const md = '```mermaid\ngraph TD\n```'; |
| 123 | + const html = await renderMarkdown(md); |
| 124 | + expect(html).toContain('<div class="mermaid"'); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('renderKatex', () => { |
| 129 | + it('should wrap content in a katex div', async () => { |
| 130 | + await mockDocLines(1); |
| 131 | + const content = 'E = mc^2'; |
| 132 | + const html = await renderKatex(content); |
| 133 | + expect(html).toContain('<div class="katex">'); |
| 134 | + expect(html).toContain('</div>'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should render KaTeX HTML output', async () => { |
| 138 | + await mockDocLines(1); |
| 139 | + const content = 'x^2 + y^2 = z^2'; |
| 140 | + const html = await renderKatex(content); |
| 141 | + expect(html).toContain('class="katex'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle invalid LaTeX gracefully', async () => { |
| 145 | + await mockDocLines(1); |
| 146 | + const content = '\\invalid{command}'; |
| 147 | + const html = await renderKatex(content); |
| 148 | + expect(html).toContain('<div class="katex">'); |
| 149 | + // With throwOnError: false, KaTeX renders error spans instead of throwing |
| 150 | + expect(html).toBeDefined(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should trim whitespace from content', async () => { |
| 154 | + await mockDocLines(1); |
| 155 | + const content = ' E = mc^2 \n'; |
| 156 | + const html = await renderKatex(content); |
| 157 | + expect(html).toContain('class="katex'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should include line info attributes when lineInfo is true', async () => { |
| 161 | + await mockDocLines(3); |
| 162 | + const content = 'a + b = c'; |
| 163 | + const html = await renderKatex(content, true); |
| 164 | + expect(html).toContain('data-line-from="0"'); |
| 165 | + expect(html).toContain('data-line-to="2"'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should not include line info attributes by default', async () => { |
| 169 | + await mockDocLines(1); |
| 170 | + const content = 'E = mc^2'; |
| 171 | + const html = await renderKatex(content); |
| 172 | + expect(html).not.toContain('data-line-from'); |
| 173 | + expect(html).not.toContain('data-line-to'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should handle single-line content with lineInfo', async () => { |
| 177 | + await mockDocLines(1); |
| 178 | + const content = 'E = mc^2'; |
| 179 | + const html = await renderKatex(content, true); |
| 180 | + expect(html).toContain('data-line-from="0"'); |
| 181 | + expect(html).toContain('data-line-to="0"'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should not affect markdown katex rendering', async () => { |
| 185 | + const md = '$E = mc^2$'; |
| 186 | + const html = await renderMarkdown(md); |
| 187 | + expect(html).toContain('class="katex'); |
| 188 | + }); |
| 189 | +}); |
0 commit comments