diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-10-04 10:15:35 -0500 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-10-04 10:15:35 -0500 |
commit | 0296e8411e528ae1c39a5d8231ba2ec89fa2633e (patch) | |
tree | 476b1b963eeb957644175c9f1522330f19e87bd3 /src/view/lib/strings.ts | |
parent | 195d2f7d2bd193108938901c3f757a9e89080b63 (diff) | |
download | voidsky-0296e8411e528ae1c39a5d8231ba2ec89fa2633e.tar.zst |
Fixes to entity extraction
Diffstat (limited to 'src/view/lib/strings.ts')
-rw-r--r-- | src/view/lib/strings.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/view/lib/strings.ts b/src/view/lib/strings.ts index d468a18ac..eecec890c 100644 --- a/src/view/lib/strings.ts +++ b/src/view/lib/strings.ts @@ -1,4 +1,7 @@ import {AdxUri} from '../../third-party/uri' +import {Entity as Entities} from '../../third-party/api/src/types/todo/social/post' + +type Entity = Entities[0] export function pluralize(n: number, base: string, plural?: string): string { if (n === 1) { @@ -53,3 +56,20 @@ export function ago(date: number | string | Date): string { return new Date(ts).toLocaleDateString() } } + +export function extractEntities(text: string): Entity[] | undefined { + let match + let ents: Entity[] = [] + const re = /(^|\s)(@)([a-zA-Z0-9\.-]+)(\b)/dg + while ((match = re.exec(text))) { + ents.push({ + type: 'mention', + value: match[3], + index: [ + match.indices[2][0], // skip the (^|\s) but include the '@' + match.indices[3][1], + ], + }) + } + return ents.length > 0 ? ents : undefined +} |