Skip to content
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

fix: export link regex for use in edit message #540

Merged
merged 7 commits into from
May 23, 2023
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Add a 'P' to the end of the name so we know it's not just a string MARKDOWN_LINK_REGEXP. Idk, maybe too subtle.


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) {
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
const linksInOld = this.extractLinksInMarkdownComment(oldComment);
const linksInNew = this.extractLinksInMarkdownComment(newComment);
return _.difference(linksInOld, linksInNew);
}
}