diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-11-18 13:59:17 -0600 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-11-18 13:59:17 -0600 |
commit | 6e2b7a0b90ae8140d81d2d66c87ec9e40d9f45ec (patch) | |
tree | f36f902da70006364e839ee53d55a468c7539e21 /src/view/lib/strings.ts | |
parent | 9d13e05dbf0bb6430060ddf556006b1721444edc (diff) | |
download | voidsky-6e2b7a0b90ae8140d81d2d66c87ec9e40d9f45ec.tar.zst |
Add support for web links
Diffstat (limited to 'src/view/lib/strings.ts')
-rw-r--r-- | src/view/lib/strings.ts | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/src/view/lib/strings.ts b/src/view/lib/strings.ts index 6d963b281..b12fbd398 100644 --- a/src/view/lib/strings.ts +++ b/src/view/lib/strings.ts @@ -63,19 +63,36 @@ export function extractEntities( ): Entity[] | undefined { let match let ents: Entity[] = [] - const re = /(^|\s)(@)([a-zA-Z0-9\.-]+)(\b)/dg - while ((match = re.exec(text))) { - if (knownHandles && !knownHandles.has(match[3])) { - continue // not a known handle + { + // mentions + const re = /(^|\s)(@)([a-zA-Z0-9\.-]+)(\b)/dg + while ((match = re.exec(text))) { + if (knownHandles && !knownHandles.has(match[3])) { + continue // not a known handle + } + ents.push({ + type: 'mention', + value: match[3], + index: { + start: match.indices[2][0], // skip the (^|\s) but include the '@' + end: match.indices[3][1], + }, + }) + } + } + { + // links + const re = /(^|\s)(https?:\/\/[\S]+)(\b)/dg + while ((match = re.exec(text))) { + ents.push({ + type: 'link', + value: match[2], + index: { + start: match.indices[1][0], // skip the (^|\s) but include the '@' + end: match.indices[2][1], + }, + }) } - ents.push({ - type: 'mention', - value: match[3], - index: { - start: match.indices[2][0], // skip the (^|\s) but include the '@' - end: match.indices[3][1], - }, - }) } return ents.length > 0 ? ents : undefined } |