Skip to content

Commit 8ce5ba0

Browse files
committed
Remove DisableHtmlWithExceptions and implement UseHardBreaks.
The `DisableHtmlWithExceptions` logic was removed and replaced with a simpler `UseHardBreaks` implementation for handling `<br>` tags. This refactor simplifies the markdown pipeline and improves maintainability while retaining support for hard breaks.
1 parent 9e89ea5 commit 8ce5ba0

File tree

5 files changed

+79
-85
lines changed

5 files changed

+79
-85
lines changed

src/Elastic.Markdown/Myst/Extensions/DisableHtmlWithExceptionsExtension.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Markdig;
6+
using Markdig.Helpers;
7+
using Markdig.Parsers;
8+
using Markdig.Parsers.Inlines;
9+
using Markdig.Renderers;
10+
using Markdig.Renderers.Html;
11+
using Markdig.Renderers.Html.Inlines;
12+
using Markdig.Syntax.Inlines;
13+
14+
namespace Elastic.Markdown.Myst.InlineParsers;
15+
16+
public static class HardBreakBuilderExtensions
17+
{
18+
public static MarkdownPipelineBuilder UseHardBreaks(this MarkdownPipelineBuilder pipeline)
19+
{
20+
pipeline.Extensions.AddIfNotAlready<HardBreakBuilderExtension>();
21+
return pipeline;
22+
}
23+
}
24+
25+
public class HardBreakBuilderExtension : IMarkdownExtension
26+
{
27+
public void Setup(MarkdownPipelineBuilder pipeline) =>
28+
pipeline.InlineParsers.InsertBefore<EmphasisInlineParser>(new HardBreakParser());
29+
30+
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) =>
31+
renderer.ObjectRenderers.InsertAfter<EmphasisInlineRenderer>(new HardBreakRenderer());
32+
}
33+
34+
public class HardBreakParser : InlineParser
35+
{
36+
public HardBreakParser() => OpeningCharacters = ['<'];
37+
38+
public override bool Match(InlineProcessor processor, ref StringSlice slice)
39+
{
40+
var span = slice.AsSpan();
41+
if (!span.StartsWith("<br"))
42+
return false;
43+
44+
var closingStart = span[3..].IndexOf('>');
45+
// we allow
46+
if (closingStart != 0)
47+
return false;
48+
49+
processor.Inline = new HardBreak();
50+
51+
var sliceEnd = slice.Start + 4; //<br + >
52+
while (slice.Start != sliceEnd)
53+
slice.SkipChar();
54+
55+
return true;
56+
}
57+
}
58+
59+
public class HardBreak : LeafInline;
60+
61+
public class HardBreakRenderer : HtmlObjectRenderer<HardBreak>
62+
{
63+
protected override void Write(HtmlRenderer renderer, HardBreak obj) =>
64+
renderer.Write("<br>");
65+
}

src/Elastic.Markdown/Myst/InlineParsers/RestrictedAutolinkInlineParser.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Elastic.Markdown/Myst/MarkdownParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Elastic.Markdown.Myst.CodeBlocks;
1010
using Elastic.Markdown.Myst.Comments;
1111
using Elastic.Markdown.Myst.Directives;
12-
using Elastic.Markdown.Myst.Extensions;
1312
using Elastic.Markdown.Myst.FrontMatter;
1413
using Elastic.Markdown.Myst.InlineParsers;
1514
using Elastic.Markdown.Myst.Substitution;
@@ -53,7 +52,8 @@ public class MarkdownParser(
5352
.UsePipeTables()
5453
.UseDirectives()
5554
.UseEnhancedCodeBlocks()
56-
.DisableHtmlWithExceptions(["<br>"])
55+
.DisableHtml()
56+
.UseHardBreaks()
5757
.Build();
5858

5959
public ConfigurationFile Configuration { get; } = configuration;

tests/Elastic.Markdown.Tests/Inline/RestrictedAutolinkinlineTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Elastic.Markdown.Tests.Inline;
99

10-
public class AllowBRTagTest(ITestOutputHelper output)
10+
public class AllowBrTagTest(ITestOutputHelper output)
1111
: InlineTest(output,
1212
"Hello,<br>World!")
1313
{
@@ -18,6 +18,17 @@ public void GeneratesHtml() =>
1818
);
1919
}
2020

21+
public class BrTagNeedsToBeExact(ITestOutputHelper output)
22+
: InlineTest(output,
23+
"Hello,<br >World<br />!")
24+
{
25+
[Fact]
26+
public void GeneratesHtml() =>
27+
Html.Should().Contain(
28+
"<p>Hello,&lt;br &gt;World&lt;br /&gt;!</p>"
29+
);
30+
}
31+
2132
public class DisallowSpanTag(ITestOutputHelper output)
2233
: InlineTest(output,
2334
"Hello,<span>World!</span>")

0 commit comments

Comments
 (0)