Skip to content

Commit

Permalink
Merge pull request #540 from s-alves10/fix/issue-18911
Browse files Browse the repository at this point in the history
fix: export link regex for use in edit message
  • Loading branch information
neil-marcellini committed May 23, 2023
2 parents 77b3790 + 7312a08 commit 68abe48
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Str from './str';
import {MARKDOWN_URL_REGEX, LOOSE_URL_REGEX, URL_REGEX} from './Url';
import {CONST} from './CONST';

const MARKDOWN_LINK_REGEX = new RegExp(`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`, 'gi');

const SLACK_SPAN_NEW_LINE_TAG = '<span class="c-mrkdwn__br" data-stringify-type="paragraph-break" style="box-sizing: inherit; display: block; height: unset;"></span>';

export default class ExpensiMark {
Expand Down Expand Up @@ -72,13 +74,7 @@ export default class ExpensiMark {
*/
{
name: 'link',
process: (textToProcess, replacement) => {
const regex = new RegExp(
`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`,
'gi',
);
return this.modifyTextForUrlLinks(regex, textToProcess, replacement);
},
process: (textToProcess, replacement) => this.modifyTextForUrlLinks(MARKDOWN_LINK_REGEX, textToProcess, replacement),

replacement: (match, g1, g2) => {
if (g1.match(CONST.REG_EXP.EMOJIS) || !g1.trim()) {
Expand Down Expand Up @@ -722,4 +718,30 @@ export default class ExpensiMark {
// If there are any tags left in the stack, they're unclosed
return tagStack.length !== 0;
}

/**
* @param {String} comment
* @returns {Array}
*/
extractLinksInMarkdownComment(comment) {
const escapedComment = _.escape(comment);
const matches = [...escapedComment.matchAll(MARKDOWN_LINK_REGEX)];

// Element 1 from match is the regex group if it exists which contains the link URLs
const links = _.map(matches, match => Str.sanitizeURL(match[2]));
return links;
}

/**
* Compares two markdown comments and returns a list of the links removed in a new comment.
*
* @param {String} oldComment
* @param {String} newComment
* @returns {Array}
*/
getRemovedMarkdownLinks(oldComment, newComment) {
const linksInOld = this.extractLinksInMarkdownComment(oldComment);
const linksInNew = this.extractLinksInMarkdownComment(newComment);
return _.difference(linksInOld, linksInNew);
}
}

0 comments on commit 68abe48

Please sign in to comment.