fix(markdown): balanced parentheses inside autolinks (#41)
This commit is contained in:
parent
39ac149810
commit
1ed066ca36
@ -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', () => {
|
test('standard URLs with autolinks disabled', () => {
|
||||||
const input = 'Visit https://example.com and http://test.org';
|
const input = 'Visit https://example.com and http://test.org';
|
||||||
const flags = 0;
|
const flags = 0;
|
||||||
|
|||||||
@ -314,10 +314,31 @@ export function extractUrlSegment(text: string, parserFlags: number): ParserResu
|
|||||||
|
|
||||||
let end = prefixLength;
|
let end = prefixLength;
|
||||||
const textLength = text.length;
|
const textLength = text.length;
|
||||||
|
let parenthesesDepth = 0;
|
||||||
|
|
||||||
while (end < textLength && !StringUtils.isUrlTerminationChar(text[end])) {
|
while (end < textLength) {
|
||||||
end++;
|
const currentChar = text[end];
|
||||||
if (end - prefixLength > MAX_LINK_URL_LENGTH) break;
|
|
||||||
|
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);
|
let urlString = text.slice(0, end);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user