feat: clickable URLs in chat log

Adds a parsing step when constructing `Message` objects that scans the
message content for anything that looks URL-like, and inserts new
`TextChunk`s into the message content with a URIPayload set.

Hovering over a URL shows an on-hover effect. Clicking a URL opens it in
the default browser. Right clicking shows the hostname, with an option
to open and an option to copy the URL to the clipboard.
This commit is contained in:
Dean Sheather
2024-04-09 00:49:15 +10:00
parent fed420901c
commit 4701bb3f6d
10 changed files with 295 additions and 4 deletions
+88 -1
View File
@@ -1,7 +1,9 @@
using ChatTwo.Code;
using ChatTwo.Util;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using LiteDB;
using System.Text.RegularExpressions;
namespace ChatTwo;
@@ -70,7 +72,7 @@ internal class Message {
this.Date = DateTime.UtcNow;
this.Code = code;
this.Sender = sender;
this.Content = content;
this.Content = ReplaceContentURLs(content);
this.SenderSource = senderSource;
this.ContentSource = contentSource;
this.SortCode = new SortCode(this.Code.Type, this.Code.Source);
@@ -89,6 +91,8 @@ internal class Message {
this.Date = date;
this.Code = BsonMapper.Global.ToObject<ChatCode>(code);
this.Sender = BsonMapper.Global.Deserialize<List<Chunk>>(sender);
// Don't call ReplaceContentURLs here since we're loading the message
// from the database and it should already have parsed URL data.
this.Content = BsonMapper.Global.Deserialize<List<Chunk>>(content);
this.SenderSource = BsonMapper.Global.Deserialize<SeString>(senderSource);
this.ContentSource = BsonMapper.Global.Deserialize<SeString>(contentSource);
@@ -108,6 +112,8 @@ internal class Message {
this.Date = date;
this.Code = BsonMapper.Global.ToObject<ChatCode>(code);
this.Sender = BsonMapper.Global.Deserialize<List<Chunk>>(sender);
// Don't call ReplaceContentURLs here since we're loading the message
// from the database and it should already have parsed URL data.
this.Content = BsonMapper.Global.Deserialize<List<Chunk>>(content);
this.SenderSource = BsonMapper.Global.Deserialize<SeString>(senderSource);
this.ContentSource = BsonMapper.Global.Deserialize<SeString>(contentSource);
@@ -138,4 +144,85 @@ internal class Message {
return Guid.Empty;
}
/// <summary>
/// URLRegex returns a regex object that matches URLs like:
/// - https://example.com
/// - http://example.com
/// - www.example.com
/// - https://sub.example.com
/// - example.com
/// - sub.example.com
///
/// It matches URLs with www. or https:// prefix, and also matches URLs
/// without a prefix on specific TLDs.
/// </summary>
private static Regex URLRegex = new Regex(
@"((https?:\/\/|www\.)[a-z0-9-]+(\.[a-z0-9-]+)*|([a-z0-9-]+(\.[a-z0-9-]+)*\.(com|net|org|co|io|app)))(:[\d]{1,5})?(\/[^\s]+)?",
RegexOptions.Compiled | RegexOptions.IgnoreCase
);
/// <summary>
/// Finds all URL strings in all TextChunks, splits the parent TextChunk
/// apart and inserts a new TextChunk with a URIPayload.
/// </summary>
private List<Chunk> ReplaceContentURLs(List<Chunk> content)
{
var newChunks = new List<Chunk>();
void AddChunkWithMessage(Chunk chunk) {
chunk.Message = this;
newChunks.Add(chunk);
}
foreach (var chunk in content)
{
// Use as is if it's not a text chunk or it already has a payload.
if (chunk is not TextChunk text || chunk.Link != null)
{
// No need to call AddChunkWithMessage here since the chunk
// already has the Message field set.
newChunks.Add(chunk);
continue;
}
// Find all URLs with the regex and insert a new TextChunk with a
// URIPayload.
var matches = URLRegex.Matches(text.Content);
var remainderIndex = 0;
foreach (Match match in matches.Cast<Match>())
{
// Add the text before the URL.
if (match.Index > remainderIndex)
{
AddChunkWithMessage(text.NewWithStyle(chunk.Source, chunk.Link, text.Content[remainderIndex..match.Index]));
}
// Update the remainder index.
remainderIndex = match.Index + match.Length;
// Create a new TextChunk with a URIPayload for the URL text.
try
{
var link = URIPayload.ResolveURI(match.Value);
AddChunkWithMessage(text.NewWithStyle(chunk.Source, link, match.Value));
}
catch (UriFormatException)
{
Plugin.Log.Debug($"Invalid URL accepted by Regex but failed URI parsing: '{match.Value}'");
// If the URL is invalid, set the remainder index to the
// beginning of the match so it'll get included in the next
// regular text chunk.
remainderIndex = match.Index;
}
}
// Add the text after the last URL.
if (remainderIndex < text.Content.Length)
{
AddChunkWithMessage(text.NewWithStyle(chunk.Source, null, text.Content[remainderIndex..]));
}
}
return newChunks;
}
}