Markdown link eats end parenthesis
If you haven’t already, check out our contributing guidelines for onboarding!
Platform - version: All - reproducible on v1.0.1-439
Action Performed (reproducible steps):
- Log into https://expensify.cash/
- Start chatting with another user
- Send a message in parenthesis with a hyperlink in markdown that has a
/at end of URL eg([google](https://google.com/))
Expected Result: For the URL in parenthesis to hyperlink and for the bracket to close (google)
Actual Result:
the last parenthetical bracket doesn't appear -

Hi @laurenreidexpensify
We need to update URL_PATH_REGEX in expensify-common/lib/ExpensiMark .
and updated URL_PATH_REGEX will look like this
const URL_PATH_REGEX = '(?:\\/[-\\w$@.&+!*"\'(),=%][-\\w~@:%)]|\\/)*';
Here is screen recoding with updated regex https://recordit.co/NNWAxPWz0R
NOTE: expensify-common is an npm package and we need to fix this regex in that package.
This solution already posted in slack by someone else https://expensify.slack.com/archives/C01GTK53T8Q/p1614702046088700?thread_ts=1614696084.088600&cid=C01GTK53T8Q
Thanks
cc @deetergp you were familiar with this issue
@aliabbasmalik8 That solution will break anything other than a trailing / for the URL path.
https://user-images.githubusercontent.com/766197/110391612-960ff580-801c-11eb-96e6-66667242fcff.mp4
Hi @deetergp
I tested all of above-mentioned URL with this regex and its work for me
just need to replace URL_PATH_REGEX in expensify-common/lib/ExpensiMark
const URL_PATH_REGEX = '(?:\\/[-\\w$@.&+!*"\'(),=%]*[-\\w~@:%]|\\/)*';
Please have a look. Thanks
@laurenreidexpensify It looks like @aliabbasmalik8 latest suggestion will do the trick 👍

Please some test assertions in ExpensiMark-test.js that confirm this does what it needs to do.
Not passed all test that mentioned in this fileExpensiMark-test.js,
It does not work for these type of URLs http://foo.com/(something)?after=parens
will let you know if found a solution.
Hi @laurenreidexpensify,
Here is my proposal.
This does happen, regex itself ignores the attached parenthesis () from the URL. In ExpensiMark.js There are three parts this issue occurs.
- URL_PATH_REGEX
- URL_PARAM_REGEX
- URL_FRAGMENT_REGEX
These are the three-part of scenarios that this issue occurs.
Fix
Here are the modified regex patterns.
const URL_PATH_REGEX = '(?:\\/[-\\w$@.&+!*"\'(),=%]*[-\\w~@:%]|\\/)*';
const URL_PARAM_REGEX = '(?:\\?[-\\w$@.&+!*"\'\\/,=%{}:;\\[\\]\\|_]+)?';
const URL_FRAGMENT_REGEX = '(?:#[-\\w$@.&+!*"\'[\\],=%;\\/:~]*)?';
Test cases
Here are the successfully tested test cases
([google](https://google.com/))
[google](https://google.com/)
([google](https://google.com))
[google](https://google.com)
([google](https://google.com/foo))
[google](https://google.com/foo)
([google](https://google.com/foo/))
[google](https://google.com/foo/)
([google](https://google.com/foo?abc=xyz))
[google](https://google.com/foo?abc=xyz)
([google](https://google.com/foo?abc=xyz/))
[google](https://google.com/foo?abc=xyz/)
([google](https://google.com/foo?abc=xyz&asd=expensify%cash))
[google](https://google.com/foo?abc=xyz&asd=expensify%cash)
([google](https://google.com/foo?abc=xyz#fragment))
[google](https://google.com/foo?abc=xyz#fragment)
([google](https://google.com/foo?abc=xyz#fragment/))
[google](https://google.com/foo?abc=xyz#fragment/)
([google](https://google.com/foo#fragment))
[google](https://google.com/foo#fragment)
([google](https://google.com/foo#fragment/))
[google](https://google.com/foo#fragment/)
Therefore, this PR should point to expensify-common repository
Screenshot

Thanks.
@SameeraMadushan Your solution also works, but just like @aliabbasmalik8's solution, it doesn't handle URLs that have parens in them like
([google](http://google.com/(something)?after=parens))
[google](http://google.com/(something)?after=parens)

@deetergp @laurenreidexpensify Here is another solution to this issue.
need to update modifyTextForUrlLinks in ExpensiMark.js file.
the solution is removed parentheses at the start and after replacing text add accordingly
Video: http://recordit.co/Y1eXVXst5r
Update modifyTextForUrlLinks function
modifyTextForUrlLinks(regex, textToCheck, replacement) {
let count = 0;
let updatedTextToCheck = textToCheck;
for (let i = 0; i < textToCheck.length && textToCheck[i] === '(' && textToCheck[textToCheck.length-i-1] === ')'; i++) {
count += 1;
}
if (count > 0 ) updatedTextToCheck = textToCheck.slice(count, -count);
let match = regex.exec(updatedTextToCheck);
let replacedText = '';
let startIndex = 0;
let abort = false;
while (match !== null) {
// we want to avoid matching email address domains
if ((match.index !== 0) && (updatedTextToCheck[match.index - 1] === '@')) {
abort = true;
}
// we want to avoid matching ending ) unless it is a closing parenthesis for the URL
if (updatedTextToCheck[(match.index + match[2].length) - 1] === ')' && !match[2].includes('(')) {
match[0] = match[0].substr(0, match[0].length - 1);
match[2] = match[2].substr(0, match[2].length - 1);
}
replacedText = replacedText.concat(updatedTextToCheck.substr(startIndex, (match.index - startIndex)));
if (abort) {
replacedText = replacedText.concat(updatedTextToCheck.substr(match.index, (match[0].length)));
} else {
replacedText = replacedText.concat(replacement(match[0], match[1], match[2], match[3]));
}
startIndex = match.index + (match[0].length);
// Now we move to the next match that the js regex found in the text
match = regex.exec(updatedTextToCheck);
}
if (startIndex < updatedTextToCheck.length) replacedText = replacedText.concat(updatedTextToCheck.substr(startIndex));
if (count !== 0) {
for (let i = 0; i < count; i++) {
replacedText = '(' + replacedText + ')';
}
}
return replacedText;
}
So the problem here is that when we add a trailing / to the url, the URL_PATH_REGEX kicks in and captures out to match out to the last ) which happens to be the one that isn't a part of the markdown link and shouldn't be captured. Sadly, JavaScript's regex engine does do recursion, otherwise we'd be able to do something like (?<text>\[(?>[^\[\]]+|(?&text))*\])(?<url>\((?>[^\(\)]+|(?&url))*\)) and get everything we need.
We want to be able to capture only matching pairs of []s and ()s and ignore any that don't have a counterpart. This solution seems really specific to just this case and feels like it's doing a lot of unnecessary looping. Unfortunately, I don't have great suggestion for how to make it better.
cc @timszot @marcaaron for any thoughts either of you might have.
Hi,
This is Kaushik here and I would like to suggest a simple and effective explanation for this,
- We need to update URL_PATH_REGEX in expensify-common/lib/ExpensiMark and updated URL_PATH_REGEX will look like this const URL_PATH_REGEX = '(?:\/[-\w$@.&+!"'(),=%][-\w~@:%)]|\/)';
@kaushiktd Your solution does fix this problem but breaks every other type of valid Markdown link I tried:

Hey @deetergp, It seems that your comment here https://github.com/Expensify/Expensify.cash/issues/1656#issuecomment-797789932 is not covered by the existing implementation. It means the current parser does not handle these URLs including the one mentioned in the issue. I have tried one solution here that covers all the existing tests and the URL mentioned in the issue and in your comments.
By looking at the code here. https://github.com/Expensify/expensify-common/blob/679fc86cfc4f9bc701e3757d583d74057edbbe28/lib/ExpensiMark.js#L205-L208
// we want to avoid matching ending ) unless it is a closing parenthesis for the URL
if (textToCheck[(match.index + match[2].length) - 1] === ')' && !match[2].includes('(')) {
match[0] = match[0].substr(0, match[0].length - 1);
match[2] = match[2].substr(0, match[2].length - 1);
}
I realized that we don't handle nested brackets. So we can introduce a bracket check here and solve this problem.
// we need this check as this function is used for two types of urls markdown links(eg: [link](url) ) and autolinks(eg: http//:google.com)
// if condition only run this logic for markdown style links
if(type === 'link'){
// remove extra brace
let brace = 0;
if (match[2][match[2].length - 1] === ')') {
for (let i = 0; i < match[2].length; i++) {
if (match[2][i] === '(') {
brace++;
}
if (match[2][i] === ')') {
brace--;
}
}
if (brace) {
match[0] = match[0].substr(0, match[0].length - 1);
match[2] = match[2].substr(0, match[2].length - 1);
}
}
}
It fixes this issue and the issues mentioned in your comment. I have set up a branch here with changes. https://github.com/parasharrajat/Expensify.cash/tree/parasharrajat/urlfix. You can pull it and try it. Let me know If can send the PR.
@Beamanator can you take a look at Rajat's proposal above?
@parasharrajat your solution looks like it's working amazing, but I did a bit of extra investigation and I think we might to use your code to fix some other broken markdown links & tests. Please let me know what you think about the following:
First, when I ran all test (including your extra tests in your PR) on your code seemed to work quite well 👍 so nice job there!
While I was testing I thought "why is your solution only useful when running the 'link' rule"? So I removed only your if condition, re-ran tests, and found the tests ONLY failed on a link with a closing paren ) at the end - 'https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878) ' (see hidden image for test output)
Image of failing test

This made me realize that test is actually broken too - the trailing paren ) should not be included inside the link, it should be external! So I did a super basic link test in staging - (http://foo.com/(something)?after=parens) and got:
https://user-images.githubusercontent.com/3885503/114176180-19ce4200-993b-11eb-8c51-9900f73c0264.mov
To me, it looks like this also needs to be fixed - am i crazy?
So here's what I'm thinking, but I'd appreciate a few people reviewing this cuz it involves editing / fixing a test :D
- Fix the test for
'https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878) '- Expected output is currently
'<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878)" target="_blank">https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878)</a> ', which i think should be changed to'<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878" target="_blank">https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878</a>) '(notice the closing paren doesn't have a matching starting paren, so it shouldn't be inside the anchor tag
- Expected output is currently
- Add tests for basic links with surrounding parenthesis (I believe my video proves that is currently also broken)
- Add your code, without the if-condition (looks like all tests run, plus it fixes the new issue i mentioned)
I'd love if someone else could double-check before approving :D
@Beamanator I see. I will investigate it a bit to see what else is broken and try to fix that. I think I know How to fix that as well.
@Beamanator This solution working properly. can you please have a look https://github.com/Expensify/Expensify.cash/issues/1656#issuecomment-800346840 Thanks
@Beamanator Yeah you are correct. we don't want the if condition at all. it fixes the issue that you have showed in the video. Plus I tested a couple of other links such as
(((http://foo.com/(something)?after=parens))
http://foo.com/(something)?after=parens)))
([google](http://google.com/(something)?after=parens))))
which are were not working fine but. I realized that I just need to adjust one line in my solution.
if (brace) {
console.debug(brace);
// removed all the braces match[0].length + brace
match[0] = match[0].substr(0, match[0].length + brace);
match[2] = match[2].substr(0, match[2].length + brace);
}
here we remove all the extra matched brackets which we just calculated in brace variable and it worked.
I fixed that test as well, it was wrong (Nice catch). Now it covers all the tests without if check. and I added more tests. PR is ready. Needs a :+1:
Just leaving :my_two_cents: here since I wrote the original parentheses block here.
I like this proposed solution as that expands and improves the original implementation.
@Beamanator This solution working properly. can you please have a look #1656 (comment) Thanks
Hey @aliabbasmalik8 I checked your solution with some of the new cases we're going to add tests for, and these seem to be failing still:
<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878" target="_blank">https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878</a>)- Note: this is an existing test that need to be updated, as I mentioned here
([google](http://google.com/(something)?after=parens))->(<a href="http://google.com/(something)?after=parens" target="_blank">google</a>)([google](http://google.com/(something)?after=parens))))->(<a href="http://google.com/(something)?after=parens" target="_blank">google</a>)))
The last two are some new tests we're going to add