about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorMary <148872143+mary-ext@users.noreply.github.com>2024-01-11 13:56:45 +0700
committerGitHub <noreply@github.com>2024-01-10 22:56:45 -0800
commit0b2daa787cc6ee7c9fe597aee8ccf9168bada7e7 (patch)
tree42c9e0b747326ee2187cc9b484f9051f5b96dc57 /src/lib
parentf7b01c3542f701930764c57b7b2f6bff54a9b675 (diff)
downloadvoidsky-0b2daa787cc6ee7c9fe597aee8ccf9168bada7e7.tar.zst
Use the RichText facets when copying post text (#2481)
* feat: serialize rich text to string

* feat: wire richTextToString to copy post text
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/strings/rich-text-helpers.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/lib/strings/rich-text-helpers.ts b/src/lib/strings/rich-text-helpers.ts
new file mode 100644
index 000000000..08971ca03
--- /dev/null
+++ b/src/lib/strings/rich-text-helpers.ts
@@ -0,0 +1,29 @@
+import {AppBskyRichtextFacet, RichText} from '@atproto/api'
+import {linkRequiresWarning} from './url-helpers'
+
+export function richTextToString(rt: RichText): string {
+  const {text, facets} = rt
+
+  if (!facets?.length) {
+    return text
+  }
+
+  let result = ''
+
+  for (const segment of rt.segments()) {
+    const link = segment.link
+
+    if (link && AppBskyRichtextFacet.validateLink(link).success) {
+      const href = link.uri
+      const text = segment.text
+
+      const requiresWarning = linkRequiresWarning(href, text)
+
+      result += !requiresWarning ? href : `[${text}](${href})`
+    } else {
+      result += segment.text
+    }
+  }
+
+  return result
+}