|
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 <[email protected]> |
|
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\Extension\Embed; |
|
15
|
|
|
|
|
16
|
|
|
use League\CommonMark\Node\Block\Document; |
|
17
|
|
|
use League\CommonMark\Parser\Block\BlockStart; |
|
18
|
|
|
use League\CommonMark\Parser\Block\BlockStartParserInterface; |
|
19
|
|
|
use League\CommonMark\Parser\Cursor; |
|
20
|
|
|
use League\CommonMark\Parser\MarkdownParserStateInterface; |
|
21
|
|
|
use League\CommonMark\Util\LinkParserHelper; |
|
22
|
|
|
|
|
23
|
|
|
class EmbedStartParser implements BlockStartParserInterface |
|
24
|
|
|
{ |
|
25
|
8 |
|
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
|
26
|
|
|
{ |
|
27
|
8 |
|
if ($cursor->isIndented() || $parserState->getParagraphContent() !== null || ! ($parserState->getLastMatchedBlockParser()->getBlock() instanceof Document)) { |
|
28
|
2 |
|
return BlockStart::none(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// 0-3 leading spaces are okay |
|
32
|
8 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
33
|
|
|
|
|
34
|
|
|
// The line must begin with "https://" |
|
35
|
8 |
|
if (! str_starts_with($cursor->getRemainder(), 'https://')) { |
|
36
|
8 |
|
return BlockStart::none(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// A valid link must be found next |
|
40
|
8 |
|
if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { |
|
41
|
|
|
return BlockStart::none(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Skip any trailing whitespace |
|
45
|
8 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
46
|
|
|
|
|
47
|
|
|
// We must be at the end of the line; otherwise, this link was not by itself |
|
48
|
8 |
|
if (! $cursor->isAtEnd()) { |
|
49
|
2 |
|
return BlockStart::none(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
return BlockStart::of(new EmbedParser($dest))->at($cursor); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|