Skip to content
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 null reference when markdown content is empty #7463

Merged
merged 4 commits into from
Aug 27, 2018
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
30 changes: 17 additions & 13 deletions src/Microsoft.PowerShell.MarkdownRender/FencedCodeBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using Markdig;
using Markdig.Helpers;
using Markdig.Renderers;
using Markdig.Syntax;

Expand All @@ -16,25 +17,28 @@ internal class FencedCodeBlockRenderer : VT100ObjectRenderer<FencedCodeBlock>
{
protected override void Write(VT100Renderer renderer, FencedCodeBlock obj)
{
foreach (var codeLine in obj.Lines.Lines)
if (obj?.Lines.Lines != null)
{
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
foreach (StringLine codeLine in obj.Lines.Lines)
{
// If the code block is of type YAML, then tab to right to improve readability.
// This specifically helps for parameters help content.
if (string.Equals(obj.Info, "yaml", StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
{
renderer.Write("\t").WriteLine(codeLine.ToString());
}
else
{
renderer.WriteLine(renderer.EscapeSequences.FormatCode(codeLine.ToString(), isInline: false));
// If the code block is of type YAML, then tab to right to improve readability.
// This specifically helps for parameters help content.
if (string.Equals(obj.Info, "yaml", StringComparison.OrdinalIgnoreCase))
{
renderer.Write("\t").WriteLine(codeLine.ToString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

We call codeLine.ToString()) twice above - should we use a variable?
And in line 34 too.

Copy link
Member Author

Choose a reason for hiding this comment

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

One is in if and other is in else, so only one of them is called. And this change is out of scope for this PR.

}
else
{
renderer.WriteLine(renderer.EscapeSequences.FormatCode(codeLine.ToString(), isInline: false));
}
}
}
}

// Add a blank line after the code block for better readability.
renderer.WriteLine();
// Add a blank line after the code block for better readability.
renderer.WriteLine();
}
}
}
}
67 changes: 36 additions & 31 deletions src/Microsoft.PowerShell.MarkdownRender/HeaderBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,43 @@ internal class HeaderBlockRenderer : VT100ObjectRenderer<HeadingBlock>
{
protected override void Write(VT100Renderer renderer, HeadingBlock obj)
{
// Format header and then add blank line to improve readability.
switch (obj.Level)
string headerText = obj?.Inline?.FirstChild?.ToString();

if (!string.IsNullOrEmpty(headerText))
{
case 1:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader1(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;

case 2:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader2(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;

case 3:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader3(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;

case 4:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader4(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;

case 5:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader5(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;

case 6:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader6(obj.Inline.FirstChild.ToString()));
renderer.WriteLine();
break;
// Format header and then add blank line to improve readability.
switch (obj.Level)
{
case 1:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader1(headerText));
renderer.WriteLine();
break;

case 2:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader2(headerText));
renderer.WriteLine();
break;

case 3:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader3(headerText));
renderer.WriteLine();
break;

case 4:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader4(headerText));
renderer.WriteLine();
break;

case 5:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader5(headerText));
renderer.WriteLine();
break;

case 6:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader6(headerText));
renderer.WriteLine();
break;
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.PowerShell.MarkdownRender/LinkInlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ internal class LinkInlineRenderer : VT100ObjectRenderer<LinkInline>
{
protected override void Write(VT100Renderer renderer, LinkInline obj)
{
string text = obj.FirstChild?.ToString();

// Format link as image or link.
if (obj.IsImage)
{
renderer.Write(renderer.EscapeSequences.FormatImage(obj.FirstChild.ToString()));
renderer.Write(renderer.EscapeSequences.FormatImage(text));
}
else
{
renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild.ToString(), obj.Url));
renderer.Write(renderer.EscapeSequences.FormatLink(text, obj.Url));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ Describe 'ConvertFrom-Markdown tests' -Tags 'CI' {

Context 'Basic tests' {
BeforeAll {
$esc = [char]0x1b
$mdFile = New-Item -Path $TestDrive/input.md -Value "Some **test string** to write in a file" -Force
$mdLiteralPath = New-Item -Path $TestDrive/LiteralPath.md -Value "Some **test string** to write in a file" -Force
$expectedStringFromFile = "Some $esc[1mtest string$esc[0m to write in a file`n`n"
Expand Down Expand Up @@ -280,6 +279,34 @@ bool function()`n{`n}
}
}

Context "ConvertFrom-Markdown empty or null content tests" {
BeforeAll {
$codeBlock = @'
```CSharp
```
'@

$testCases = @(
@{Type = "CodeBlock"; Markdown = "$codeBlock"; ExpectedOutput = ''}
@{Type = "Header1"; Markdown = "# "; ExpectedOutput = ''}
@{Type = "Header2"; Markdown = "## "; ExpectedOutput = ''}
@{Type = "Header3"; Markdown = "### "; ExpectedOutput = ''}
@{Type = "Header4"; Markdown = "#### "; ExpectedOutput = ''}
@{Type = "Header5"; Markdown = "##### "; ExpectedOutput = ''}
@{Type = "Header6"; Markdown = "###### "; ExpectedOutput = ''}
@{Type = "Image"; Markdown = "'![]()'"; ExpectedOutput = "'$esc[33m[Image]$esc[0m'"}
@{Type = "Link"; Markdown = "'[]()'"; ExpectedOutput = "'$esc[4;38;5;117m`"`"$esc[0m'"}
)
}

It "No error if thrown when empty content is provided for mardown element : <Type>" -TestCases $testCases {
param($Type, $Markdown, $ExpectedOutput)

$resultObj = ConvertFrom-Markdown -InputObject $Markdown -AsVT100EncodedString
$resultObj.VT100EncodedString.Trim() | Should -BeExactly $ExpectedOutput
}
}

Context "Get/Set-MarkdownOption tests" {

BeforeAll {
Expand Down