File size: 9,462 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, beforeAll, vi } from 'vitest';
import chalk from 'chalk';
import { parseMarkdownToANSI } from './markdownParsingUtils.js';

// Mock the theme to use explicit colors instead of empty strings from the default theme.
// This ensures that ansiColorize actually applies ANSI codes that we can verify.
vi.mock('../semantic-colors.js', () => ({
  theme: {
    text: {
      primary: 'white',
      accent: 'cyan',
      link: 'blue',
    },
    ui: {
      focus: 'green',
    },
  },
}));

import { theme } from '../semantic-colors.js';
import { resolveColor, INK_NAME_TO_HEX_MAP } from '../themes/color-utils.js';
import { themeManager, DEFAULT_THEME } from '../themes/theme-manager.js';

describe('parsingUtils', () => {
  beforeAll(() => {
    themeManager.setActiveTheme(DEFAULT_THEME.name);
    themeManager.setTerminalBackground(undefined);
  });

  /**
   * Helper to replicate the colorization logic for expected values.
   */
  const expectedColorize = (str: string, color: string) => {
    const resolved = resolveColor(color);
    if (!resolved) return str;
    if (resolved.startsWith('#')) return chalk.hex(resolved)(str);
    const mappedHex = INK_NAME_TO_HEX_MAP[resolved];
    if (mappedHex) return chalk.hex(mappedHex)(str);

    // Simple mapping for standard colors if they aren't in the hex map
    switch (resolved) {
      case 'black':
        return chalk.black(str);
      case 'red':
        return chalk.red(str);
      case 'green':
        return chalk.green(str);
      case 'yellow':
        return chalk.yellow(str);
      case 'blue':
        return chalk.blue(str);
      case 'magenta':
        return chalk.magenta(str);
      case 'cyan':
        return chalk.cyan(str);
      case 'white':
        return chalk.white(str);
      case 'gray':
      case 'grey':
        return chalk.gray(str);
      default:
        return str;
    }
  };

  const primary = (str: string) => expectedColorize(str, theme.text.primary);
  const accent = (str: string) => expectedColorize(str, theme.text.accent);
  const link = (str: string) => expectedColorize(str, theme.text.link);

  describe('parseMarkdownToANSI', () => {
    it('should return plain text with default color', () => {
      const input = 'Hello world';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(primary(input));
    });

    it('should handle bold text', () => {
      const input = 'This is **bold** text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.bold(primary('bold'))}${primary(' text')}`,
      );
    });

    it('should handle italic text with *', () => {
      const input = 'This is *italic* text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.italic(primary('italic'))}${primary(' text')}`,
      );
    });

    it('should handle italic text with _', () => {
      const input = 'This is _italic_ text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.italic(primary('italic'))}${primary(' text')}`,
      );
    });

    it('should handle bold italic text with ***', () => {
      const input = 'This is ***bold italic*** text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.bold(chalk.italic(primary('bold italic')))}${primary(' text')}`,
      );
    });

    it('should handle strikethrough text', () => {
      const input = 'This is ~~strikethrough~~ text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.strikethrough(primary('strikethrough'))}${primary(' text')}`,
      );
    });

    it('should handle inline code', () => {
      const input = 'This is `code` text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${accent('code')}${primary(' text')}`,
      );
    });

    it('should handle links', () => {
      const input = 'Check [this link](https://example.com)';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('Check ')}${primary('this link')}${primary(' (')}${link(
          'https://example.com',
        )}${primary(')')}`,
      );
    });

    it('should handle bare URLs', () => {
      const input = 'Visit https://google.com now';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('Visit ')}${link('https://google.com')}${primary(' now')}`,
      );
    });

    it('should handle underline tags', () => {
      const input = 'This is <u>underlined</u> text';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${primary('This is ')}${chalk.underline(primary('underlined'))}${primary(' text')}`,
      );
    });

    it('should handle complex mixed markdown', () => {
      const input = '**Bold** and *italic* and `code` and [link](url)';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        `${chalk.bold(primary('Bold'))}${primary(' and ')}${chalk.italic(
          primary('italic'),
        )}${primary(' and ')}${accent('code')}${primary(' and ')}${primary(
          'link',
        )}${primary(' (')}${link('url')}${primary(')')}`,
      );
    });

    it('should respect custom default color', () => {
      const customColor = 'cyan';
      const input = 'Hello **world**';
      const output = parseMarkdownToANSI(input, customColor);
      const cyan = (str: string) => expectedColorize(str, 'cyan');
      expect(output).toBe(`${cyan('Hello ')}${chalk.bold(cyan('world'))}`);
    });

    it('should handle nested formatting in bold/italic', () => {
      const input = '**Bold with *italic* inside**';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        chalk.bold(
          `${primary('Bold with ')}${chalk.italic(primary('italic'))}${primary(
            ' inside',
          )}`,
        ),
      );
    });

    it('should handle hex colors as default', () => {
      const hexColor = '#ff00ff';
      const input = 'Hello **world**';
      const output = parseMarkdownToANSI(input, hexColor);
      const magenta = (str: string) => chalk.hex('#ff00ff')(str);
      expect(output).toBe(
        `${magenta('Hello ')}${chalk.bold(magenta('world'))}`,
      );
    });

    it('should override default color with link color', () => {
      const input = 'Check [link](url)';
      const output = parseMarkdownToANSI(input, 'red');
      const red = (str: string) => chalk.red(str);
      expect(output).toBe(
        `${red('Check ')}${red('link')}${red(' (')}${link('url')}${red(')')}`,
      );
    });

    it('should override default color with accent color for code', () => {
      const input = 'Code: `const x = 1`';
      const output = parseMarkdownToANSI(input, 'green');
      const green = (str: string) => chalk.green(str);
      const cyan = (str: string) => chalk.cyan(str);
      expect(output).toBe(`${green('Code: ')}${cyan('const x = 1')}`);
    });

    it('should handle nested formatting with color overrides', () => {
      const input = '**Bold with `code` inside**';
      const output = parseMarkdownToANSI(input);
      expect(output).toBe(
        chalk.bold(
          `${primary('Bold with ')}${accent('code')}${primary(' inside')}`,
        ),
      );
    });

    describe('LaTeX conversion (issue #25656)', () => {
      it('converts LaTeX in plain text (no markdown tokens)', () => {
        const input = 'No cycles $\\to$ no deadlock';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(primary('No cycles → no deadlock'));
      });

      it('converts LaTeX in the set example from the issue', () => {
        const input = 'Processes $\\{P_0, \\dots, P_n\\}$';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(primary('Processes {P₀, …, Pₙ}'));
      });

      it('preserves LaTeX inside inline code', () => {
        // Content between backticks must be rendered verbatim — conversion
        // must NOT be applied inside code spans, even when the code contains
        // `$...$` that would otherwise be stripped.
        const input = 'use `$\\to$` for an arrow';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(
          `${primary('use ')}${accent('$\\to$')}${primary(' for an arrow')}`,
        );
      });

      it('converts LaTeX in slices around markdown tokens', () => {
        const input = '$\\alpha$ is **bold** and $\\beta$ is plain';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(
          `${primary('α is ')}${chalk.bold(primary('bold'))}${primary(
            ' and β is plain',
          )}`,
        );
      });

      it('leaves Windows paths alone', () => {
        const input = 'Path: C:\\Users\\foo';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(primary('Path: C:\\Users\\foo'));
      });

      it('leaves currency amounts alone', () => {
        const input = 'It costs $5.99 total';
        const output = parseMarkdownToANSI(input);
        expect(output).toBe(primary('It costs $5.99 total'));
      });
    });
  });
});