From 5632d6c3867d2f79cba9578056c98caee4377c1b Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 4 Jun 2026 13:09:03 +0300 Subject: [PATCH] HTML-encode markdown content when formatting is disabled (#1545) --- .../Exporting/MessageGroupTemplate.cshtml | 35 ++++++++++--------- .../Exporting/PreambleTemplate.cshtml | 11 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml b/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml index f0dba0de..b3c06837 100644 --- a/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml @@ -2,6 +2,7 @@ @using System.Collections.Generic @using System.Linq @using System.Threading.Tasks +@using RazorBlade @using DiscordChatExporter.Core.Discord.Data @using DiscordChatExporter.Core.Discord.Data.Embeds @using DiscordChatExporter.Core.Markdown.Parsing @@ -23,15 +24,15 @@ string FormatDate(DateTimeOffset instant, string format = "g") => Context.FormatDate(instant, format); - async ValueTask FormatMarkdownAsync(string markdown) => + async ValueTask FormatMarkdownAsync(string markdown) => Context.Request.ShouldFormatMarkdown - ? await HtmlMarkdownVisitor.FormatAsync(Context, markdown, true, CancellationToken) - : markdown; + ? Html.Raw(await HtmlMarkdownVisitor.FormatAsync(Context, markdown, true, CancellationToken)) + : Html.Raw(Html.Encode(markdown)); - async ValueTask FormatEmbedMarkdownAsync(string markdown) => + async ValueTask FormatEmbedMarkdownAsync(string markdown) => Context.Request.ShouldFormatMarkdown - ? await HtmlMarkdownVisitor.FormatAsync(Context, markdown, false, CancellationToken) - : markdown; + ? Html.Raw(await HtmlMarkdownVisitor.FormatAsync(Context, markdown, false, CancellationToken)) + : Html.Raw(Html.Encode(markdown)); }
@@ -179,7 +180,7 @@ @if (!string.IsNullOrWhiteSpace(message.ReferencedMessage.Content) && !message.ReferencedMessage.IsContentHidden()) { - @Html.Raw(await FormatEmbedMarkdownAsync(message.ReferencedMessage.Content)) + @(await FormatEmbedMarkdownAsync(message.ReferencedMessage.Content)) } else if (message.ReferencedMessage.Attachments.Any() || message.ReferencedMessage.Embeds.Any()) { @@ -252,7 +253,7 @@ @* Text *@ @if (!string.IsNullOrWhiteSpace(message.Content) && !message.IsContentHidden()) { - @Html.Raw(await FormatMarkdownAsync(message.Content)) + @(await FormatMarkdownAsync(message.Content)) } @* Edited timestamp *@ @@ -278,7 +279,7 @@ @if (!string.IsNullOrWhiteSpace(message.ForwardedMessage.Content)) {
- @Html.Raw(await FormatMarkdownAsync(message.ForwardedMessage.Content)) + @(await FormatMarkdownAsync(message.ForwardedMessage.Content))
} @@ -504,12 +505,12 @@ @if (!string.IsNullOrWhiteSpace(embed.Url)) { -
@Html.Raw(await FormatEmbedMarkdownAsync(embed.Title))
+
@(await FormatEmbedMarkdownAsync(embed.Title))
} else { -
@Html.Raw(await FormatEmbedMarkdownAsync(embed.Title))
+
@(await FormatEmbedMarkdownAsync(embed.Title))
}
} @@ -543,7 +544,7 @@ } // Generic video embed - else if (embed.Kind == EmbedKind.Video + else if (embed.Kind == EmbedKind.Video && !string.IsNullOrWhiteSpace(embed.Url) // Twitch clips cannot be embedded in local HTML files && embed.TryGetTwitchClip() is null) @@ -624,12 +625,12 @@ @if (!string.IsNullOrWhiteSpace(embed.Url)) { -
@Html.Raw(await FormatEmbedMarkdownAsync(embed.Title))
+
@(await FormatEmbedMarkdownAsync(embed.Title))
} else { -
@Html.Raw(await FormatEmbedMarkdownAsync(embed.Title))
+
@(await FormatEmbedMarkdownAsync(embed.Title))
} } @@ -638,7 +639,7 @@ @if (!string.IsNullOrWhiteSpace(embed.Description)) {
-
@Html.Raw(await FormatEmbedMarkdownAsync(embed.Description))
+
@(await FormatEmbedMarkdownAsync(embed.Description))
} @@ -652,14 +653,14 @@ @if (!string.IsNullOrWhiteSpace(field.Name)) {
-
@Html.Raw(await FormatEmbedMarkdownAsync(field.Name))
+
@(await FormatEmbedMarkdownAsync(field.Name))
} @if (!string.IsNullOrWhiteSpace(field.Value)) {
-
@Html.Raw(await FormatEmbedMarkdownAsync(field.Value))
+
@(await FormatEmbedMarkdownAsync(field.Value))
} diff --git a/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml b/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml index aa42294d..1b5f300a 100644 --- a/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml @@ -1,5 +1,6 @@ @using System @using System.Threading.Tasks +@using RazorBlade @inherits RazorBlade.HtmlTemplate @@ -24,10 +25,10 @@ string FormatDate(DateTimeOffset instant, string format = "g") => Context.FormatDate(instant, format); - async ValueTask FormatMarkdownAsync(string markdown) => + async ValueTask FormatMarkdownAsync(string markdown) => Context.Request.ShouldFormatMarkdown - ? await HtmlMarkdownVisitor.FormatAsync(Context, markdown, true, CancellationToken) - : markdown; + ? Html.Raw(await HtmlMarkdownVisitor.FormatAsync(Context, markdown, true, CancellationToken)) + : Html.Raw(Html.Encode(markdown)); } @@ -748,7 +749,7 @@ .chatlog__embed-spotify { border: 0; } - + .chatlog__embed-twitch { border: 0; } @@ -1063,7 +1064,7 @@ @if (!string.IsNullOrWhiteSpace(Context.Request.Channel.Topic)) { -
@Html.Raw(await FormatMarkdownAsync(Context.Request.Channel.Topic))
+
@(await FormatMarkdownAsync(Context.Request.Channel.Topic))
} @if (Context.Request.After is not null || Context.Request.Before is not null)