diff options
author | Eric Bailey <git@esb.lol> | 2024-03-01 15:50:14 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-01 15:50:14 -0600 |
commit | e11bd4385d1f39becdc18462f400e89bcc5cd75e (patch) | |
tree | 0b4c1d5537a282df427dcaab1d3179f8578c86be /src | |
parent | f8aa5fd0050d3bd6a5e932b0b9d202597cb815e3 (diff) | |
download | voidsky-e11bd4385d1f39becdc18462f400e89bcc5cd75e.tar.zst |
Integrate new tag detection and regexes (#3063)
* Integrate new tag detection and regexes * Bump SDK * Can use const * Clarify intent * Enhance clarity
Diffstat (limited to 'src')
-rw-r--r-- | src/view/com/composer/text-input/web/LinkDecorator.ts | 5 | ||||
-rw-r--r-- | src/view/com/composer/text-input/web/TagDecorator.ts | 22 |
2 files changed, 18 insertions, 9 deletions
diff --git a/src/view/com/composer/text-input/web/LinkDecorator.ts b/src/view/com/composer/text-input/web/LinkDecorator.ts index 19945de08..e36ac80e4 100644 --- a/src/view/com/composer/text-input/web/LinkDecorator.ts +++ b/src/view/com/composer/text-input/web/LinkDecorator.ts @@ -18,6 +18,8 @@ import {Mark} from '@tiptap/core' import {Plugin, PluginKey} from '@tiptap/pm/state' import {Node as ProsemirrorNode} from '@tiptap/pm/model' import {Decoration, DecorationSet} from '@tiptap/pm/view' +import {URL_REGEX} from '@atproto/api' + import {isValidDomain} from 'lib/strings/url-helpers' export const LinkDecorator = Mark.create({ @@ -78,8 +80,7 @@ function linkDecorator() { function iterateUris(str: string, cb: (from: number, to: number) => void) { let match - const re = - /(^|\s|\()((https?:\/\/[\S]+)|((?<domain>[a-z][a-z0-9]*(\.[a-z0-9]+)+)[\S]*))/gim + const re = URL_REGEX while ((match = re.exec(str))) { let uri = match[2] if (!uri.startsWith('http')) { diff --git a/src/view/com/composer/text-input/web/TagDecorator.ts b/src/view/com/composer/text-input/web/TagDecorator.ts index d820ec3f0..2bf3184a8 100644 --- a/src/view/com/composer/text-input/web/TagDecorator.ts +++ b/src/view/com/composer/text-input/web/TagDecorator.ts @@ -18,28 +18,36 @@ import {Mark} from '@tiptap/core' import {Plugin, PluginKey} from '@tiptap/pm/state' import {Node as ProsemirrorNode} from '@tiptap/pm/model' import {Decoration, DecorationSet} from '@tiptap/pm/view' +import {TAG_REGEX, TRAILING_PUNCTUATION_REGEX} from '@atproto/api' function getDecorations(doc: ProsemirrorNode) { const decorations: Decoration[] = [] doc.descendants((node, pos) => { if (node.isText && node.text) { - const regex = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g + const regex = TAG_REGEX const textContent = node.textContent let match while ((match = regex.exec(textContent))) { - const [matchedString, tag] = match + const [matchedString, _, tag] = match - if (tag.length > 66) continue + if (!tag || tag.replace(TRAILING_PUNCTUATION_REGEX, '').length > 64) + continue - const [trailingPunc = ''] = tag.match(/\p{P}+$/u) || [] + const [trailingPunc = ''] = tag.match(TRAILING_PUNCTUATION_REGEX) || [] + const matchedFrom = match.index + matchedString.indexOf(tag) + const matchedTo = matchedFrom + (tag.length - trailingPunc.length) - const from = match.index + matchedString.indexOf(tag) - const to = from + (tag.length - trailingPunc.length) + /* + * The match is exclusive of `#` so we need to adjust the start of the + * highlight by -1 to include the `#` + */ + const start = pos + matchedFrom - 1 + const end = pos + matchedTo decorations.push( - Decoration.inline(pos + from, pos + to, { + Decoration.inline(start, end, { class: 'autolink', }), ) |