forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {AppBskyRichtextFacet, type RichText} from '@atproto/api'
2
3import {linkRequiresWarning} from './url-helpers'
4
5export function richTextToString(rt: RichText, loose: boolean): string {
6 const {text, facets} = rt
7
8 if (!facets?.length) {
9 return text
10 }
11
12 let result = ''
13
14 for (const segment of rt.segments()) {
15 const link = segment.link
16
17 if (link && AppBskyRichtextFacet.validateLink(link).success) {
18 const href = link.uri
19 const text = segment.text
20
21 const requiresWarning = linkRequiresWarning(href, text)
22
23 result += !requiresWarning ? href : loose ? `[${text}](${href})` : text
24 } else {
25 result += segment.text
26 }
27 }
28
29 return result
30}