Add support for links with no scheme in composer

This commit is contained in:
Paul Frazee 2022-11-22 14:30:35 -06:00
parent 1df48d4dad
commit e488cf8f44
4 changed files with 216 additions and 46 deletions

View file

@ -20,6 +20,7 @@ import {useStores} from '../../../state'
import * as apilib from '../../../state/lib/api'
import {ComposerOpts} from '../../../state/models/shell-ui'
import {s, colors, gradients} from '../../lib/styles'
import {detectLinkables} from '../../../lib/strings'
const MAX_TEXT_LENGTH = 256
const WARNING_TEXT_LENGTH = 200
@ -108,24 +109,18 @@ export const ComposePost = observer(function ComposePost({
: undefined
const textDecorated = useMemo(() => {
const re = /(@[a-z0-9\.]*)|(https?:\/\/[\S]+)/gi
const segments = []
let match
let start = 0
let i = 0
while ((match = re.exec(text))) {
segments.push(text.slice(start, match.index))
segments.push(
<Text key={i++} style={{color: colors.blue3}}>
{match[0]}
</Text>,
)
start = match.index + match[0].length
}
if (start < text.length) {
segments.push(text.slice(start))
}
return segments
return detectLinkables(text).map(v => {
if (typeof v === 'string') {
return v
} else {
return (
<Text key={i++} style={{color: colors.blue3}}>
{v.link}
</Text>
)
}
})
}, [text])
return (

View file

@ -77,7 +77,9 @@ function* toSegments(text: string, entities: Entity[]) {
let subtext = text.slice(currEnt.index.start, currEnt.index.end)
if (
!subtext.trim() ||
stripUsername(subtext) !== stripUsername(currEnt.value)
(currEnt.type === 'mention' &&
stripUsername(subtext) !== stripUsername(currEnt.value)) ||
(currEnt.type === 'link' && !isSameLink(subtext, currEnt.value))
) {
// dont yield links to empty strings or strings that don't match the entity value
yield subtext
@ -99,3 +101,9 @@ function* toSegments(text: string, entities: Entity[]) {
function stripUsername(v: string): string {
return v.trim().replace('@', '')
}
function isSameLink(a: string, b: string) {
a = a.startsWith('http') ? a : `https://${a}`
b = b.startsWith('http') ? b : `https://${b}`
return a === b
}