From 5e2a3bebaca1cece858e5c54149a6ecc1e5f351f Mon Sep 17 00:00:00 2001 From: scanash00 Date: Mon, 1 Dec 2025 18:13:02 -0900 Subject: [PATCH] fix redrafting for the 39482398th time - Fixed truncated links - Fixed multiple line breaks replaced with spaces - Fixed HTML tags stripped on web --- .../PostControls/PostMenu/PostMenuItems.tsx | 3 +- src/lib/api/index.ts | 6 ++-- src/lib/strings/rich-text-manip.ts | 32 +++++++++++++++++++ .../com/composer/text-input/TextInput.web.tsx | 10 +++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/components/PostControls/PostMenu/PostMenuItems.tsx b/src/components/PostControls/PostMenu/PostMenuItems.tsx index 28f62d034..970969dfb 100644 --- a/src/components/PostControls/PostMenu/PostMenuItems.tsx +++ b/src/components/PostControls/PostMenu/PostMenuItems.tsx @@ -37,6 +37,7 @@ import { } from '#/lib/routes/types' import {logEvent, useGate} from '#/lib/statsig/statsig' import {richTextToString} from '#/lib/strings/rich-text-helpers' +import {restoreLinks} from '#/lib/strings/rich-text-manip' import {toShareUrl} from '#/lib/strings/url-helpers' import {logger} from '#/logger' import {isWeb} from '#/platform/detection' @@ -350,7 +351,7 @@ let PostMenuItems = ({ } openComposer({ - text: record.text, + text: restoreLinks(record.text, record.facets), imageUris, videoUri, onPost: () => { diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 2eda1a54e..02ad4395d 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -203,7 +203,7 @@ async function resolveRT(agent: BskyAgent, richtext: RichText) { const {text: parsedText, facets: markdownFacets} = parseMarkdownLinks(trimmedText) - let rt = new RichText({text: parsedText}, {cleanNewlines: true}) + let rt = new RichText({text: parsedText}) await rt.detectFacets(agent) if (markdownFacets.length > 0) { @@ -368,11 +368,11 @@ async function resolveMedia( ) const width = Math.round( - videoDraft.asset?.width || + videoDraft.asset?.width || ('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.width : 1000) ) const height = Math.round( - videoDraft.asset?.height || + videoDraft.asset?.height || ('redraftDimensions' in videoDraft ? videoDraft.redraftDimensions.height : 1000) ) diff --git a/src/lib/strings/rich-text-manip.ts b/src/lib/strings/rich-text-manip.ts index a1afdc4f1..cc2f2a4c0 100644 --- a/src/lib/strings/rich-text-manip.ts +++ b/src/lib/strings/rich-text-manip.ts @@ -2,6 +2,38 @@ import {AppBskyRichtextFacet,type RichText,UnicodeString} from '@atproto/api' import {toShortUrl} from './url-helpers' +export function restoreLinks( + text: string, + facets?: AppBskyRichtextFacet.Main[], +): string { + if (!facets?.length) { + return text + } + + const rt = new UnicodeString(text) + const parts: string[] = [] + let lastIndex = 0 + + const sortedFacets = [...facets].sort( + (a, b) => a.index.byteStart - b.index.byteStart, + ) + + for (const facet of sortedFacets) { + const isLink = facet.features.find(AppBskyRichtextFacet.isLink) + if (!isLink) { + continue + } + + parts.push(rt.slice(lastIndex, facet.index.byteStart)) + parts.push(isLink.uri) + lastIndex = facet.index.byteEnd + } + + parts.push(rt.slice(lastIndex)) + + return parts.join('') +} + export function shortenLinks(rt: RichText): RichText { if (!rt.facets?.length) { return rt diff --git a/src/view/com/composer/text-input/TextInput.web.tsx b/src/view/com/composer/text-input/TextInput.web.tsx index 0df2d960c..092df738f 100644 --- a/src/view/com/composer/text-input/TextInput.web.tsx +++ b/src/view/com/composer/text-input/TextInput.web.tsx @@ -241,7 +241,7 @@ export function TextInput({ } }, }, - content: generateJSON(richtext.text.toString(), extensions, { + content: generateJSON(textToHtml(richtext.text.toString()), extensions, { preserveWhitespace: 'full', }), autofocus: 'end', @@ -503,6 +503,14 @@ const styles = StyleSheet.create({ }, }) +function textToHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/\n/g, '
') +} + function getImageOrVideoFromUri( items: DataTransferItemList, callback: (uri: string) => void, -- 2.50.1 (Apple Git-155)