Integrate new tag detection and regexes (#3063)
* Integrate new tag detection and regexes * Bump SDK * Can use const * Clarify intent * Enhance clarityzio/stable
parent
f8aa5fd005
commit
e11bd4385d
|
@ -44,7 +44,7 @@
|
||||||
"update-extensions": "scripts/updateExtensions.sh"
|
"update-extensions": "scripts/updateExtensions.sh"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@atproto/api": "^0.10.3",
|
"@atproto/api": "^0.10.4",
|
||||||
"@bam.tech/react-native-image-resizer": "^3.0.4",
|
"@bam.tech/react-native-image-resizer": "^3.0.4",
|
||||||
"@braintree/sanitize-url": "^6.0.2",
|
"@braintree/sanitize-url": "^6.0.2",
|
||||||
"@emoji-mart/react": "^1.1.1",
|
"@emoji-mart/react": "^1.1.1",
|
||||||
|
|
|
@ -18,6 +18,8 @@ import {Mark} from '@tiptap/core'
|
||||||
import {Plugin, PluginKey} from '@tiptap/pm/state'
|
import {Plugin, PluginKey} from '@tiptap/pm/state'
|
||||||
import {Node as ProsemirrorNode} from '@tiptap/pm/model'
|
import {Node as ProsemirrorNode} from '@tiptap/pm/model'
|
||||||
import {Decoration, DecorationSet} from '@tiptap/pm/view'
|
import {Decoration, DecorationSet} from '@tiptap/pm/view'
|
||||||
|
import {URL_REGEX} from '@atproto/api'
|
||||||
|
|
||||||
import {isValidDomain} from 'lib/strings/url-helpers'
|
import {isValidDomain} from 'lib/strings/url-helpers'
|
||||||
|
|
||||||
export const LinkDecorator = Mark.create({
|
export const LinkDecorator = Mark.create({
|
||||||
|
@ -78,8 +80,7 @@ function linkDecorator() {
|
||||||
|
|
||||||
function iterateUris(str: string, cb: (from: number, to: number) => void) {
|
function iterateUris(str: string, cb: (from: number, to: number) => void) {
|
||||||
let match
|
let match
|
||||||
const re =
|
const re = URL_REGEX
|
||||||
/(^|\s|\()((https?:\/\/[\S]+)|((?<domain>[a-z][a-z0-9]*(\.[a-z0-9]+)+)[\S]*))/gim
|
|
||||||
while ((match = re.exec(str))) {
|
while ((match = re.exec(str))) {
|
||||||
let uri = match[2]
|
let uri = match[2]
|
||||||
if (!uri.startsWith('http')) {
|
if (!uri.startsWith('http')) {
|
||||||
|
|
|
@ -18,28 +18,36 @@ import {Mark} from '@tiptap/core'
|
||||||
import {Plugin, PluginKey} from '@tiptap/pm/state'
|
import {Plugin, PluginKey} from '@tiptap/pm/state'
|
||||||
import {Node as ProsemirrorNode} from '@tiptap/pm/model'
|
import {Node as ProsemirrorNode} from '@tiptap/pm/model'
|
||||||
import {Decoration, DecorationSet} from '@tiptap/pm/view'
|
import {Decoration, DecorationSet} from '@tiptap/pm/view'
|
||||||
|
import {TAG_REGEX, TRAILING_PUNCTUATION_REGEX} from '@atproto/api'
|
||||||
|
|
||||||
function getDecorations(doc: ProsemirrorNode) {
|
function getDecorations(doc: ProsemirrorNode) {
|
||||||
const decorations: Decoration[] = []
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
doc.descendants((node, pos) => {
|
doc.descendants((node, pos) => {
|
||||||
if (node.isText && node.text) {
|
if (node.isText && node.text) {
|
||||||
const regex = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g
|
const regex = TAG_REGEX
|
||||||
const textContent = node.textContent
|
const textContent = node.textContent
|
||||||
|
|
||||||
let match
|
let match
|
||||||
while ((match = regex.exec(textContent))) {
|
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(
|
decorations.push(
|
||||||
Decoration.inline(pos + from, pos + to, {
|
Decoration.inline(start, end, {
|
||||||
class: 'autolink',
|
class: 'autolink',
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
|
@ -34,10 +34,10 @@
|
||||||
jsonpointer "^5.0.0"
|
jsonpointer "^5.0.0"
|
||||||
leven "^3.1.0"
|
leven "^3.1.0"
|
||||||
|
|
||||||
"@atproto/api@^0.10.3":
|
"@atproto/api@^0.10.4":
|
||||||
version "0.10.3"
|
version "0.10.4"
|
||||||
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.10.3.tgz#cdef37a23b53e2e84840e527992482b0a0ab9d47"
|
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.10.4.tgz#b73446f2344783c42c6040082756449443f15750"
|
||||||
integrity sha512-eNP/6YLor48SUD38Jn5C7xocQL9XzW+BbYat2whWerKsFMn6Kfkk5O3fW1pvcc6NKtKVTo7/4ixMAS+dG2o/Yg==
|
integrity sha512-9gwZt4v4pngfD4mgsET9i9Ym0PpMSzftTzqBjCbFpObx15zMkFemYnLUnyT/NEww2u/aRxjAe2TeBnU0dIbbuQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@atproto/common-web" "^0.2.3"
|
"@atproto/common-web" "^0.2.3"
|
||||||
"@atproto/lexicon" "^0.3.2"
|
"@atproto/lexicon" "^0.3.2"
|
||||||
|
|
Loading…
Reference in New Issue