Skip to content

Commit 801a4da

Browse files
show invalid table markdown (#276)
Co-authored-by: mihails.kuzmins <mihails.kuzmins@daytongroup.lv>
1 parent 52cacf3 commit 801a4da

File tree

6 files changed

+170
-22
lines changed

6 files changed

+170
-22
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Markdig.Syntax;
2+
3+
namespace MudBlazor;
4+
5+
internal static class SourceSpanEx
6+
{
7+
public static string TryGetText(this SourceSpan @this, in string originalText)
8+
{
9+
if (@this.Start >= originalText.Length)
10+
return string.Empty;
11+
12+
var length = Math.Min(@this.Length, originalText.Length - @this.Start);
13+
return originalText.Substring(@this.Start, length);
14+
}
15+
}

src/MudBlazor.Markdown/MudMarkdown.razor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ protected virtual void RenderInlines(ContainerInline inlines, RenderTreeBuilder
324324
builder.CloseComponent();
325325
break;
326326
}
327+
case PipeTableDelimiterInline x:
328+
{
329+
// It usually indicates that there are some issues with table markdown
330+
var markdownValue = x.Parent?.ParentBlock?.Span.TryGetText(Value);
331+
TryRenderMarkdownError(markdownValue, builder);
332+
333+
break;
334+
}
327335
default:
328336
{
329337
OnRenderInlinesDefault(inline, builder);
@@ -333,6 +341,17 @@ protected virtual void RenderInlines(ContainerInline inlines, RenderTreeBuilder
333341
}
334342
}
335343

344+
protected virtual void TryRenderMarkdownError(string? text, RenderTreeBuilder builder)
345+
{
346+
if (string.IsNullOrEmpty(text))
347+
return;
348+
349+
builder.OpenElement(ElementIndex++, "div");
350+
builder.AddAttribute(ElementIndex++, "class", "markdown-error");
351+
builder.AddContent(ElementIndex++, text);
352+
builder.CloseElement();
353+
}
354+
336355
/// <summary>
337356
/// Renders inline block which is not covered by the switch-case block in <see cref="RenderInlines"/>
338357
/// </summary>

src/MudBlazor.Markdown/Resources/MudBlazor.Markdown.css

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ pre code.hljs {
1717
position: relative !important;
1818
}
1919

20-
.mud-markdown-body .snippet-clipboard-content:hover > .snippet-clipboard-copy-icon {
21-
display: block !important;
22-
}
20+
.mud-markdown-body .snippet-clipboard-content:hover > .snippet-clipboard-copy-icon {
21+
display: block !important;
22+
}
2323

24-
.mud-markdown-body .snippet-clipboard-content .snippet-clipboard-copy-icon {
25-
position: absolute;
26-
display: none;
27-
top: 0;
28-
right: 0;
29-
}
24+
.mud-markdown-body .snippet-clipboard-content .snippet-clipboard-copy-icon {
25+
position: absolute;
26+
display: none;
27+
top: 0;
28+
right: 0;
29+
}
3030

3131
/* Quote */
3232
.mud-markdown-body blockquote {
@@ -37,9 +37,9 @@ pre code.hljs {
3737
margin: 0.5em 0 1.25em;
3838
}
3939

40-
.mud-markdown-body blockquote p {
41-
margin-bottom: 0 !important;
42-
}
40+
.mud-markdown-body blockquote p {
41+
margin-bottom: 0 !important;
42+
}
4343

4444
/* Table */
4545
.mud-markdown-body table {
@@ -61,15 +61,15 @@ pre code.hljs {
6161
margin-bottom: 1.25em !important;
6262
}
6363

64-
.mud-markdown-body ul ul {
65-
list-style-type: circle;
66-
margin-bottom: 0 !important;
67-
}
64+
.mud-markdown-body ul ul {
65+
list-style-type: circle;
66+
margin-bottom: 0 !important;
67+
}
6868

69-
.mud-markdown-body ul ul ul {
70-
list-style-type: square;
71-
margin-bottom: 0 !important;
72-
}
69+
.mud-markdown-body ul ul ul {
70+
list-style-type: square;
71+
margin-bottom: 0 !important;
72+
}
7373

7474
.mud-markdown-body li {
7575
display: list-item !important;
@@ -104,6 +104,10 @@ pre code.hljs {
104104
margin-bottom: 0 !important;
105105
}
106106

107+
.mud-markdown-body p .markdown-error {
108+
white-space: pre;
109+
}
110+
107111
/* Images */
108112
.mud-markdown-body img {
109113
max-width: 100%;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace MudBlazor.Markdown.Tests.Extensions.SourceSpanEx;
2+
3+
public sealed class TryGetTextShould
4+
{
5+
[Fact]
6+
public void ReturnEmptyIfStartGreaterThanString()
7+
{
8+
const string value = nameof(value);
9+
var fixture = new SourceSpan(value.Length, value.Length);
10+
11+
var result = fixture.TryGetText(value);
12+
13+
result
14+
.Should()
15+
.BeEmpty();
16+
}
17+
18+
[Fact]
19+
public void ReturnSubstringIfLengthTooGreat()
20+
{
21+
const string value = nameof(value), expected = "e";
22+
var fixture = new SourceSpan(value.Length - 1, value.Length);
23+
24+
var result = fixture.TryGetText(value);
25+
26+
result
27+
.Should()
28+
.Be(expected);
29+
}
30+
31+
[Fact]
32+
public void ReturnStringWithin()
33+
{
34+
const string value = nameof(value), expected = "alu";
35+
var fixture = new SourceSpan(1, 3);
36+
37+
var result = fixture.TryGetText(value);
38+
39+
result
40+
.Should()
41+
.Be(expected);
42+
}
43+
44+
[Fact]
45+
public void ReturnStringStartsWith()
46+
{
47+
const string value = nameof(value), expected = "valu";
48+
var fixture = new SourceSpan(0, 3);
49+
50+
var result = fixture.TryGetText(value);
51+
52+
result
53+
.Should()
54+
.Be(expected);
55+
}
56+
57+
[Fact]
58+
public void ReturnStringEndsWith()
59+
{
60+
const string value = nameof(value), expected = "alue";
61+
var fixture = new SourceSpan(1, value.Length - 1);
62+
63+
var result = fixture.TryGetText(value);
64+
65+
result
66+
.Should()
67+
.Be(expected);
68+
}
69+
}

tests/MudBlazor.Markdown.Tests/MarkdownComponentTests/MarkdownComponentCasesShould.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,45 @@ public void RenderListWithWithCompositeListItems()
549549
}
550550

551551
#endregion
552+
553+
#region https://github.com/MyNihongo/MudBlazor.Markdown/issues/274
554+
555+
[Fact]
556+
public void RenderTableWithWeirdFormat()
557+
{
558+
const string value =
559+
"### My Table: \n\n" +
560+
"| **Column 1** | **Column 2** | **Column 3** |\n" +
561+
"|--------------|----------------|-----------------------------|\n" +
562+
"| Row 1, Col 1 | Row 1, Col 2 | |\n" +
563+
"| | Row 2, Col 2 | Row 2, Col 3 |\n" +
564+
"| Row 3, Col 1 | | \u0060\u0060\u0060python |\n" +
565+
"| | Row 4, Col 2 | def greet(name): |\n" +
566+
"| Row 5, Col 1 | | return name |\n" +
567+
"| | | \u0060\u0060\u0060 |";
568+
569+
const string expected =
570+
"""
571+
<article class='mud-markdown-body'>
572+
<h3 id='my-table' class='mud-typography mud-typography-h3'>My Table:</h3>
573+
<p class='mud-typography mud-typography-body1'>
574+
<div class='markdown-error'>
575+
| **Column 1** | **Column 2** | **Column 3** |
576+
|--------------|----------------|-----------------------------|
577+
| Row 1, Col 1 | Row 1, Col 2 | |
578+
| | Row 2, Col 2 | Row 2, Col 3 |
579+
| Row 3, Col 1 | | ```python |
580+
| | Row 4, Col 2 | def greet(name): |
581+
| Row 5, Col 1 | | return name |
582+
| | | ``` |
583+
</div>
584+
</p>
585+
</article>
586+
""";
587+
588+
using var fixture = CreateFixture(value);
589+
fixture.MarkupMatches(expected);
590+
}
591+
592+
#endregion
552593
}

tests/MudBlazor.Markdown.Tests/MudBlazor.Markdown.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="bunit" Version="1.31.3" />
12-
<PackageReference Include="FluentAssertions" Version="6.12.0" />
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
12+
<PackageReference Include="FluentAssertions" Version="6.12.1" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
1414
<PackageReference Include="MoqMicrosoftConfiguration" Version="1.0.5" />
1515
<PackageReference Include="MyNihongo.Option" Version="2.2.0" />
1616
<PackageReference Include="xunit" Version="2.9.0" />

0 commit comments

Comments
 (0)