mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 09:32:55 +08:00
Merge pull request #6591 from bdach/markdown-inline-links
Add support for Markdown inline links
This commit is contained in:
commit
5a70431263
@ -273,6 +273,96 @@ namespace osu.Game.Tests.Chat
|
||||
Assert.AreEqual(21, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkWithInlineTitle()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "I haven't seen [this link format](https://osu.ppy.sh \"osu!\") before..." });
|
||||
|
||||
Assert.AreEqual("I haven't seen this link format before...", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[0].Url);
|
||||
Assert.AreEqual(15, result.Links[0].Index);
|
||||
Assert.AreEqual(16, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkWithInlineTitleAndEscapedQuotes()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "I haven't seen [this link format](https://osu.ppy.sh \"inner quote \\\" just to confuse \") before..." });
|
||||
|
||||
Assert.AreEqual("I haven't seen this link format before...", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[0].Url);
|
||||
Assert.AreEqual(15, result.Links[0].Index);
|
||||
Assert.AreEqual(16, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkWithUrlInTextAndInlineTitle()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "I haven't seen [https://osu.ppy.sh](https://osu.ppy.sh \"https://osu.ppy.sh\") before..." });
|
||||
|
||||
Assert.AreEqual("I haven't seen https://osu.ppy.sh before...", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[0].Url);
|
||||
Assert.AreEqual(15, result.Links[0].Index);
|
||||
Assert.AreEqual(18, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkWithUrlAndTextInTitle()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "I haven't seen [oh no, text here! https://osu.ppy.sh](https://osu.ppy.sh) before..." });
|
||||
|
||||
Assert.AreEqual("I haven't seen oh no, text here! https://osu.ppy.sh before...", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[0].Url);
|
||||
Assert.AreEqual(15, result.Links[0].Index);
|
||||
Assert.AreEqual(36, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkWithMisleadingUrlInText()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "I haven't seen [https://google.com](https://osu.ppy.sh) before..." });
|
||||
|
||||
Assert.AreEqual("I haven't seen https://google.com before...", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[0].Url);
|
||||
Assert.AreEqual(15, result.Links[0].Index);
|
||||
Assert.AreEqual(18, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkThatContractsIntoLargerLink()
|
||||
{
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "super broken https://[osu.ppy](https://reddit.com).sh/" });
|
||||
|
||||
Assert.AreEqual("super broken https://osu.ppy.sh/", result.DisplayContent);
|
||||
Assert.AreEqual(1, result.Links.Count);
|
||||
Assert.AreEqual("https://reddit.com", result.Links[0].Url);
|
||||
Assert.AreEqual(21, result.Links[0].Index);
|
||||
Assert.AreEqual(7, result.Links[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMarkdownFormatLinkDirectlyNextToRawLink()
|
||||
{
|
||||
// the raw link has a port at the end of it, so that the raw link regex terminates at the port and doesn't consume display text from the formatted one
|
||||
Message result = MessageFormatter.FormatMessage(new Message { Content = "https://localhost:8080[https://osu.ppy.sh](https://osu.ppy.sh) should be two links" });
|
||||
|
||||
Assert.AreEqual("https://localhost:8080https://osu.ppy.sh should be two links", result.DisplayContent);
|
||||
Assert.AreEqual(2, result.Links.Count);
|
||||
|
||||
Assert.AreEqual("https://localhost:8080", result.Links[0].Url);
|
||||
Assert.AreEqual(0, result.Links[0].Index);
|
||||
Assert.AreEqual(22, result.Links[0].Length);
|
||||
|
||||
Assert.AreEqual("https://osu.ppy.sh", result.Links[1].Url);
|
||||
Assert.AreEqual(22, result.Links[1].Index);
|
||||
Assert.AreEqual(18, result.Links[1].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestChannelLink()
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ namespace osu.Game.Online.Chat
|
||||
private static readonly Regex new_link_regex = new Regex(@"\[(?<url>[a-z]+://[^ ]+) (?<text>(((?<=\\)[\[\]])|[^\[\]])*(((?<open>\[)(((?<=\\)[\[\]])|[^\[\]])*)+((?<close-open>\])(((?<=\\)[\[\]])|[^\[\]])*)+)*(?(open)(?!)))\]");
|
||||
|
||||
// [test](https://osu.ppy.sh/b/1234) -> test (https://osu.ppy.sh/b/1234) aka correct markdown format
|
||||
private static readonly Regex markdown_link_regex = new Regex(@"\[(?<text>(((?<=\\)[\[\]])|[^\[\]])*(((?<open>\[)(((?<=\\)[\[\]])|[^\[\]])*)+((?<close-open>\])(((?<=\\)[\[\]])|[^\[\]])*)+)*(?(open)(?!)))\]\((?<url>[a-z]+://[^ ]+)\)");
|
||||
private static readonly Regex markdown_link_regex = new Regex(@"\[(?<text>(((?<=\\)[\[\]])|[^\[\]])*(((?<open>\[)(((?<=\\)[\[\]])|[^\[\]])*)+((?<close-open>\])(((?<=\\)[\[\]])|[^\[\]])*)+)*(?(open)(?!)))\]\((?<url>[a-z]+://[^ ]+)(\s+(?<title>""([^""]|(?<=\\)"")*""))?\)");
|
||||
|
||||
// advanced, RFC-compatible regular expression that matches any possible URL, *but* allows certain invalid characters that are widely used
|
||||
// This is in the format (<required>, [optional]):
|
||||
@ -95,11 +95,17 @@ namespace osu.Game.Online.Chat
|
||||
foreach (Match m in regex.Matches(result.Text, startIndex))
|
||||
{
|
||||
var index = m.Index;
|
||||
var link = m.Groups["link"].Value;
|
||||
var indexLength = link.Length;
|
||||
var linkText = m.Groups["link"].Value;
|
||||
var indexLength = linkText.Length;
|
||||
|
||||
var details = getLinkDetails(link);
|
||||
result.Links.Add(new Link(link, index, indexLength, details.Action, details.Argument));
|
||||
var details = getLinkDetails(linkText);
|
||||
var link = new Link(linkText, index, indexLength, details.Action, details.Argument);
|
||||
|
||||
// sometimes an already-processed formatted link can reduce to a simple URL, too
|
||||
// (example: [mean example - https://osu.ppy.sh](https://osu.ppy.sh))
|
||||
// therefore we need to check if any of the pre-existing links contains the raw one we found
|
||||
if (result.Links.All(existingLink => !existingLink.Overlaps(link)))
|
||||
result.Links.Add(link);
|
||||
}
|
||||
}
|
||||
|
||||
@ -292,6 +298,8 @@ namespace osu.Game.Online.Chat
|
||||
Argument = argument;
|
||||
}
|
||||
|
||||
public bool Overlaps(Link otherLink) => Index < otherLink.Index + otherLink.Length && otherLink.Index < Index + Length;
|
||||
|
||||
public int CompareTo(Link otherLink) => Index > otherLink.Index ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user