From 56805874c28faf1741425923f44ba65ba94e6387 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 9 Apr 2026 15:15:48 +0200 Subject: [PATCH] Normalize markdown indentation --- .../Settings/DataModel/DataMandatoryInfo.cs | 3 +- app/MindWork AI Studio/Tools/Markdown.cs | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/app/MindWork AI Studio/Settings/DataModel/DataMandatoryInfo.cs b/app/MindWork AI Studio/Settings/DataModel/DataMandatoryInfo.cs index ad49b008..167f9dc3 100644 --- a/app/MindWork AI Studio/Settings/DataModel/DataMandatoryInfo.cs +++ b/app/MindWork AI Studio/Settings/DataModel/DataMandatoryInfo.cs @@ -80,12 +80,13 @@ public sealed record DataMandatoryInfo return false; } + var normalizedMarkdown = AIStudio.Tools.Markdown.RemoveSharedIndentation(markdown); mandatoryInfo = new DataMandatoryInfo { Id = id.ToString(), Title = title, VersionText = versionText, - Markdown = markdown, + Markdown = normalizedMarkdown, AcceptButtonText = acceptButtonText, RejectButtonText = rejectButtonText, EnterpriseConfigurationPluginId = configPluginId, diff --git a/app/MindWork AI Studio/Tools/Markdown.cs b/app/MindWork AI Studio/Tools/Markdown.cs index 49a2309c..13b42ea8 100644 --- a/app/MindWork AI Studio/Tools/Markdown.cs +++ b/app/MindWork AI Studio/Tools/Markdown.cs @@ -26,4 +26,57 @@ public static class Markdown }, } }; + + public static string RemoveSharedIndentation(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + var normalized = value.Replace("\r\n", "\n"); + var lines = normalized.Split('\n'); + + var firstContentLine = 0; + while (firstContentLine < lines.Length && string.IsNullOrWhiteSpace(lines[firstContentLine])) + firstContentLine++; + + var lastContentLine = lines.Length - 1; + while (lastContentLine >= firstContentLine && string.IsNullOrWhiteSpace(lines[lastContentLine])) + lastContentLine--; + + if (firstContentLine > lastContentLine) + return string.Empty; + + var commonIndentation = int.MaxValue; + for (var i = firstContentLine; i <= lastContentLine; i++) + { + var line = lines[i]; + if (string.IsNullOrWhiteSpace(line)) + continue; + + var indentation = 0; + while (indentation < line.Length && char.IsWhiteSpace(line[indentation])) + indentation++; + + commonIndentation = Math.Min(commonIndentation, indentation); + } + + if (commonIndentation == int.MaxValue) + commonIndentation = 0; + + for (var i = firstContentLine; i <= lastContentLine; i++) + { + var line = lines[i]; + if (string.IsNullOrWhiteSpace(line)) + { + lines[i] = string.Empty; + continue; + } + + lines[i] = line.Length <= commonIndentation + ? string.Empty + : line[commonIndentation..]; + } + + return string.Join('\n', lines[firstContentLine..(lastContentLine + 1)]); + } } \ No newline at end of file