-
Notifications
You must be signed in to change notification settings - Fork 3.9k
skip to replace link text which is a link #17451
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
amyevans
merged 14 commits into
Expensify:main
from
eh2077:skip-to-replace-link-text-which-is-a-link
Apr 18, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b2327f0
skip to replace link text which is a link
eh2077 0f46d46
fix link text replacement
eh2077 ebdd11a
remove unused methods and fix test cases
eh2077 d3e6ad8
improve test
eh2077 a6d09a9
fix lint
eh2077 6238868
update expensify-common
eh2077 979d093
add comment for removeLinksFromHtml
eh2077 94a4003
Update src/libs/actions/Report.js
eh2077 5398e02
Update src/libs/actions/Report.js
eh2077 9c4eee2
Update tests/actions/ReportTest.js
eh2077 565cc9d
Update tests/actions/ReportTest.js
eh2077 291ee4a
Update tests/actions/ReportTest.js
eh2077 34c72f9
Update tests/actions/ReportTest.js
eh2077 a033979
fix import order
eh2077 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -823,42 +823,42 @@ const getRemovedMarkdownLinks = (oldComment, newComment) => { | |
| }; | ||
|
|
||
| /** | ||
| * Removes the links in a markdown comment. | ||
| * Removes the links in html of a comment. | ||
| * example: | ||
| * comment="test [link](https://www.google.com) test", | ||
| * html="test <a href="https://www.google.com" target="_blank" rel="noreferrer noopener">https://www.google.com</a> test" | ||
| * links=["https://www.google.com"] | ||
| * returns: "test link test" | ||
| * @param {String} comment | ||
| * returns: "test https://www.google.com test" | ||
| * | ||
| * @param {String} html | ||
| * @param {Array} links | ||
| * @returns {String} | ||
| */ | ||
| const removeLinks = (comment, links) => { | ||
| let commentCopy = comment.slice(); | ||
| const removeLinksFromHtml = (html, links) => { | ||
| let htmlCopy = html.slice(); | ||
| _.forEach(links, (link) => { | ||
| const regex = new RegExp(`\\[([^\\[\\]]*)\\]\\(${link}\\)`, 'gm'); | ||
| const linkMatch = regex.exec(commentCopy); | ||
| const linkText = linkMatch && linkMatch[1]; | ||
| commentCopy = commentCopy.replace(`[${linkText}](${link})`, linkText); | ||
| // We want to match the anchor tag of the link and replace the whole anchor tag with the text of the anchor tag | ||
| const regex = new RegExp(`<(a)[^><]*href\\s*=\\s*(['"])(${Str.escapeForRegExp(link)})\\2(?:".*?"|'.*?'|[^'"><])*>([\\s\\S]*?)<\\/\\1>(?![^<]*(<\\/pre>|<\\/code>))`, 'gi'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This caused the following bug: We should not have ignored the case flag. |
||
| htmlCopy = htmlCopy.replace(regex, '$4'); | ||
| }); | ||
| return commentCopy; | ||
| return htmlCopy; | ||
| }; | ||
|
|
||
| /** | ||
| * This function will handle removing only links that were purposely removed by the user while editing. | ||
| * | ||
| * @param {String} newCommentText text of the comment after editing. | ||
| * @param {Array} originalHtml original html of the comment before editing | ||
| * @param {String} originalHtml original html of the comment before editing. | ||
| * @returns {String} | ||
| */ | ||
| const handleUserDeletedLinks = (newCommentText, originalHtml) => { | ||
| const handleUserDeletedLinksInHtml = (newCommentText, originalHtml) => { | ||
| const parser = new ExpensiMark(); | ||
| if (newCommentText.length >= CONST.MAX_MARKUP_LENGTH) { | ||
| return newCommentText; | ||
| } | ||
| const htmlWithAutoLinks = parser.replace(newCommentText); | ||
| const markdownWithAutoLinks = parser.htmlToMarkdown(htmlWithAutoLinks).trim(); | ||
| const markdownOriginalComment = parser.htmlToMarkdown(originalHtml).trim(); | ||
| const htmlForNewComment = parser.replace(newCommentText); | ||
| const removedLinks = getRemovedMarkdownLinks(markdownOriginalComment, newCommentText); | ||
| return removeLinks(markdownWithAutoLinks, removedLinks); | ||
| return removeLinksFromHtml(htmlForNewComment, removedLinks); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -875,16 +875,13 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { | |
| // https://github.com/Expensify/App/issues/9090 | ||
| // https://github.com/Expensify/App/issues/13221 | ||
| const originalCommentHTML = lodashGet(originalReportAction, 'message[0].html'); | ||
| const markdownForNewComment = handleUserDeletedLinks(textForNewComment, originalCommentHTML); | ||
|
|
||
| const autolinkFilter = {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}; | ||
| const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentHTML); | ||
|
|
||
| // For comments shorter than 10k chars, convert the comment from MD into HTML because that's how it is stored in the database | ||
| // For longer comments, skip parsing and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!! | ||
| let htmlForNewComment = markdownForNewComment; | ||
| let parsedOriginalCommentHTML = originalCommentHTML; | ||
| if (markdownForNewComment.length < CONST.MAX_MARKUP_LENGTH) { | ||
| htmlForNewComment = parser.replace(markdownForNewComment, autolinkFilter); | ||
| if (textForNewComment.length < CONST.MAX_MARKUP_LENGTH) { | ||
| const autolinkFilter = {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}; | ||
| parsedOriginalCommentHTML = parser.replace(parser.htmlToMarkdown(originalCommentHTML).trim(), autolinkFilter); | ||
| } | ||
|
|
||
|
|
@@ -909,7 +906,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { | |
| ...originalMessage, | ||
| isEdited: true, | ||
| html: htmlForNewComment, | ||
| text: markdownForNewComment, | ||
| text: textForNewComment, | ||
| }], | ||
| }, | ||
| }; | ||
|
|
@@ -1476,7 +1473,7 @@ export { | |
| broadcastUserIsTyping, | ||
| togglePinnedState, | ||
| editReportComment, | ||
| handleUserDeletedLinks, | ||
| handleUserDeletedLinksInHtml, | ||
| saveReportActionDraft, | ||
| saveReportActionDraftNumberOfLines, | ||
| deleteReportComment, | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@eh2077 Can we add a comment here explaining the logic behind it.
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.
@fedirjh Updated