Fix for table depth error when cell contains backticks (#891)

* failing test

* fixed bug with table containing back tick which causes depth error
This commit is contained in:
Phillip Haydon
2025-09-22 02:26:02 +12:00
committed by GitHub
parent 1b04599c44
commit 0e9e80e1cd
3 changed files with 73 additions and 1 deletions

View File

@@ -57,4 +57,48 @@ public sealed class TestPipeTable
Assert.AreEqual(0, column.Width);
}
}
[Test]
public void TableWithUnbalancedCodeSpanParsesWithoutDepthLimitError()
{
const string markdown = """
| Count | A | B | C | D | E |
|-------|---|---|---|---|---|
| 0 | B | C | D | E | F |
| 1 | B | `C | D | E | F |
| 2 | B | `C | D | E | F |
| 3 | B | C | D | E | F |
| 4 | B | C | D | E | F |
| 5 | B | C | D | E | F |
| 6 | B | C | D | E | F |
| 7 | B | C | D | E | F |
| 8 | B | C | D | E | F |
| 9 | B | C | D | E | F |
| 10 | B | C | D | E | F |
| 11 | B | C | D | E | F |
| 12 | B | C | D | E | F |
| 13 | B | C | D | E | F |
| 14 | B | C | D | E | F |
| 15 | B | C | D | E | F |
| 16 | B | C | D | E | F |
| 17 | B | C | D | E | F |
| 18 | B | C | D | E | F |
| 19 | B | C | D | E | F |
""";
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.Build();
MarkdownDocument document = null!;
Assert.DoesNotThrow(() => document = Markdown.Parse(markdown, pipeline));
var tables = document.Descendants().OfType<Table>().ToArray();
Assert.That(tables, Has.Length.EqualTo(1));
string html = string.Empty;
Assert.DoesNotThrow(() => html = Markdown.ToHtml(markdown, pipeline));
Assert.That(html, Does.Contain("<table"));
Assert.That(html, Does.Contain("<td>`C</td>"));
}
}

View File

@@ -196,6 +196,16 @@ public class PipeTableParser : InlineParser, IPostInlineProcessor
// Continue
if (tableState is null || container is null || tableState.IsInvalidTable || !tableState.LineHasPipe ) //|| tableState.LineIndex != state.LocalLineIndex)
{
if (tableState is not null)
{
foreach (var inline in tableState.ColumnAndLineDelimiters)
{
if (inline is PipeTableDelimiterInline pipeDelimiter)
{
pipeDelimiter.ReplaceByLiteral();
}
}
}
return true;
}

View File

@@ -35,6 +35,7 @@ public class CodeInlineParser : InlineParser
Debug.Assert(match is not ('\r' or '\n'));
// Match the opened sticks
int openingStart = slice.Start;
int openSticks = slice.CountAndSkipChar(match);
// A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick.
@@ -75,8 +76,25 @@ public class CodeInlineParser : InlineParser
{
break;
}
else if (closeSticks == 0)
if (closeSticks == 0)
{
ReadOnlySpan<char> lookAhead = span.Length > 1 ? span.Slice(1) : ReadOnlySpan<char>.Empty;
while (!lookAhead.IsEmpty && (lookAhead[0] == '\r' || lookAhead[0] == '\n'))
{
lookAhead = lookAhead.Slice(1);
}
int whitespace = 0;
while (whitespace < lookAhead.Length && (lookAhead[whitespace] == ' ' || lookAhead[whitespace] == '\t'))
{
whitespace++;
}
if (whitespace < lookAhead.Length && lookAhead[whitespace] == '|')
{
slice.Start = openingStart;
return false;
}
containsNewLines = true;
span = span.Slice(1);
}