Skip to content

Commit

Permalink
Ignore invalid timestamp formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored and cooljeanius committed Nov 20, 2023
1 parent d4caae7 commit ebd84e6
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions DiscordChatExporter.Core/Markdown/Parsing/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,21 +340,38 @@ internal static partial class MarkdownParser
)
);
// https://discord.com/developers/docs/reference#message-formatting-timestamp-styles
var format = m.Groups[2].Value.NullIfWhiteSpace() switch
{
// Ignore the 'relative' format because it doesn't make sense in a static export
// Known formats
"t" => "t",
"T" => "T",
"d" => "d",
"D" => "D",
"f" => "f",
"F" => "F",
// Relative format: ignore because it doesn't make sense in a static export
"r" => null,
"R" => null,
// Discord's date formats are (mostly) compatible with .NET's date formats
var f => f
// Unknown format: throw an exception to consider this timestamp invalid
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1156
var f
=> throw new InvalidOperationException(
$"Unknown timestamp format '{f}'."
)
};
return new TimestampNode(instant, format);
}
// https://github.com/Tyrrrz/DiscordChatExporter/issues/681
// https://github.com/Tyrrrz/DiscordChatExporter/issues/766
catch (Exception ex)
when (ex is FormatException or ArgumentOutOfRangeException or OverflowException)
when (ex
is FormatException
or ArgumentOutOfRangeException
or OverflowException
or InvalidOperationException
)
{
// For invalid timestamps, Discord renders "Invalid Date" instead of ignoring the markdown
return TimestampNode.Invalid;
Expand Down

0 comments on commit ebd84e6

Please sign in to comment.