Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit b853eef

Browse files
committed
Fix url parsing.
1 parent b492928 commit b853eef

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Libs/Quarrel.Markdown/Parsing/Parser.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Text;
67
using System.Text.RegularExpressions;
78

@@ -92,8 +93,18 @@ internal static IList<IAST> ParseAST(string text, bool inlineState, bool inQuote
9293
}
9394
else if (url.Match(text) is { Success: true } urlMatch && inlineState)
9495
{
95-
inline = new Url(urlMatch.Groups[1].Value);
96-
text = text.Substring(urlMatch.Length);
96+
string urlContent = urlMatch.Groups[1].Value;
97+
for (int i = urlContent.Length - 1; i >= 0; i--)
98+
{
99+
if (urlContent[i] != ')')
100+
{
101+
int count = urlContent.Count(c => c == '(');
102+
urlContent = urlContent.Substring(0, Math.Min(i + count + 1, urlContent.Length));
103+
break;
104+
}
105+
}
106+
inline = new Url(urlContent);
107+
text = text.Substring(urlContent.Length);
97108
}
98109
else if (strong.Match(text) is { Success: true } strongMatch && inlineState)
99110
{

src/Libs/Quarrel.Markdown/Rendering/MessageRenderer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ private void RenderMarkdown(IList<ASTRoot> tree)
281281
}
282282
break;
283283
case Url url:
284-
inlineCollection.Add(new Hyperlink() { NavigateUri = new Uri(url.Content), Inlines = { new Run() { Text = url.Content } } });
284+
inlineCollection.Add(new Hyperlink()
285+
{
286+
NavigateUri = Uri.TryCreate(url.Content, UriKind.RelativeOrAbsolute, out Uri uri) ? uri : null,
287+
Inlines = { new Run() { Text = url.Content } }
288+
});
285289
break;
286290
case IEmojiAST emoji:
287291
{

0 commit comments

Comments
 (0)