| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
* This file is part of the league/commonmark package.
|
|
|
7 |
*
|
|
|
8 |
* (c) Colin O'Dell <colinodell@gmail.com>
|
|
|
9 |
*
|
|
|
10 |
* For the full copyright and license information, please view the LICENSE
|
|
|
11 |
* file that was distributed with this source code.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
namespace League\CommonMark\Parser\Block;
|
|
|
15 |
|
|
|
16 |
use League\CommonMark\Parser\Cursor;
|
|
|
17 |
use League\CommonMark\Parser\MarkdownParserStateInterface;
|
|
|
18 |
use League\CommonMark\Util\RegexHelper;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* @internal
|
|
|
22 |
*
|
|
|
23 |
* This "parser" is actually a performance optimization.
|
|
|
24 |
*
|
|
|
25 |
* Most lines in a typical Markdown document probably won't match a block start. This is especially true for lines starting
|
|
|
26 |
* with letters - nothing in the core CommonMark spec or our supported extensions will match those lines as blocks. Therefore,
|
|
|
27 |
* if we can identify those lines and skip block start parsing, we can optimize performance by ~10%.
|
|
|
28 |
*
|
|
|
29 |
* Previously this optimization was hard-coded in the MarkdownParser but did not allow users to override this behavior.
|
|
|
30 |
* By implementing this optimization as a block parser instead, users wanting custom blocks starting with letters
|
|
|
31 |
* can instead register their block parser with a higher priority to ensure their parser is always called first.
|
|
|
32 |
*/
|
|
|
33 |
final class SkipLinesStartingWithLettersParser implements BlockStartParserInterface
|
|
|
34 |
{
|
|
|
35 |
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
|
|
|
36 |
{
|
|
|
37 |
if (! $cursor->isIndented() && RegexHelper::isLetter($cursor->getNextNonSpaceCharacter())) {
|
|
|
38 |
$cursor->advanceToNextNonSpaceOrTab();
|
|
|
39 |
|
|
|
40 |
return BlockStart::abort();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
return BlockStart::none();
|
|
|
44 |
}
|
|
|
45 |
}
|