mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-06-27 19:16:27 +00:00
Normalize markdown indentation
This commit is contained in:
parent
fb685a44a5
commit
56805874c2
@ -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,
|
||||||
|
|||||||
@ -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)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user