Skip to content

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

Merged
merged 5 commits into from
Dec 8, 2017

Conversation

BernieWhite
Copy link
Contributor

  • Updates to markdown parser and renderer to preserve line breaks after headers. Multiple line breaks are ignored.
  • Extra line break is removed when related links does not contain any links.
  • Test cases updated to test for preserving line breaks and when line breaks are absent.

Fix #319

@BernieWhite
Copy link
Contributor Author

@vors There is a fair bit in this PR. Any questions please let me know.

@vors
Copy link
Collaborator

vors commented Dec 5, 2017

I would need some time to review that

Copy link
Collaborator

@vors vors left a 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)
Copy link
Collaborator

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

Copy link
Contributor Author

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)
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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);
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Contributor Author

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)
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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()
Copy link
Collaborator

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.

Copy link
Contributor Author

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;
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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,
Copy link
Collaborator

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?

Copy link
Contributor Author

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);
Copy link
Collaborator

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

@vors
Copy link
Collaborator

vors commented Dec 6, 2017

Also, please don't rewrite history in this branch and just make commits on top, that would make subsequent reviews much easier.

@BernieWhite
Copy link
Contributor Author

@vors Thanks. Unless I'm missing it, testing Update-MarkdownHelp using v0.8.3 still seems update Merge-MarkdownHelp.md with those extra lines.

Fairly sure I see the issue though, so I'll add it to the PR and update tests to catch it in the future.

@vors
Copy link
Collaborator

vors commented Dec 7, 2017

Oh, you are so right! That's pretty bad. If you can fix it as part of this PR that would be wonderful.

@BernieWhite
Copy link
Contributor Author

All good. Update included.

@vors vors merged commit 16a5f68 into PowerShell:master Dec 8, 2017
Copy link
Collaborator

@vors vors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@@ -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.
Copy link
Collaborator

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"))
Copy link
Collaborator

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
Copy link
Collaborator

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

@BernieWhite BernieWhite deleted the bewhite/blank-line-issue-319 branch December 12, 2017 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants