Skip to content

Commit

Permalink
Fix heading rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jan 24, 2024
1 parent 52b7841 commit 23d6f98
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/squidex/squidex</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>6.3.5</Version>
<Version>6.3.6</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
1 change: 0 additions & 1 deletion text/Squidex.Text.Tests/RichText/ComplexText.min.html

This file was deleted.

1 change: 0 additions & 1 deletion text/Squidex.Text.Tests/RichText/ComplexText.txt

This file was deleted.

11 changes: 5 additions & 6 deletions text/Squidex.Text.Tests/RichText/RichTextComplexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Squidex.RichText.Json;
using Squidex.Text.RichText.Model;
using Xunit;
using static Google.Rpc.Context.AttributeContext.Types;

namespace Squidex.Text.RichText;

Expand Down Expand Up @@ -235,13 +234,13 @@ public void Should_render_complex_state_from_json()
[Fact]
public void Should_render_from_files()
{
var inputJson = File.ReadAllText("RichText/ComplexText.json");
var inputJson = File.ReadAllText("RichText/TestCases/ComplexText/ComplexText.json");
var inputNode = JsonSerializer.Deserialize<Node>(inputJson)!;

RenderUtils.AssertNode(inputNode,
File.ReadAllText("RichText/ComplexText.md"),
File.ReadAllText("RichText/ComplexText.html"),
File.ReadAllText("RichText/ComplexText.min.html"),
File.ReadAllText("RichText/ComplexText.txt"));
File.ReadAllText("RichText/TestCases/ComplexText/ComplexText.md"),
File.ReadAllText("RichText/TestCases/ComplexText/ComplexText.html"),
File.ReadAllText("RichText/TestCases/ComplexText/ComplexText.min.html"),
File.ReadAllText("RichText/TestCases/ComplexText/ComplexText.txt"));
}
}
29 changes: 29 additions & 0 deletions text/Squidex.Text.Tests/RichText/RichTextRepeatedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Text.Json;
using Squidex.Text.RichText.Model;
using Xunit;

namespace Squidex.Text.RichText;

