Skip to content

Commit

Permalink
Fix incorrect Table start line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Jul 24, 2024
1 parent 200869c commit e1178f5
Show file tree
Hide file tree
Showing 3 changed files with 42 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
20 changes: 14 additions & 6 deletions src/Parser/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ 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();
}

unset($oldBlockLineStart);
}

// What remains at the offset is a text line. Add the text to the appropriate block.
Expand Down Expand Up @@ -275,12 +278,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 +310,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 +323,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);

Check failure on line 69 in tests/functional/Extension/Table/TableMarkdownTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

Check failure on line 69 in tests/functional/Extension/Table/TableMarkdownTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

0 comments on commit e1178f5

Please sign in to comment.