Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(markdown): balanced parentheses inside autolinks (#41)

hampus-fluxer1ed066ca361d
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 03338448a..f27b38c3f 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 c091e161c..fa0ca5087 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) {
+ 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++;
+ }
- while (end < textLength && !StringUtils.isUrlTerminationChar(text[end])) {
- end++;
- if (end - prefixLength > MAX_LINK_URL_LENGTH) break;
+ if (end - prefixLength > MAX_LINK_URL_LENGTH) {
+ end = prefixLength + MAX_LINK_URL_LENGTH;
+ break;
+ }
}
let urlString = text.slice(0, end);