Skip to content

Commit e8a1953

Browse files
committed
Fix incorrect Table start line numbers
1 parent 200869c commit e8a1953

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
99
### Fixed
1010

1111
- Fixed attribute parsing incorrectly parsing mustache-like syntax (#1035)
12+
- Fixed incorrect `Table` start line numbers (#1037)
1213

1314
## [2.5.0] - 2024-07-22
1415

src/Parser/MarkdownParser.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ private function parseLine(string $line): void
158158
$unmatchedBlocks = 0;
159159
}
160160

161+
$oldBlockLineStart = null;
161162
if ($blockStart->isReplaceActiveBlockParser()) {
162-
$this->prepareActiveBlockParserForReplacement();
163+
$oldBlockLineStart = $this->prepareActiveBlockParserForReplacement();
163164
}
164165

165166
foreach ($blockStart->getBlockParsers() as $newBlockParser) {
166-
$blockParser = $this->addChild($newBlockParser);
167+
$blockParser = $this->addChild($newBlockParser, $oldBlockLineStart);
167168
$tryBlockStarts = $newBlockParser->isContainer();
168169
}
170+
171+
unset($oldBlockLineStart);
169172
}
170173

171174
// What remains at the offset is a text line. Add the text to the appropriate block.
@@ -275,12 +278,12 @@ private function processInlines(): void
275278
* Add block of type tag as a child of the tip. If the tip can't accept children, close and finalize it and try
276279
* its parent, and so on til we find a block that can accept children.
277280
*/
278-
private function addChild(BlockContinueParserInterface $blockParser): BlockContinueParserInterface
281+
private function addChild(BlockContinueParserInterface $blockParser, ?int $startLineNumber = null): BlockContinueParserInterface
279282
{
280-
$blockParser->getBlock()->setStartLine($this->lineNumber);
283+
$blockParser->getBlock()->setStartLine($startLineNumber ?? $this->lineNumber);
281284

282285
while (! $this->getActiveBlockParser()->canContain($blockParser->getBlock())) {
283-
$this->closeBlockParsers(1, $this->lineNumber - 1);
286+
$this->closeBlockParsers(1, ($startLineNumber ?? $this->lineNumber) - 1);
284287
}
285288

286289
$this->getActiveBlockParser()->getBlock()->appendChild($blockParser->getBlock());
@@ -307,7 +310,10 @@ private function deactivateBlockParser(): BlockContinueParserInterface
307310
return $popped;
308311
}
309312

310-
private function prepareActiveBlockParserForReplacement(): void
313+
/**
314+
* @return int|null The line number where the old block started
315+
*/
316+
private function prepareActiveBlockParserForReplacement(): ?int
311317
{
312318
// Note that we don't want to parse inlines or finalize this block, as it's getting replaced.
313319
$old = $this->deactivateBlockParser();
@@ -317,6 +323,8 @@ private function prepareActiveBlockParserForReplacement(): void
317323
}
318324

319325
$old->getBlock()->detach();
326+
327+
return $old->getBlock()->getStartLine();
320328
}
321329

322330
/**

tests/functional/Extension/Table/TableMarkdownTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use League\CommonMark\ConverterInterface;
1919
use League\CommonMark\Environment\Environment;
2020
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
21+
use League\CommonMark\Extension\Table\Table;
2122
use League\CommonMark\Extension\Table\TableExtension;
2223
use League\CommonMark\MarkdownConverter;
24+
use League\CommonMark\Parser\MarkdownParser;
2325
use League\CommonMark\Tests\Functional\AbstractLocalDataTestCase;
2426

2527
/**
@@ -46,4 +48,29 @@ public static function dataProvider(): iterable
4648
{
4749
yield from self::loadTests(__DIR__ . '/md');
4850
}
51+
52+
public function testStartEndLinesProperlySet(): void
53+
{
54+
$markdown = <<<MD
55+
56+
## Tabelle
57+
58+
| Datum | Programm | Ort |
59+
| --- | --- | --- |
60+
| 22. Mai | Anreise | Eichberg |
61+
| 23. Mai | Programm | Eichberg |
62+
MD;
63+
64+
$environment = new Environment([]);
65+
$environment->addExtension(new CommonMarkCoreExtension());
66+
$environment->addExtension(new TableExtension());
67+
68+
$parser = new MarkdownParser($environment);
69+
$doc = $parser->parse($markdown);
70+
71+
$table = $doc->lastChild();
72+
$this->assertInstanceOf(Table::class, $table);
73+
$this->assertSame(4, $table->getStartLine());
74+
$this->assertSame(7, $table->getEndLine());
75+
}
4976
}

0 commit comments

Comments
 (0)