public class RichTextRepeatedTests
{
[Fact]

public void Should_render_from_files()
{
var inputJson = File.ReadAllText("RichText/TestCases/Repeated/Repeated.json");
var inputNode = JsonSerializer.Deserialize<Node>(inputJson)!;

RenderUtils.AssertNode(inputNode,
null,
null,
null,
File.ReadAllText("RichText/TestCases/Repeated/Repeated.txt"));
}
}
86 changes: 83 additions & 3 deletions text/Squidex.Text.Tests/RichText/RichTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void Should_render_link()
new Mark
{
Type = MarkType.Link,
Attributes = new Attributes()
Attributes = new Attributes
{
["href"] = "https://squidex.io",
["target"] = "_blank"
Expand Down Expand Up @@ -351,7 +351,7 @@ public void Should_render_image()
var source = new Node
{
Type = NodeType.Image,
Attributes = new Attributes()
Attributes = new Attributes
{
["src"] = "https://squidex.io/logo.png",
["alt"] = "Logo",
Expand All @@ -374,7 +374,7 @@ public void Should_render_blank_image()
var source = new Node
{
Type = NodeType.Image,
Attributes = new Attributes()
Attributes = new Attributes
{
["src"] = "https://squidex.io/logo.png"
}
Expand All @@ -389,6 +389,86 @@ public void Should_render_blank_image()
<img src=""https://squidex.io/logo.png"">");
}

[Fact]
public void Should_render_heading()
{
var source = new Node
{
Type = NodeType.Heading,
Content = [
new Node
{
Type = NodeType.Text,
Text = "Heading 1",
},
]
};

RenderUtils.AssertNode(source,
markdown: @"
# Heading 1",
html: @"
<h1>Heading 1</h1>",
minHtml: @"
<h1>Heading 1</h1>");
}

[Fact]
public void Should_render_heading1()
{
var source = new Node
{
Type = NodeType.Heading,
Attributes = new Attributes
{
["level"] = 1
},
Content = [
new Node
{
Type = NodeType.Text,
Text = "Heading 1",
},
]
};

RenderUtils.AssertNode(source,
markdown: @"
# Heading 1",
html: @"
<h1>Heading 1</h1>",
minHtml: @"
<h1>Heading 1</h1>");
}

[Fact]
public void Should_render_heading2()
{
var source = new Node
{
Type = NodeType.Heading,
Attributes = new Attributes
{
["level"] = 2
},
Content = [
new Node
{
Type = NodeType.Text,
Text = "Heading 2",
},
]
};

RenderUtils.AssertNode(source,
markdown: @"
## Heading 2",
html: @"
<h2>Heading 2</h2>",
minHtml: @"
<h2>Heading 2</h2>");
}

[Fact]
public void Should_render_max_length()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<h1>Header</h1>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u>, <code>code</code> and <span class="__editor_text-left">a class</span>.</p>
<blockquote>
<p>Quote</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,31 @@
"content": [
{
"type": "text",
"text": "Header"
"text": "Heading 1"
}
]
},
{
"type": "heading",
"attrs": {
"level": 2
},
"content": [
{
"type": "text",
"text": "Heading 2"
}
]
},
{
"type": "heading",
"attrs": {
"level": 3
},
"content": [
{
"type": "text",
"text": "Heading 3"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Header
# Heading 1

## Heading 2

### Heading 3

Content with **bold**, *italic*, underline, `code` and a class.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u>, <code>code</code> and <span class="__editor_text-left">a class</span>.</p><blockquote><p>Quote</p></blockquote><pre class="language-javascript"><code data-code-block-language="javascript">Code Block in Javascript</code></pre><p>Just another paragraph</p><hr><ul><li><p>Item 1</p></li><li><p>Item 2</p></li></ul><ol><li><p>Item A</p></li><li><p>Item B</p></li></ol><p><a href="https://cloud.squidex.io" rel="noopener noreferrer nofollow">A link</a></p><p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Adams_The_Tetons_and_the_Snake_River.jpg/1280px-Adams_The_Tetons_and_the_Snake_River.jpg" title="My Image"></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Heading 1 Heading 2 Heading 3 Content with bold, italic, underline, code and a class. Quote Code Block in Javascript Just another paragraph Item 1 Item 2 Item A Item B A link
50 changes: 50 additions & 0 deletions text/Squidex.Text.Tests/RichText/TestCases/Repeated/Repeated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World Hello World Hello World Hello World Hello World
16 changes: 11 additions & 5 deletions text/Squidex.Text.Tests/Squidex.Text.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@
</ItemGroup>

<ItemGroup>
<None Update="RichText\ComplexText.txt">
<None Update="RichText\TestCases\ComplexText\ComplexText.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\ComplexText.min.html">
<None Update="RichText\TestCases\ComplexText\ComplexText.min.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\ComplexText.html">
<None Update="RichText\TestCases\ComplexText\ComplexText.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\ComplexText.json">
<None Update="RichText\TestCases\ComplexText\ComplexText.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\ComplexText.md">
<None Update="RichText\TestCases\ComplexText\ComplexText.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\TestCases\Repeated\Repeated.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="RichText\TestCases\Repeated\Repeated.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\SvgBase64Use.svg">
Expand Down
8 changes: 5 additions & 3 deletions text/Squidex.Text/RichText/HtmlWriterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ private void WriteAttributes()

private static string GetHeading(int level)
{
if (level < 0 || level >= Headings.Length)
var index = level - 1;

if (level < 0 || index >= Headings.Length)
{
return Headings[^1];
return Headings[0];
}
else
{
return Headings[0];
return Headings[index];
}
}
}

0 comments on commit 23d6f98

Please sign in to comment.