|
| 1 | +import { parseTextLinks, type TextSegment } from './parseTextLinks'; |
| 2 | + |
| 3 | +/** Just the link segments. */ |
| 4 | +const links = (text: string) => |
| 5 | + parseTextLinks(text).filter( |
| 6 | + (s): s is Extract<TextSegment, { type: 'link' }> => s.type === 'link', |
| 7 | + ); |
| 8 | + |
| 9 | +/** Display values of the link segments. */ |
| 10 | +const linkValues = (text: string) => links(text).map((l) => l.value); |
| 11 | + |
| 12 | +/** Resolved URLs of the link segments. */ |
| 13 | +const linkUrls = (text: string) => links(text).map((l) => l.url); |
| 14 | + |
| 15 | +describe('parseTextLinks', () => { |
| 16 | + describe('no links', () => { |
| 17 | + it('returns a single text segment for plain text', () => { |
| 18 | + expect(parseTextLinks('just some words')).toEqual([ |
| 19 | + { type: 'text', value: 'just some words' }, |
| 20 | + ]); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns nothing for an empty string', () => { |
| 24 | + expect(parseTextLinks('')).toEqual([]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('does not linkify "node.js" / "e.g." / "file.txt" (unknown TLDs)', () => { |
| 28 | + expect(linkValues('see index.js and file.txt, e.g. nothing')).toEqual([]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not linkify a domain with no TLD', () => { |
| 32 | + expect(linkValues('localhost and foo are not links')).toEqual([]); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('http(s) URLs', () => { |
| 37 | + it('detects an http URL', () => { |
| 38 | + expect(links('go http://example.com now')).toEqual([ |
| 39 | + { |
| 40 | + type: 'link', |
| 41 | + value: 'http://example.com', |
| 42 | + url: 'http://example.com', |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('detects an https URL', () => { |
| 48 | + expect(linkUrls('https://example.com')).toEqual(['https://example.com']); |
| 49 | + }); |
| 50 | + |
| 51 | + it('keeps path, query, and fragment', () => { |
| 52 | + const url = 'https://example.com/a/b?x=1&y=2#frag'; |
| 53 | + expect(linkUrls(url)).toEqual([url]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('keeps a port number', () => { |
| 57 | + expect(linkUrls('http://localhost:3000/x')).toEqual([ |
| 58 | + 'http://localhost:3000/x', |
| 59 | + ]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('handles an uppercase scheme', () => { |
| 63 | + expect(linkUrls('HTTPS://Example.com')).toEqual(['HTTPS://Example.com']); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('www. and bare domains (scheme prepended)', () => { |
| 68 | + it('detects a www. domain and prepends https', () => { |
| 69 | + expect(links('visit www.example.com')).toEqual([ |
| 70 | + { |
| 71 | + type: 'link', |
| 72 | + value: 'www.example.com', |
| 73 | + url: 'https://www.example.com', |
| 74 | + }, |
| 75 | + ]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('detects a bare domain with a known TLD', () => { |
| 79 | + expect(links('go to example.com today')).toEqual([ |
| 80 | + { type: 'link', value: 'example.com', url: 'https://example.com' }, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('detects a bare domain with a path', () => { |
| 85 | + expect(links('example.org/path/to/page')).toEqual([ |
| 86 | + { |
| 87 | + type: 'link', |
| 88 | + value: 'example.org/path/to/page', |
| 89 | + url: 'https://example.org/path/to/page', |
| 90 | + }, |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('detects subdomains', () => { |
| 95 | + expect(linkValues('sub.docs.example.io')).toEqual([ |
| 96 | + 'sub.docs.example.io', |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('detects multi-part TLDs (example.co.uk)', () => { |
| 101 | + expect(linkValues('example.co.uk/about')).toEqual([ |
| 102 | + 'example.co.uk/about', |
| 103 | + ]); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('trailing punctuation', () => { |
| 108 | + it.each([ |
| 109 | + ['period', 'see example.com.', 'example.com', '.'], |
| 110 | + ['comma', 'see example.com, then', 'example.com', ','], |
| 111 | + ['exclamation', 'wow https://example.com!', 'https://example.com', '!'], |
| 112 | + ['question', 'is it example.com?', 'example.com', '?'], |
| 113 | + ['close paren', '(https://example.com)', 'https://example.com', ')'], |
| 114 | + ])('excludes a trailing %s from the link', (_label, input, value) => { |
| 115 | + expect(linkValues(input)).toEqual([value]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('leaves trailing punctuation in the text stream', () => { |
| 119 | + const segs = parseTextLinks('see example.com.'); |
| 120 | + expect(segs).toEqual([ |
| 121 | + { type: 'text', value: 'see ' }, |
| 122 | + { type: 'link', value: 'example.com', url: 'https://example.com' }, |
| 123 | + { type: 'text', value: '.' }, |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('keeps a trailing slash (not punctuation)', () => { |
| 128 | + expect(linkValues('https://example.com/')).toEqual([ |
| 129 | + 'https://example.com/', |
| 130 | + ]); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('emails', () => { |
| 135 | + it('does not linkify an email address', () => { |
| 136 | + expect(linkValues('reach me@example.com please')).toEqual([]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('does not linkify the domain inside an email', () => { |
| 140 | + expect(parseTextLinks('a@b.com')).toEqual([ |
| 141 | + { type: 'text', value: 'a@b.com' }, |
| 142 | + ]); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('multiple links & surrounding text', () => { |
| 147 | + it('detects several links with text between them', () => { |
| 148 | + const segs = parseTextLinks( |
| 149 | + 'a https://x.com b www.y.org c example.net d', |
| 150 | + ); |
| 151 | + expect(segs).toEqual([ |
| 152 | + { type: 'text', value: 'a ' }, |
| 153 | + { type: 'link', value: 'https://x.com', url: 'https://x.com' }, |
| 154 | + { type: 'text', value: ' b ' }, |
| 155 | + { type: 'link', value: 'www.y.org', url: 'https://www.y.org' }, |
| 156 | + { type: 'text', value: ' c ' }, |
| 157 | + { type: 'link', value: 'example.net', url: 'https://example.net' }, |
| 158 | + { type: 'text', value: ' d' }, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('handles a link at the very start and end', () => { |
| 163 | + expect(parseTextLinks('https://a.com')).toEqual([ |
| 164 | + { type: 'link', value: 'https://a.com', url: 'https://a.com' }, |
| 165 | + ]); |
| 166 | + }); |
| 167 | + |
| 168 | + it('preserves newlines around links', () => { |
| 169 | + const segs = parseTextLinks('line1\nhttps://example.com\nline2'); |
| 170 | + expect(segs).toEqual([ |
| 171 | + { type: 'text', value: 'line1\n' }, |
| 172 | + { |
| 173 | + type: 'link', |
| 174 | + value: 'https://example.com', |
| 175 | + url: 'https://example.com', |
| 176 | + }, |
| 177 | + { type: 'text', value: '\nline2' }, |
| 178 | + ]); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('losslessness', () => { |
| 183 | + it.each([ |
| 184 | + 'plain text only', |
| 185 | + 'see https://example.com/path?x=1#y now', |
| 186 | + '(www.example.com), and example.org. done', |
| 187 | + 'email a@b.com plus https://c.io end', |
| 188 | + 'multi https://x.com www.y.org example.net z', |
| 189 | + '', |
| 190 | + ])('rejoining all segment values reproduces the input: %s', (input) => { |
| 191 | + const joined = parseTextLinks(input) |
| 192 | + .map((s) => s.value) |
| 193 | + .join(''); |
| 194 | + expect(joined).toBe(input); |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments