Skip to content

Commit 867e872

Browse files
authored
Merge pull request #1038 from thephpleague/table-start-line
Fix table start line numbers
2 parents 200869c + 62a823f commit 867e872

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,13 @@ 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
}
169170
}
@@ -275,12 +276,12 @@ private function processInlines(): void
275276
* Add block of type tag as a child of the tip. If the tip can't accept children, close and finalize it and try
276277
* its parent, and so on til we find a block that can accept children.
277278
*/
278-
private function addChild(BlockContinueParserInterface $blockParser): BlockContinueParserInterface
279+
private function addChild(BlockContinueParserInterface $blockParser, ?int $startLineNumber = null): BlockContinueParserInterface
279280
{
280-
$blockParser->getBlock()->setStartLine($this->lineNumber);
281+
$blockParser->getBlock()->setStartLine($startLineNumber ?? $this->lineNumber);
281282

282283
while (! $this->getActiveBlockParser()->canContain($blockParser->getBlock())) {
283-
$this->closeBlockParsers(1, $this->lineNumber - 1);
284+
$this->closeBlockParsers(1, ($startLineNumber ?? $this->lineNumber) - 1);
284285
}
285286

286287
$this->getActiveBlockParser()->getBlock()->appendChild($blockParser->getBlock());
@@ -307,7 +308,10 @@ private function deactivateBlockParser(): BlockContinueParserInterface
307308
return $popped;
308309
}
309310

310-
private function prepareActiveBlockParserForReplacement(): void
311+
/**
312+
* @return int|null The line number where the old block started
313+
*/
314+
private function prepareActiveBlockParserForReplacement(): ?int
311315
{
312316
// Note that we don't want to parse inlines or finalize this block, as it's getting replaced.
313317
$old = $this->deactivateBlockParser();
@@ -317,6 +321,8 @@ private function prepareActiveBlockParserForReplacement(): void
317321
}
318322

319323
$old->getBlock()->detach();
324+
325+
return $old->getBlock()->getStartLine();
320326
}
321327

322328
/**

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)