|
1 | 1 | import { describe, expect, it } from 'vitest' |
| 2 | +import * as fc from 'fast-check' |
2 | 3 | import { |
3 | 4 | escapeHtml, |
4 | 5 | parseJsDocLinks, |
@@ -40,6 +41,15 @@ describe('stripAnsi', () => { |
40 | 41 | const input = `object is ReactElement${ESC}[0m${ESC}[38;5;12m<${ESC}[0mP${ESC}[38;5;12m>${ESC}[0m` |
41 | 42 | expect(stripAnsi(input)).toBe('object is ReactElement<P>') |
42 | 43 | }) |
| 44 | + |
| 45 | + it('should strip everything in one pass', () => { |
| 46 | + fc.assert( |
| 47 | + fc.property(fc.string(), input => { |
| 48 | + const stripped = stripAnsi(input) |
| 49 | + expect(stripAnsi(stripped)).toBe(stripped) |
| 50 | + }), |
| 51 | + ) |
| 52 | + }) |
43 | 53 | }) |
44 | 54 |
|
45 | 55 | describe('escapeHtml', () => { |
@@ -124,6 +134,65 @@ describe('parseJsDocLinks', () => { |
124 | 134 | const result = parseJsDocLinks('{@link http://example.com}', emptyLookup) |
125 | 135 | expect(result).toContain('href="http://example.com"') |
126 | 136 | }) |
| 137 | + |
| 138 | + it('should convert external URLs using {@link url} to links', () => { |
| 139 | + fc.assert( |
| 140 | + fc.property(fc.webUrl(), url => { |
| 141 | + const result = parseJsDocLinks(`{@link ${url}}`, emptyLookup) |
| 142 | + expect(result).toContain(`href="${url.replaceAll('"', '\\"')}"`) |
| 143 | + expect(result).toContain('target="_blank"') |
| 144 | + expect(result).toContain('rel="noreferrer"') |
| 145 | + expect(result).toContain(escapeHtml(url)) |
| 146 | + }), |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + it('should convert external URLs using {@link url text} to links', () => { |
| 151 | + fc.assert( |
| 152 | + fc.property(fc.webUrl(), fc.stringMatching(/^[^}\s][^}]+[^}\s]$/), (url, text) => { |
| 153 | + const result = parseJsDocLinks(`{@link ${url} ${text}}`, emptyLookup) |
| 154 | + expect(result).toContain(`href="${url.replaceAll('"', '\\"')}"`) |
| 155 | + expect(result).toContain('target="_blank"') |
| 156 | + expect(result).toContain('rel="noreferrer"') |
| 157 | + expect(result).toContain(escapeHtml(text)) |
| 158 | + }), |
| 159 | + ) |
| 160 | + }) |
| 161 | + |
| 162 | + it('should be able to treat correctly several external URLs at the middle of a text', () => { |
| 163 | + const surrounding = fc.stringMatching(/^[^{]*$/) |
| 164 | + const link = fc.record({ |
| 165 | + url: fc.webUrl(), |
| 166 | + label: fc.option(fc.stringMatching(/^[^}\s][^}]+[^}\s]$/)), |
| 167 | + before: surrounding, |
| 168 | + after: surrounding, |
| 169 | + }) |
| 170 | + fc.assert( |
| 171 | + fc.property(fc.array(link, { minLength: 1 }), content => { |
| 172 | + let docString = '' |
| 173 | + const expectedUrls = [] |
| 174 | + for (const chunk of content) { |
| 175 | + if (chunk.before.length !== 0 || docString.length !== 0) { |
| 176 | + docString += `${chunk.before} ` |
| 177 | + } |
| 178 | + if (chunk.label === null) { |
| 179 | + docString += `{@link ${chunk.url}}` |
| 180 | + expectedUrls.push(chunk.url) |
| 181 | + } else { |
| 182 | + docString += `{@link ${chunk.url} ${chunk.label}}` |
| 183 | + expectedUrls.push(chunk.url) |
| 184 | + } |
| 185 | + if (chunk.after.length !== 0) { |
| 186 | + docString += ` ${chunk.after}` |
| 187 | + } |
| 188 | + } |
| 189 | + const result = parseJsDocLinks(docString, emptyLookup) |
| 190 | + for (const url of expectedUrls) { |
| 191 | + expect(result).toContain(`href="${url.replaceAll('"', '\\"')}"`) |
| 192 | + } |
| 193 | + }), |
| 194 | + ) |
| 195 | + }) |
127 | 196 | }) |
128 | 197 |
|
129 | 198 | describe('renderMarkdown', () => { |
|
0 commit comments