From 7996ea669bff8f966d5a8f6a222bbd0847a36b54 Mon Sep 17 00:00:00 2001 From: graphiteisaac Date: Tue, 30 Dec 2025 11:54:30 +1100 Subject: [PATCH] ensure codeblocks are closed This is really quite hard to test, but I think this should be able to fix the case reason codeblock problem we run into on the Overwatch server fairly often --- .../plugins/Cases/functions/getCaseSummary.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Cases/functions/getCaseSummary.ts b/backend/src/plugins/Cases/functions/getCaseSummary.ts index 56fab714e..826a148bb 100644 --- a/backend/src/plugins/Cases/functions/getCaseSummary.ts +++ b/backend/src/plugins/Cases/functions/getCaseSummary.ts @@ -39,8 +39,28 @@ export async function getCaseSummary( const match = reason.slice(CASE_SUMMARY_REASON_MAX_LENGTH, 100).match(/(?:[.,!?\s]|$)/); const nextWhitespaceIndex = match ? CASE_SUMMARY_REASON_MAX_LENGTH + match.index! : CASE_SUMMARY_REASON_MAX_LENGTH; const reasonChunks = splitMessageIntoChunks(reason, nextWhitespaceIndex); - reason = reasonChunks[0] + "..."; - } + reason = reasonChunks[0] + + // Sanitise case reasons to ensure codeblocks are properly closed in truncated case reasons + const tripleBacktickMatches = reason.match(/```/g); + const tripleBacktickCount = tripleBacktickMatches ? tripleBacktickMatches.length : 0; + + // If odd number of triple backticks, we have an unclosed codeblock + if (tripleBacktickCount % 2 !== 0) { + reason += "```"; + } + + // Also check for single backticks (inline code) + const singleBacktickMatches = reason.match(/(?