Skip to content
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
65 changes: 65 additions & 0 deletions src/Elastic.Markdown/Myst/InlineParsers/HardBreakParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Markdig;
using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Parsers.Inlines;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Renderers.Html.Inlines;
using Markdig.Syntax.Inlines;

namespace Elastic.Markdown.Myst.InlineParsers;

public static class HardBreakBuilderExtensions
{
public static MarkdownPipelineBuilder UseHardBreaks(this MarkdownPipelineBuilder pipeline)
{
pipeline.Extensions.AddIfNotAlready<HardBreakBuilderExtension>();
return pipeline;
}
}

public class HardBreakBuilderExtension : IMarkdownExtension
{
public void Setup(MarkdownPipelineBuilder pipeline) =>
pipeline.InlineParsers.InsertBefore<EmphasisInlineParser>(new HardBreakParser());

public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) =>
renderer.ObjectRenderers.InsertAfter<EmphasisInlineRenderer>(new HardBreakRenderer());
}

public class HardBreakParser : InlineParser
{
public HardBreakParser() => OpeningCharacters = ['<'];

public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
var span = slice.AsSpan();
if (!span.StartsWith("<br"))
return false;

var closingStart = span[3..].IndexOf('>');
// we allow
if (closingStart != 0)
return false;

processor.Inline = new HardBreak();

var sliceEnd = slice.Start + 4; //<br + >
while (slice.Start != sliceEnd)
slice.SkipChar();

return true;
}
}

public class HardBreak : LeafInline;

public class HardBreakRenderer : HtmlObjectRenderer<HardBreak>
{
protected override void Write(HtmlRenderer renderer, HardBreak obj) =>
renderer.Write("<br>");
}
3 changes: 1 addition & 2 deletions src/Elastic.Markdown/Myst/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MarkdownParser(
.UseDirectives()
.UseEnhancedCodeBlocks()
.DisableHtml()
.UseHardBreaks()
.Build();

public ConfigurationFile Configuration { get; } = configuration;
Expand Down Expand Up @@ -107,6 +108,4 @@ public MarkdownDocument Parse(string yaml, IFileInfo parent, YamlFrontMatter? ma
var markdownDocument = Markdig.Markdown.Parse(yaml, Pipeline, context);
return markdownDocument;
}


}
42 changes: 42 additions & 0 deletions tests/Elastic.Markdown.Tests/Inline/HardBreakTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using FluentAssertions;
using Xunit.Abstractions;

namespace Elastic.Markdown.Tests.Inline;

public class AllowBrTagTest(ITestOutputHelper output)
: InlineTest(output,
"Hello,<br>World!")
{
[Fact]
public void GeneratesHtml() =>
Html.Should().Contain(
"<p>Hello,<br>World!</p>"
);
}

public class BrTagNeedsToBeExact(ITestOutputHelper output)
: InlineTest(output,
"Hello,<br >World<br />!")
{
[Fact]
public void GeneratesHtml() =>
Html.Should().Contain(
"<p>Hello,&lt;br &gt;World&lt;br /&gt;!</p>"
);
}

public class DisallowSpanTag(ITestOutputHelper output)
: InlineTest(output,
"Hello,<span>World!</span>")
{
[Fact]
// span tag is rendered as text
public void GeneratesHtml() =>
Html.Should().Contain(
"<p>Hello,&lt;span&gt;World!&lt;/span&gt;</p>"
);
}
Loading