|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { renderTextWithLinks } from './links'; |
| 3 | + |
| 4 | +describe('renderTextWithLinks', () => { |
| 5 | + it('returns plain text unchanged (escaped)', () => { |
| 6 | + expect(renderTextWithLinks('Hello world')).toBe('Hello world'); |
| 7 | + }); |
| 8 | + |
| 9 | + it('escapes HTML in plain text', () => { |
| 10 | + expect(renderTextWithLinks('<b>bold</b> & "quotes"')).toBe( |
| 11 | + '<b>bold</b> & "quotes"' |
| 12 | + ); |
| 13 | + }); |
| 14 | + |
| 15 | + it('converts an https link and opens it in a new tab', () => { |
| 16 | + expect(renderTextWithLinks('See [the docs](https://example.com/docs) for more')).toBe( |
| 17 | + 'See <a href="https://example.com/docs" target="_blank" rel="noopener noreferrer">the docs</a> for more' |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('converts a site-relative link without target=_blank', () => { |
| 22 | + expect(renderTextWithLinks('Go to [registration](/register)')).toBe( |
| 23 | + 'Go to <a href="/register">registration</a>' |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it('converts a mailto link without target=_blank', () => { |
| 28 | + expect(renderTextWithLinks('[Contact us](mailto:hello@example.com)')).toBe( |
| 29 | + '<a href="mailto:hello@example.com">Contact us</a>' |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('handles multiple links in one string', () => { |
| 34 | + expect(renderTextWithLinks('[a](/x) and [b](/y)')).toBe( |
| 35 | + '<a href="/x">a</a> and <a href="/y">b</a>' |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('applies the linkClass option', () => { |
| 40 | + expect(renderTextWithLinks('[docs](/docs)', { linkClass: 'underline' })).toBe( |
| 41 | + '<a href="/docs" class="underline">docs</a>' |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('leaves javascript: URLs as escaped literal text', () => { |
| 46 | + expect(renderTextWithLinks('[click](javascript:alert(1))')).toBe( |
| 47 | + '[click](javascript:alert(1))' |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('leaves protocol-relative URLs as literal text', () => { |
| 52 | + expect(renderTextWithLinks('[click](//evil.example)')).toBe('[click](//evil.example)'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('escapes HTML inside the link label and URL', () => { |
| 56 | + expect(renderTextWithLinks('["quoted" <label>](/path?a=1&b=2)')).toBe( |
| 57 | + '<a href="/path?a=1&b=2">"quoted" <label></a>' |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not treat bare brackets as a link', () => { |
| 62 | + expect(renderTextWithLinks('array[0] and (note)')).toBe('array[0] and (note)'); |
| 63 | + }); |
| 64 | +}); |
0 commit comments