mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-01 15:49:11 +00:00
@@ -159,12 +159,18 @@ internal partial class HtmlMarkdownVisitor : MarkdownVisitor
|
||||
|
||||
protected override MarkdownNode VisitUnixTimestamp(UnixTimestampNode timestamp)
|
||||
{
|
||||
var dateString = timestamp.Date is not null
|
||||
? _context.FormatDate(timestamp.Date.Value)
|
||||
: "Invalid date";
|
||||
|
||||
// Timestamp tooltips always use full date regardless of the configured format
|
||||
var longDateString = timestamp.Value.ToLocalString("dddd, MMMM d, yyyy h:mm tt");
|
||||
var longDateString = timestamp.Date is not null
|
||||
? timestamp.Date.Value.ToLocalString("dddd, MMMM d, yyyy h:mm tt")
|
||||
: "Invalid date";
|
||||
|
||||
_buffer
|
||||
.Append($"<span class=\"timestamp\" title=\"{HtmlEncode(longDateString)}\">")
|
||||
.Append(HtmlEncode(_context.FormatDate(timestamp.Value)))
|
||||
.Append(HtmlEncode(dateString))
|
||||
.Append("</span>");
|
||||
|
||||
return base.VisitUnixTimestamp(timestamp);
|
||||
|
||||
@@ -73,7 +73,9 @@ internal partial class PlainTextMarkdownVisitor : MarkdownVisitor
|
||||
protected override MarkdownNode VisitUnixTimestamp(UnixTimestampNode timestamp)
|
||||
{
|
||||
_buffer.Append(
|
||||
_context.FormatDate(timestamp.Value)
|
||||
timestamp.Date is not null
|
||||
? _context.FormatDate(timestamp.Date.Value)
|
||||
: "Invalid date"
|
||||
);
|
||||
|
||||
return base.VisitUnixTimestamp(timestamp);
|
||||
|
||||
@@ -235,7 +235,7 @@ internal static partial class MarkdownParser
|
||||
|
||||
// Capture <t:12345678> or <t:12345678:R>
|
||||
private static readonly IMatcher<MarkdownNode> UnixTimestampNodeMatcher = new RegexMatcher<MarkdownNode>(
|
||||
new Regex("<t:(\\d+)(?::\\w)?>", DefaultRegexOptions),
|
||||
new Regex("<t:(-?\\d+)(?::\\w)?>", DefaultRegexOptions),
|
||||
(_, m) =>
|
||||
{
|
||||
// TODO: support formatting parameters
|
||||
@@ -244,17 +244,19 @@ internal static partial class MarkdownParser
|
||||
if (!long.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture,
|
||||
out var offset))
|
||||
{
|
||||
return null;
|
||||
return new UnixTimestampNode(null);
|
||||
}
|
||||
|
||||
// Bound check
|
||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/681
|
||||
if (offset < TimeSpan.MinValue.TotalSeconds || offset > TimeSpan.MaxValue.TotalSeconds)
|
||||
try
|
||||
{
|
||||
return null;
|
||||
return new UnixTimestampNode(DateTimeOffset.UnixEpoch + TimeSpan.FromSeconds(offset));
|
||||
}
|
||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/681
|
||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/766
|
||||
catch (Exception ex) when (ex is ArgumentOutOfRangeException or OverflowException)
|
||||
{
|
||||
return new UnixTimestampNode(null);
|
||||
}
|
||||
|
||||
return new UnixTimestampNode(DateTimeOffset.UnixEpoch + TimeSpan.FromSeconds(offset));
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown;
|
||||
|
||||
internal record UnixTimestampNode(DateTimeOffset Value) : MarkdownNode;
|
||||
// Null means invalid date
|
||||
internal record UnixTimestampNode(DateTimeOffset? Date) : MarkdownNode;
|
||||
Reference in New Issue
Block a user