From c1e14fe0954931d1f6fd38d2808ac808a896df18 Mon Sep 17 00:00:00 2001 From: Johan Larsson Date: Wed, 27 May 2026 10:56:59 +0200 Subject: [PATCH] Markdown reader: allow grid tables to be indented. Like the other table syntaxes (pipe, simple, and multiline tables) and block-level constructs generally, a grid table may now be indented by up to three spaces and still be recognized as a table. Previously the grid-table parser required the table to begin at the left margin, so an indented grid table was parsed as a paragraph. The leading indentation is stripped uniformly from each line before the table is parsed, so an indented grid table produces the same AST as its non-indented equivalent. Adds a command test. --- src/Text/Pandoc/Readers/Markdown.hs | 15 ++++++- test/command/grid-table-indented.md | 65 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 test/command/grid-table-indented.md diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index f0f6744c6000..a5237219b347 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -1403,7 +1403,20 @@ multilineTableHeader headless = try $ do -- ending with a footer (dashed line followed by blank line). gridTable :: PandocMonad m => MarkdownParser m (F TableComponents) -gridTable = gridTableWith' NormalizeHeader parseBlocks +gridTable = try $ do + -- Like other block-level constructs, a grid table may be indented by + -- up to three spaces. The underlying grid-table parser expects the + -- table to begin at the left margin, so strip a uniform indentation + -- from every line before handing it off. + indent <- T.length <$> lookAhead nonindentSpaces + if indent == 0 + then gridTableWith' NormalizeHeader parseBlocks + else do + let gridLine = try $ count indent (char ' ') + *> lookAhead (oneOf "+|") + *> anyLineNewline + rawTable <- T.concat <$> many1 gridLine + parseFromString' (gridTableWith' NormalizeHeader parseBlocks) rawTable pipeBreak :: PandocMonad m => MarkdownParser m ([Alignment], [Int]) pipeBreak = try $ do diff --git a/test/command/grid-table-indented.md b/test/command/grid-table-indented.md new file mode 100644 index 000000000000..cf6f24c07610 --- /dev/null +++ b/test/command/grid-table-indented.md @@ -0,0 +1,65 @@ +Like other block-level constructs, grid tables may be indented by up to +three spaces and are still recognized as tables. + +``` +% pandoc -f markdown -t html + +---+---+ + | a | b | + +===+===+ + | 1 | 2 | + +---+---+ +^D + ++++ + + + + + + + + + + + + +
ab
12
+``` + +A headerless indented grid table is recognized too. + +``` +% pandoc -f markdown -t html + +------+------+ + | foo | bar | + +------+------+ +^D + ++++ + + + + + + +
foobar
+``` + +A grid table indented four spaces is a code block, not a table. + +``` +% pandoc -f markdown -t html + +---+---+ + | a | b | + +---+---+ +^D +
+---+---+
+| a | b |
++---+---+
+```