Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Aug 14, 2024
2 parents 3b8b60d + ac81592 commit 9f9cbc7
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

## [2.5.1] - 2024-07-24

### Fixed

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

## [2.5.0] - 2024-07-22

### Added
Expand Down Expand Up @@ -607,7 +614,8 @@ No changes were introduced since the previous release.
- Alternative 1: Use `CommonMarkConverter` or `GithubFlavoredMarkdownConverter` if you don't need to customize the environment
- Alternative 2: Instantiate a new `Environment` and add the necessary extensions yourself

[unreleased]: https://github.com/thephpleague/commonmark/compare/2.5.0....main
[unreleased]: https://github.com/thephpleague/commonmark/compare/2.5.1....main
[2.5.1]: https://github.com/thephpleague/commonmark/compare/2.5.0....2.5.1
[2.5.0]: https://github.com/thephpleague/commonmark/compare/2.4.4...2.5.0
[2.4.4]: https://github.com/thephpleague/commonmark/compare/2.4.3...2.4.4
[2.4.3]: https://github.com/thephpleague/commonmark/compare/2.4.2...2.4.3
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/Attributes/Util/AttributesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
final class AttributesHelper
{
private const SINGLE_ATTRIBUTE = '\s*([.]-?[_a-z][^\s}]*|[#][^\s}]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . '?)\s*';
private const ATTRIBUTE_LIST = '/^{:?(' . self::SINGLE_ATTRIBUTE . ')+}/i';
private const ATTRIBUTE_LIST = '/^{:?(' . self::SINGLE_ATTRIBUTE . ')+}(?!})/i';

/**
* @return array<string, mixed>
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ <h2 class="main shine" id="the-site">The Site</h2>
<p>Attributes without quote and non-whitespace char and a dot <a target="_blank" href="http://url.com" rel="noopener noreferrer">link</a>.</p>
<p>Multiple attributes without quote and non-whitespace char and a dot <a class="class" id="id" target="_blank" href="http://url.com" rel="noopener noreferrer">link</a>.</p>
<p><img valueless-attribute src="/assets/image.jpg" alt="image" /></p>
<p>A paragraph containing {{ mustache }} templating</p>
<p>A paragraph ending with {{ mustache }} templating</p>
<p>{{ mustache }} A paragraph starting with mustache templating</p>
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ Attributes without quote and non-whitespace char and a dot [link](http://url.com
Multiple attributes without quote and non-whitespace char and a dot [link](http://url.com){#id .class target=_blank}.

![image](/assets/image.jpg){valueless-attribute}

A paragraph containing {{ mustache }} templating

A paragraph ending with {{ mustache }} templating

{{ mustache }} A paragraph starting with mustache templating

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());
}
}
5 changes: 5 additions & 0 deletions tests/unit/Extension/Attributes/Util/AttributesHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public static function dataForTestParseAttributes(): iterable
// Curly braces inside of values
yield [new Cursor('{: data-json="{1,2,3}" }'), ['data-json' => '{1,2,3}']];
yield [new Cursor('{data-json={1,2,3}} test'), ['data-json' => '{1,2,3}'], ' test'];

// Avoid mustache style templating language being parsed as attributes
yield [new Cursor('{{ foo }}'), [], '{{ foo }}'];
yield [new Cursor(' {{ foo }}'), [], ' {{ foo }}'];
yield [new Cursor('{ foo }}'), [], '{ foo }}'];
}

/**
Expand Down

0 comments on commit 9f9cbc7

Please sign in to comment.