Skip to content

Add line breaks in MAML for examples content #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Command/ExportMamlCommandHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ protected override void EndProcessing()
}
else
{
WriteObject(this.InvokeProvider.Item.Get(MamlConversionHelper.WriteToFile(helpInfos, moduleMamlPath, Encoding).FullName));
var mamlFile = MamlConversionHelper.WriteToFile(helpInfos, moduleMamlPath, Encoding);

// Read the MAML file and replace the specific line
string mamlContent = File.ReadAllText(mamlFile.FullName, Encoding);
// Replace the line break placeholder with a proper line break
// This is a workaround for the issue where line breaks are not preserved in MAML files
string updatedContent = mamlContent.Replace("<maml:para>__REMOVE_ME_LINE_BREAK__</maml:para>", "<maml:para>&#x20;&#x08;</maml:para>");
File.WriteAllText(mamlFile.FullName, updatedContent, Encoding);
WriteObject(this.InvokeProvider.Item.Get(mamlFile.FullName));
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/MamlWriter/MamlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,25 @@ private static Parameter ConvertParameter(Model.Parameter parameter)

private static CommandExample ConvertExample(Example example, int exampleNumber)
{
var tempDescription = new List<string>();
var newExample = new CommandExample();
newExample.Title = string.Format($"--------- {example.Title} ---------");
foreach(string s in example.Remarks.Split(new string[] { "\n\n" }, StringSplitOptions.None))
{
newExample.Description.Add(s.Trim());
tempDescription.Add(s.Trim());
}

for (int i = 0; i < tempDescription.Count; i++)
{
newExample.Description.Add(tempDescription[i]);

// Add an empty line after each item except the last
if (i < tempDescription.Count - 1)
{
newExample.Description.Add("__REMOVE_ME_LINE_BREAK__"); // Non-breaking space for empty line
}
}

return newExample;
}

Expand Down
6 changes: 6 additions & 0 deletions test/Pester/ExportMamlCommandHelp.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,11 @@ Describe "Export-MamlCommandHelp tests" {
$observed = $xml2.SelectNodes('//command:command', $ns2).Where({$_.details.name -eq "Get-Date"}).examples.example.title
$observed | Should -Be $expected
}

It "Should have the line break workaround in the example code" {
$m = Import-MarkdownCommandHelp -Path (Join-Path $assetDir 'get-date.md')
$mamlFie = $m | Export-MamlCommandHelp -OutputFolder $outputDirectory -Force
$mamlFie | Should -FileContentMatch '<maml:para>&#x20;&#x08;</maml:para>'
}
}
}