[PR #905] [MERGED] Fix pipe table parsing with a leading paragraph #1296

Open
opened 2026-01-29 14:52:45 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/xoofx/markdig/pull/905
Author: @MihaZupan
Created: 10/20/2025
Status: Merged
Merged: 10/20/2025
Merged by: @xoofx

Base: masterHead: table-para


📝 Commits (2)

  • 6f84428 Fix pipe table parsing with a leading paragraph
  • cf79677 Use the alternative approach

📊 Changes

6 files changed (+145 additions, -14 deletions)

View changed files

📝 src/Markdig.Tests/Specs/GridTableSpecs.generated.cs (+29 -0)
📝 src/Markdig.Tests/Specs/GridTableSpecs.md (+21 -0)
📝 src/Markdig.Tests/Specs/PipeTableSpecs.generated.cs (+33 -0)
📝 src/Markdig.Tests/Specs/PipeTableSpecs.md (+25 -0)
📝 src/Markdig.Tests/TestPipeTable.cs (+1 -0)
📝 src/Markdig/Extensions/Tables/PipeTableParser.cs (+36 -14)

📄 Description

Closes #818
Addresses issues referenced in https://github.com/xoofx/markdig/pull/885#issuecomment-3419948799, https://github.com/xoofx/markdig/pull/885#issuecomment-3419969194

This PR effectively reverts #885 as that change was swallowing whatever information was there in the paragraph before the table.
E.g. for

Some text
| A |
|---|
| B |

"Some text" would be lost.

Instead I relaxed the table validation to no longer enforce starting on the first line (localLineIndex > 0 and deltaLine > 0 checks), and also save the current block in the AST before we replace it with a table.

Given this is happening while we're parsing inlines, I don't think we can easily insert an extra block at the current layer, so I instead added the leading paragrah as the first child of the table, and the first element is special-cased in the renderer.
That is, instead of

document
  paragraph
    some text
  table
    A ...

we're instead producing

document
  table
    paragraph
      some text
    A ...

Since TableRow is a public type, this does risk breaking code that was walking the AST and assuming that all children of a Table are TableRows - this seems plausible given our own renderer did that.
I'm open to alternative suggestions.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/xoofx/markdig/pull/905 **Author:** [@MihaZupan](https://github.com/MihaZupan) **Created:** 10/20/2025 **Status:** ✅ Merged **Merged:** 10/20/2025 **Merged by:** [@xoofx](https://github.com/xoofx) **Base:** `master` ← **Head:** `table-para` --- ### 📝 Commits (2) - [`6f84428`](https://github.com/xoofx/markdig/commit/6f84428fc9059047c22af83de0d99b28e3600908) Fix pipe table parsing with a leading paragraph - [`cf79677`](https://github.com/xoofx/markdig/commit/cf79677838f6ae53c5f5a2bff4fd0550fc37cbab) Use the alternative approach ### 📊 Changes **6 files changed** (+145 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `src/Markdig.Tests/Specs/GridTableSpecs.generated.cs` (+29 -0) 📝 `src/Markdig.Tests/Specs/GridTableSpecs.md` (+21 -0) 📝 `src/Markdig.Tests/Specs/PipeTableSpecs.generated.cs` (+33 -0) 📝 `src/Markdig.Tests/Specs/PipeTableSpecs.md` (+25 -0) 📝 `src/Markdig.Tests/TestPipeTable.cs` (+1 -0) 📝 `src/Markdig/Extensions/Tables/PipeTableParser.cs` (+36 -14) </details> ### 📄 Description Closes #818 Addresses issues referenced in https://github.com/xoofx/markdig/pull/885#issuecomment-3419948799, https://github.com/xoofx/markdig/pull/885#issuecomment-3419969194 This PR effectively reverts #885 as that change was swallowing whatever information was there in the paragraph before the table. E.g. for ```md Some text | A | |---| | B | ``` "Some text" would be lost. Instead I relaxed the table validation to no longer enforce starting on the first line (`localLineIndex > 0` and `deltaLine > 0` checks), and also save the current block in the AST before we replace it with a table. Given this is happening while we're parsing inlines, I don't think we can easily insert an extra block at the current layer, so I instead added the leading paragrah as the first child of the table, and the first element is special-cased in the renderer. That is, instead of ``` document paragraph some text table A ... ``` we're instead producing ``` document table paragraph some text A ... ``` Since `TableRow` is a public type, this does risk breaking code that was walking the AST and assuming that all children of a `Table` are `TableRow`s - this seems plausible given our own renderer did that. I'm open to alternative suggestions. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 14:52:45 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#1296