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
5 changes: 5 additions & 0 deletions site/docs/extensions/emphasis-extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ This is ==highlighted== text.

This is ==highlighted== text.

An unmatched remainder of a closing delimiter run must not pull subsequent text
into the emphasis. For example, `===a===== tail` renders as
`=<mark>a===</mark> tail`. This example also round-trips unchanged with
`EnableTrackTrivia()` and `RoundtripRenderer`.

## HTML output

{.table}
Expand Down
83 changes: 83 additions & 0 deletions src/Markdig.Tests/TestEmphasisRoundtrip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Renderers.Roundtrip;
using Markdig.Syntax;

namespace Markdig.Tests;

[TestFixture]
public class TestEmphasisRoundtrip
{
private static string RoundTrip(string markdown, MarkdownPipeline pipeline)
{
MarkdownDocument document = Markdown.Parse(markdown, pipeline);
var writer = new StringWriter();
var renderer = new RoundtripRenderer(writer);
pipeline.Setup(renderer);
renderer.Write(document);
return writer.ToString();
}

// Leftover delimiter characters below the minimum count used to stay nested
// inside the emphasis they closed, moving the following text to the end of
// the block. https://github.com/xoofx/markdig/issues/743
[Test]
public void GridTableSeparatorRoundtripsWithTrackTrivia()
{
string markdown =
"+---------------+---------------+--------------------+\n" +
"| Fruit | Price | Advantages |\n" +
"+===============+===============+====================+\n" +
"| Bananas | first line | first line |\n" +
"| | next line | next line |\n" +
"+---------------+---------------+--------------------+\n";

var pipeline = new MarkdownPipelineBuilder()
.UseAutoLinks()
.UseEmphasisExtras()
.UseListExtras()
.EnableTrackTrivia()
.Build();

Assert.That(RoundTrip(markdown, pipeline), Is.EqualTo(markdown));
}

// Same defect, minimal: leftovers (15 = 7 * 2 + 1) below the "==" minimum.
[Test]
public void UnbalancedMarkedRunsRoundtrip()
{
string markdown = "+===============+===============+\n";

var pipeline = new MarkdownPipelineBuilder()
.UseEmphasisExtras()
.EnableTrackTrivia()
.Build();

Assert.That(RoundTrip(markdown, pipeline), Is.EqualTo(markdown));
}

[TestCase("===a===== tail\n", "<p>=<mark>a===</mark> tail</p>")]
[TestCase("===a==== tail\n", "<p>=<mark>a==</mark> tail</p>")]
[TestCase("=====a======= tail\n", "<p>=<mark><mark>a===</mark></mark> tail</p>")]
[TestCase("+++a+++++ tail\n", "<p>+<ins>a+++</ins> tail</p>")]
[TestCase("*===a===== tail*\n", "<p><em>=<mark>a===</mark> tail</em></p>")]
[TestCase("===a===== tail ==next==\n", "<p>=<mark>a===</mark> tail <mark>next</mark></p>")]
public void UnmatchedCloserPreservesTrailingContent(string markdown, string expectedHtml)
{
var pipeline = new MarkdownPipelineBuilder().UseEmphasisExtras().EnableTrackTrivia().Build();

Assert.That(RoundTrip(markdown, pipeline), Is.EqualTo(markdown));
Assert.That(Markdown.ToHtml(markdown, pipeline).Trim(), Is.EqualTo(expectedHtml));
Assert.That(Markdown.ToHtml(markdown, new MarkdownPipelineBuilder().UseEmphasisExtras().Build()).Trim(), Is.EqualTo(expectedHtml));
}

// The fix must not prevent balanced "marked" emphasis from being detected.
[Test]
public void BalancedMarkedEmphasisStillParses()
{
var pipeline = new MarkdownPipelineBuilder().UseEmphasisExtras().Build();
Assert.That(Markdown.ToHtml("==bold==", pipeline).Trim(), Is.EqualTo("<p><mark>bold</mark></p>"));
}
}
27 changes: 21 additions & 6 deletions src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,32 @@ private void ProcessEmphasis(InlineProcessor processor, List<EmphasisDelimiterIn
i--;
}
}
else if ((closeDelimiter.Type & DelimiterType.Open) == 0)
else
{
closeDelimiter.ReplaceBy(closeDelimiter.AsLiteralInline());
delimiters.RemoveAt(i);
i--;
// Keep unmatched closers attached until their trailing content
// has been moved out below.
break;
}
else
}

// Leftover delimiter characters keep the following inlines nested inside
// the emphasis; move them out so they keep their original position.
if (closeDelimiter.DelimiterCount > 0 && closeDelimiter.Parent is EmphasisInline closerParentEmphasis)
{
var outermostEmphasis = closerParentEmphasis;
while (outermostEmphasis.Parent is EmphasisInline ancestorEmphasis)
{
break;
outermostEmphasis = ancestorEmphasis;
}

closeDelimiter.MoveChildrenAfter(outermostEmphasis);
}

if (closeDelimiter.DelimiterCount > 0 && (closeDelimiter.Type & DelimiterType.Open) == 0)
{
closeDelimiter.ReplaceBy(closeDelimiter.AsLiteralInline());
delimiters.RemoveAt(i);
i--;
}
}
}
Expand Down
Loading