Normalize markdown indentation

This commit is contained in:
Thorsten Sommer 2026-04-09 15:15:48 +02:00
parent fb685a44a5
commit 56805874c2
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
2 changed files with 55 additions and 1 deletions

View File

@ -80,12 +80,13 @@ public sealed record DataMandatoryInfo
return false; return false;
} }
var normalizedMarkdown = AIStudio.Tools.Markdown.RemoveSharedIndentation(markdown);
mandatoryInfo = new DataMandatoryInfo mandatoryInfo = new DataMandatoryInfo
{ {
Id = id.ToString(), Id = id.ToString(),
Title = title, Title = title,
VersionText = versionText, VersionText = versionText,
Markdown = markdown, Markdown = normalizedMarkdown,
AcceptButtonText = acceptButtonText, AcceptButtonText = acceptButtonText,
RejectButtonText = rejectButtonText, RejectButtonText = rejectButtonText,
EnterpriseConfigurationPluginId = configPluginId, EnterpriseConfigurationPluginId = configPluginId,

View File

@ -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)]);
}
} }