-
Notifications
You must be signed in to change notification settings - Fork 156
Fix to preserve line break after headers issue #319 #321
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
Fix to preserve line break after headers issue #319 #321
Conversation
@vors There is a fair bit in this PR. Any questions please let me know. |
I would need some time to review that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, very solid approach and lots of tests, I like it a lot!
I left few comments about the code inline.
I also tried the scenario that I carried about the most: apply Update-MarkdownHelp
2 times and verify that second time didn't change anything.
This had a problem after the Example
header: keeps adding 2 new lines every time.
Repro: run Update-MarkdownHelp ./docs/
Affected file: docs/Merge-MarkdownHelp.md
return new SectionBody(text, formatOption); | ||
} | ||
|
||
public static implicit operator SectionBody(string text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid using implicit operator
it makes reading the code too obscure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
return Text; | ||
} | ||
|
||
public static SectionBody New(string text, SectionFormatOption formatOption = SectionFormatOption.None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just overloaded ctor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries. I'll do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -87,6 +87,17 @@ private static IEnumerable<XElement> GenerateParagraphs(string text) | |||
return Enumerable.Empty<XElement>(); | |||
} | |||
|
|||
private static IEnumerable<XElement> GenerateParagraphs(Markdown.MAML.Model.Markdown.SectionBody body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SectionBody.FormatOptions
is not used in this method. Should caller simple pass body to the existing method instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some occasions that ended up with a null SectionBody object. But using the null coalescing operator would probably be a better approach. Thanks, good comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -265,9 +267,17 @@ private string JoinWithComma(IEnumerable<string> args) | |||
return string.Join(", ", args); | |||
} | |||
|
|||
private bool ShouldBreak(SectionFormatOption formatOption) | |||
{ | |||
return ((formatOption & SectionFormatOption.LineBreakAfterHeader) == SectionFormatOption.LineBreakAfterHeader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider HasFlag
instead for readability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
{ | ||
param.FormatOption = strParam.FormatOption; | ||
} | ||
catch (Exception ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this can happen? It doesn't seem to protect against anything but strParam == null
case which is better to spell out explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good pickup. A null reference of strParam is really the only exception that is likely to be raised however we are already checking for that condition before the block is processed so additional handling is not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -47,6 +48,105 @@ public void ProduceMultilineDescription() | |||
Assert.Equal(3, description.Length); | |||
} | |||
|
|||
[Fact] | |||
public void PreserveMarkdownWhenUpdatingMarkdownHelp() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test seems homogeneous so it only tests the extra line case. Let's mix line-break and no-line-break in one test for coverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Updated test to also test for preserved formatting scenario used by Update-MarkdownHelp
.
var documentNode = MarkdownStringToDocumentNode($"## DESCRIPTION\r\n\r\n{expected}"); | ||
|
||
var actual = (documentNode.Children.FirstOrDefault(node => node.NodeType == MarkdownNodeType.Paragraph) as ParagraphNode).Spans.FirstOrDefault().Text; | ||
var hasLineBreak = (documentNode.Children.FirstOrDefault(node => node.NodeType == MarkdownNodeType.Heading) as HeadingNode).FormatOption == SectionFormatOption.LineBreakAfterHeader; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not readable, please break into few lines or subroutine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Assert.Equal(expected, actual); | ||
Assert.Equal(false, hasLineBreak); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tests! Great coverage! 👏
{ | ||
Type = "String", | ||
Name = "Path", | ||
FormatOption = SectionFormatOption.LineBreakAfterHeader, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mix two styles together in this test and avoid the second one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Assert.Equal(mamlCommand.Name, "Add-Member"); | ||
Assert.Equal(mamlCommand.Synopsis, "Adds custom properties and methods to an instance of a Windows PowerShell object."); | ||
Assert.Equal("Add-Member", mamlCommand.Name); | ||
Assert.Equal("Adds custom properties and methods to an instance of a Windows PowerShell object.", mamlCommand.Synopsis.Text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing these parameter order!
I made a mistake early in development and copy-pasted it everywhere
Also, please don't rewrite history in this branch and just make commits on top, that would make subsequent reviews much easier. |
@vors Thanks. Unless I'm missing it, testing Fairly sure I see the issue though, so I'll add it to the PR and update tests to catch it in the future. |
Oh, you are so right! That's pretty bad. If you can fix it as part of this PR that would be wonderful. |
All good. Update included. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -269,7 +269,8 @@ private string JoinWithComma(IEnumerable<string> args) | |||
|
|||
private bool ShouldBreak(SectionFormatOption formatOption) | |||
{ | |||
return ((formatOption & SectionFormatOption.LineBreakAfterHeader) == SectionFormatOption.LineBreakAfterHeader); | |||
// If the line break flag is set return true. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the code is pretty self explanatory, no need to this comment
@@ -538,6 +539,12 @@ private void AddParagraphs(string body, bool noNewLines = false) | |||
body = GetAutoWrappingForMarkdown(paragraphs.Select(para => GetEscapedMarkdownText(para.Trim())).ToArray()); | |||
} | |||
|
|||
// The the body already ended in a line break don't add extra lines on to the end | |||
if (body.EndsWith("\r\n\r\n")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
Report($" Exception {param.Name} FormatOption merge: \r\n{ex.Message}\r\n"); | ||
_cmdletUpdated = true; | ||
} | ||
// Update the parameter with the merged in FormatOption |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: no need for this comment
Fix #319