diff --git a/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.test.ts b/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.test.ts index 03338448..f27b38c3 100644 --- a/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.test.ts +++ b/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.test.ts @@ -672,6 +672,24 @@ describe('Fluxer Markdown Parser', () => { ]); }); + test('urls with balanced parentheses inside autolinks', () => { + const input = 'Visit https://en.wikipedia.org/wiki/Chris_Messina_(inventor) for documentation.'; + const flags = ParserFlags.ALLOW_AUTOLINKS; + const parser = new Parser(input, flags); + const {nodes: ast} = parser.parse(); + + expect(ast).toEqual([ + {type: NodeType.Text, content: 'Visit '}, + { + type: NodeType.Link, + text: undefined, + url: 'https://en.wikipedia.org/wiki/Chris_Messina_(inventor)', + escaped: false, + }, + {type: NodeType.Text, content: ' for documentation.'}, + ]); + }); + test('standard URLs with autolinks disabled', () => { const input = 'Visit https://example.com and http://test.org'; const flags = 0; diff --git a/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.ts b/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.ts index c091e161..fa0ca508 100644 --- a/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.ts +++ b/fluxer_app/src/lib/markdown/parser/parsers/link-parsers.ts @@ -314,10 +314,31 @@ export function extractUrlSegment(text: string, parserFlags: number): ParserResu let end = prefixLength; const textLength = text.length; + let parenthesesDepth = 0; - while (end < textLength && !StringUtils.isUrlTerminationChar(text[end])) { - end++; - if (end - prefixLength > MAX_LINK_URL_LENGTH) break; + while (end < textLength) { + const currentChar = text[end]; + + if (currentChar === '(') { + parenthesesDepth++; + end++; + } else if (currentChar === ')') { + if (parenthesesDepth > 0) { + parenthesesDepth--; + end++; + } else { + break; + } + } else if (StringUtils.isUrlTerminationChar(currentChar)) { + break; + } else { + end++; + } + + if (end - prefixLength > MAX_LINK_URL_LENGTH) { + end = prefixLength + MAX_LINK_URL_LENGTH; + break; + } } let urlString = text.slice(0, end);