Skip to content

Commit

Permalink
Merge pull request #407 from mananjadhav/fix/md-hyperlink-regex
Browse files Browse the repository at this point in the history
Fix MD Hyperlink Regex for nested cases
  • Loading branch information
tgolen authored Aug 18, 2021
2 parents 2cda5ef + 11018de commit a88a45c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 18 additions & 2 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,15 @@ test('Test markdown and url links with inconsistent starting and closing parens'
+ '[Yo (click here to see a cool cat)](https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg) '
+ '[Yo click here to see a cool cat)](https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg) '
+ '[Yo (click here to see a cool cat](https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg) '
+ '[Yo click * $ & here to see a cool cat](https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg) ';
+ '[Yo click * $ & here to see a cool cat](https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg) '
+ '[Text text] more text ([link here](www.google.com))'
+ '[Text text] more text ([link [square brackets within] here](www.google.com))'
+ '[Text text] more text ([link (parenthesis within) here](www.google.com))'
+ '[Text text] more text [link here](www.google.com)'
+ '[Text text] more text ([link here ](www.google.com))'
+ '[Text text] more text (([link here](www.google.com)))'
+ '[Text text] more text [([link here](www.google.com))]'
+ '[Text text] more text ([link here](www.google.com))[Text text] more text ([link here](www.google.com))';


const resultString = '<a href="http://google.com/(something)?after=parens" target="_blank">google</a> '
Expand All @@ -353,7 +361,15 @@ test('Test markdown and url links with inconsistent starting and closing parens'
+ '<a href="https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg" target="_blank">Yo (click here to see a cool cat)</a> '
+ '<a href="https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg" target="_blank">Yo click here to see a cool cat)</a> '
+ '<a href="https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg" target="_blank">Yo (click here to see a cool cat</a> '
+ '<a href="https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg" target="_blank">Yo click * $ &amp; here to see a cool cat</a> ';
+ '<a href="https://c8.alamy.com/compes/ha11pc/cookie-cat-con-sombrero-de-cowboy-y-sun-glass-ha11pc.jpg" target="_blank">Yo click * $ &amp; here to see a cool cat</a> '
+ '[Text text] more text (<a href="http://www.google.com" target="_blank">link here</a>)'
+ '[Text text] more text (<a href="http://www.google.com" target="_blank">link [square brackets within] here</a>)'
+ '[Text text] more text (<a href="http://www.google.com" target="_blank">link (parenthesis within) here</a>)'
+ '[Text text] more text <a href="http://www.google.com" target="_blank">link here</a>'
+ '[Text text] more text (<a href="http://www.google.com" target="_blank">link here </a>)'
+ '[Text text] more text ((<a href="http://www.google.com" target="_blank">link here</a>))'
+ '[Text text] more text [(<a href="http://www.google.com" target="_blank">link here</a>)]'
+ '[Text text] more text (<a href="http://www.google.com" target="_blank">link here</a>)[Text text] more text (<a href="http://www.google.com" target="_blank">link here</a>)'


expect(parser.replace(testString)).toBe(resultString);
Expand Down
3 changes: 1 addition & 2 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ export default class ExpensiMark {
*/
{
name: 'link',

process: (textToProcess, replacement) => {
const regex = new RegExp(
`\\[(.+?)\\]\\(${URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`,
`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`,
'gi'
);
return this.modifyTextForUrlLinks(regex, textToProcess, replacement);
Expand Down

0 comments on commit a88a45c

Please sign in to comment.