|
1 |
| -import { processLaTeX } from './latex'; |
| 1 | +/* eslint-disable no-useless-escape */ |
| 2 | +import { processLaTeX, preprocessLaTeX } from './latex'; |
2 | 3 |
|
3 | 4 | describe('processLaTeX', () => {
|
4 | 5 | test('returns the same string if no LaTeX patterns are found', () => {
|
@@ -103,3 +104,92 @@ describe('processLaTeX', () => {
|
103 | 104 | });
|
104 | 105 | });
|
105 | 106 | });
|
| 107 | + |
| 108 | +describe('preprocessLaTeX', () => { |
| 109 | + test('returns the same string if no LaTeX patterns are found', () => { |
| 110 | + const content = 'This is a test string without LaTeX'; |
| 111 | + expect(preprocessLaTeX(content)).toBe(content); |
| 112 | + }); |
| 113 | + |
| 114 | + test('escapes dollar signs followed by digits', () => { |
| 115 | + const content = 'Price is $50 and $100'; |
| 116 | + const expected = 'Price is \\$50 and \\$100'; |
| 117 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 118 | + }); |
| 119 | + |
| 120 | + test('does not escape dollar signs not followed by digits', () => { |
| 121 | + const content = 'This $variable is not escaped'; |
| 122 | + expect(preprocessLaTeX(content)).toBe(content); |
| 123 | + }); |
| 124 | + |
| 125 | + test('preserves existing LaTeX expressions', () => { |
| 126 | + const content = 'Inline $x^2 + y^2 = z^2$ and block $$E = mc^2$$'; |
| 127 | + expect(preprocessLaTeX(content)).toBe(content); |
| 128 | + }); |
| 129 | + |
| 130 | + test('handles mixed LaTeX and currency', () => { |
| 131 | + const content = 'LaTeX $x^2$ and price $50'; |
| 132 | + const expected = 'LaTeX $x^2$ and price \\$50'; |
| 133 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 134 | + }); |
| 135 | + |
| 136 | + test('converts LaTeX delimiters', () => { |
| 137 | + const content = 'Brackets \\[x^2\\] and parentheses \\(y^2\\)'; |
| 138 | + const expected = 'Brackets $$x^2$$ and parentheses $y^2$'; |
| 139 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 140 | + }); |
| 141 | + |
| 142 | + test('escapes mhchem commands', () => { |
| 143 | + const content = '$\\ce{H2O}$ and $\\pu{123 J}$'; |
| 144 | + const expected = '$\\\\ce{H2O}$ and $\\\\pu{123 J}$'; |
| 145 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 146 | + }); |
| 147 | + |
| 148 | + test('handles complex mixed content', () => { |
| 149 | + const content = ` |
| 150 | + LaTeX inline $x^2$ and block $$y^2$$ |
| 151 | + Currency $100 and $200 |
| 152 | + Chemical $\\ce{H2O}$ |
| 153 | + Brackets \\[z^2\\] |
| 154 | + `; |
| 155 | + const expected = ` |
| 156 | + LaTeX inline $x^2$ and block $$y^2$$ |
| 157 | + Currency \\$100 and \\$200 |
| 158 | + Chemical $\\\\ce{H2O}$ |
| 159 | + Brackets $$z^2$$ |
| 160 | + `; |
| 161 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 162 | + }); |
| 163 | + |
| 164 | + test('handles empty string', () => { |
| 165 | + expect(preprocessLaTeX('')).toBe(''); |
| 166 | + }); |
| 167 | + |
| 168 | + test('preserves code blocks', () => { |
| 169 | + const content = '```\n$100\n```\nOutside $200'; |
| 170 | + const expected = '```\n$100\n```\nOutside \\$200'; |
| 171 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 172 | + }); |
| 173 | + |
| 174 | + test('handles multiple currency values in a sentence', () => { |
| 175 | + const content = 'I have $50 in my wallet and $100 in the bank.'; |
| 176 | + const expected = 'I have \\$50 in my wallet and \\$100 in the bank.'; |
| 177 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 178 | + }); |
| 179 | + |
| 180 | + test('preserves LaTeX expressions with numbers', () => { |
| 181 | + const content = 'The equation is $f(x) = 2x + 3$ where x is a variable.'; |
| 182 | + expect(preprocessLaTeX(content)).toBe(content); |
| 183 | + }); |
| 184 | + |
| 185 | + test('handles currency values with commas', () => { |
| 186 | + const content = 'The price is $1,000,000 for this item.'; |
| 187 | + const expected = 'The price is \\$1,000,000 for this item.'; |
| 188 | + expect(preprocessLaTeX(content)).toBe(expected); |
| 189 | + }); |
| 190 | + |
| 191 | + test('preserves LaTeX expressions with special characters', () => { |
| 192 | + const content = 'The set is defined as $\\{x | x > 0\\}$.'; |
| 193 | + expect(preprocessLaTeX(content)).toBe(content); |
| 194 | + }); |
| 195 | +}); |
0 commit comments