-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Feature: Automatically create Markdown links when pasting a link on highlighted text #71819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blimpich
merged 12 commits into
Expensify:main
from
marufsharifi:feat/auto-markdown-link-on-paste
Oct 13, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
06197f9
Feature: Automatically create Markdown links when pasting a link on h…
marufsharifi 4f85ae4
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi 8c1d2fd
Add unit test for MarkdownLinkHelper
marufsharifi 1dea2e4
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi 6b8402d
feat(integration-test): add integration test for ReportActionCompose …
marufsharifi 67ab8d7
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi 2fad97d
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi e53062b
extended test cases for ReportActionCompose
marufsharifi 82dc8a0
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi a99daf4
Fix: type, eslint issues
marufsharifi 3d636c0
fix spell check failing
marufsharifi cc4454b
Merge branch 'main' into feat/auto-markdown-link-on-paste
marufsharifi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import type {MutableRefObject} from 'react'; | ||
| import type {RefObject} from 'react'; | ||
| import type {TextInput} from 'react-native'; | ||
|
|
||
| type UseHtmlPaste = ( | ||
| textInputRef: MutableRefObject<(HTMLTextAreaElement & TextInput) | TextInput | null>, | ||
| textInputRef: RefObject<(HTMLTextAreaElement & TextInput) | TextInput | null>, | ||
| preHtmlPasteCallback?: (event: ClipboardEvent) => boolean, | ||
| isActive?: boolean, | ||
| maxLength?: number, // Maximum length of the text input value after pasting | ||
| ) => void; | ||
| ) => void | { | ||
| handlePastePlainText: (event: ClipboardEvent) => void; | ||
| }; | ||
|
|
||
| export default UseHtmlPaste; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import {Str} from 'expensify-common'; | ||
|
|
||
| /** | ||
| * Returns true if `text` is a single token URL (no whitespace), allowing optional angle-bracket wrappers. | ||
| */ | ||
| const isStandaloneURL = (text: string): boolean => { | ||
| if (!text) { | ||
| return false; | ||
| } | ||
| const trimmed = text.trim(); | ||
| if (/\s/.test(trimmed)) { | ||
| return false; | ||
| } | ||
| const unwrapped = trimmed.replace(/^<|>$/g, ''); | ||
|
|
||
| // Reject if contains emoji or any non-ASCII characters | ||
| // (valid URLs per RFC 3986 should be ASCII-only) | ||
| // eslint-disable-next-line no-control-regex | ||
| if (/[^\x00-\x7F]/.test(unwrapped)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Str.isValidURL(unwrapped); | ||
| }; | ||
|
|
||
| /** | ||
| * Collapse newlines to spaces, collapse repeated whitespace to single space, trim, | ||
| * and replace opening [ and closing ] square brackets with HTML codes, which would otherwise break Markdown link text. | ||
| */ | ||
| const escapeLinkText = (text: string): string => { | ||
| if (!text) { | ||
| return ''; | ||
| } | ||
| const collapsed = text | ||
| .replace(/\r?\n+/g, ' ') | ||
| .replace(/\s+/g, ' ') | ||
| .trim(); | ||
| return collapsed.replace(/\[/g, '[').replace(/\]/g, ']'); | ||
| }; | ||
|
|
||
| /** | ||
| * Sanitize the URL for Markdown link. Remove surrounding < > if present and encode it | ||
| * to avoid raw spaces or other invalid characters inside the parentheses. | ||
| * We won't alter semantics otherwise. | ||
| */ | ||
| const sanitizeUrlForMarkdown = (url: string): string => { | ||
| if (!isStandaloneURL(url)) { | ||
| return url; | ||
| } | ||
|
|
||
| const trimmed = (url || '').trim(); | ||
| const unwrapped = trimmed.replace(/^<|>$/g, ''); | ||
|
|
||
| try { | ||
| return encodeURI(unwrapped); | ||
| } catch { | ||
| return unwrapped; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Build a Markdown link: [escaped-selected-text](sanitized-url) | ||
| * We do NOT wrap the URL in < > here — angle brackets are only valid for "auto links", not inside link destinations. | ||
| */ | ||
| const toMarkdownLink = (selectedText: string, url: string): string => { | ||
| const safeText = escapeLinkText(selectedText); | ||
| const safeUrl = sanitizeUrlForMarkdown(url); | ||
| return `[${safeText}](${safeUrl})`; | ||
| }; | ||
|
|
||
| type DetectRewriteResult = {text: string | null; didReplace: boolean}; | ||
|
|
||
| /** | ||
| * Given prevText and a selection (start/end) and the insertedText (e.g. diff), | ||
| * returns a rewritten text if this looks like "selection replaced by a single URL" and we should turn it into a markdown link. | ||
| */ | ||
| const detectAndRewritePaste = (prevText: string, selectionStart: number, selectionEnd: number, insertedText: string): DetectRewriteResult => { | ||
| if (!insertedText || !isStandaloneURL(insertedText)) { | ||
| return {text: null, didReplace: false}; | ||
| } | ||
|
|
||
| const replacedSelectionLength = Math.max(0, selectionEnd - selectionStart); | ||
| if (replacedSelectionLength === 0) { | ||
| // nothing replaced (user pasted URL without selecting text) -> don't rewrite | ||
| return {text: null, didReplace: false}; | ||
| } | ||
|
|
||
| const selectedText = prevText.substring(selectionStart, selectionEnd); | ||
| const replacement = toMarkdownLink(selectedText, insertedText); | ||
| const newText = prevText.slice(0, selectionStart) + replacement + prevText.slice(selectionEnd); | ||
|
|
||
| return {text: newText, didReplace: true}; | ||
| }; | ||
|
|
||
| export {isStandaloneURL, escapeLinkText, sanitizeUrlForMarkdown, toMarkdownLink, detectAndRewritePaste}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn’t add trim(), because it removes whitespace when pasting text. This is what caused issue #74749. So we applied the fix here.