Skip to content

Commit

Permalink
Merge pull request #1038 from thephpleague/table-start-line
Browse files Browse the repository at this point in the history
Fix table start line numbers
  • Loading branch information
colinodell authored Jul 24, 2024
2 parents 200869c + 62a823f commit 867e872
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
### Fixed

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

## [2.5.0] - 2024-07-22

Expand Down
18 changes: 12 additions & 6 deletions src/Parser/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ private function parseLine(string $line): void
$unmatchedBlocks = 0;
}

$oldBlockLineStart = null;
if ($blockStart->isReplaceActiveBlockParser()) {
$this->prepareActiveBlockParserForReplacement();
$oldBlockLineStart = $this->prepareActiveBlockParserForReplacement();
}

foreach ($blockStart->getBlockParsers() as $newBlockParser) {
$blockParser = $this->addChild($newBlockParser);
$blockParser = $this->addChild($newBlockParser, $oldBlockLineStart);
$tryBlockStarts = $newBlockParser->isContainer();
}
}
Expand Down Expand Up @@ -275,12 +276,12 @@ private function processInlines(): void
* Add block of type tag as a child of the tip. If the tip can't accept children, close and finalize it and try
* its parent, and so on til we find a block that can accept children.
*/
private function addChild(BlockContinueParserInterface $blockParser): BlockContinueParserInterface
private function addChild(BlockContinueParserInterface $blockParser, ?int $startLineNumber = null): BlockContinueParserInterface
{
$blockParser->getBlock()->setStartLine($this->lineNumber);
$blockParser->getBlock()->setStartLine($startLineNumber ?? $this->lineNumber);

while (! $this->getActiveBlockParser()->canContain($blockParser->getBlock())) {
$this->closeBlockParsers(1, $this->lineNumber - 1);
$this->closeBlockParsers(1, ($startLineNumber ?? $this->lineNumber) - 1);
}

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

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

$old->getBlock()->detach();

return $old->getBlock()->getStartLine();
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/Extension/Table/TableMarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use League\CommonMark\ConverterInterface;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\Table;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\MarkdownConverter;
use League\CommonMark\Parser\MarkdownParser;
use League\CommonMark\Tests\Functional\AbstractLocalDataTestCase;

/**
Expand All @@ -46,4 +48,29 @@ public static function dataProvider(): iterable
{
yield from self::loadTests(__DIR__ . '/md');
}

public function testStartEndLinesProperlySet(): void
{
$markdown = <<<MD
## Tabelle
| Datum | Programm | Ort |
| --- | --- | --- |
| 22. Mai | Anreise | Eichberg |
| 23. Mai | Programm | Eichberg |
MD;

$environment = new Environment([]);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new TableExtension());

$parser = new MarkdownParser($environment);
$doc = $parser->parse($markdown);

$table = $doc->lastChild();
$this->assertInstanceOf(Table::class, $table);
$this->assertSame(4, $table->getStartLine());
$this->assertSame(7, $table->getEndLine());
}
}

0 comments on commit 867e872

Please sign in to comment.