1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries\Markdown\StyleBlock;
7
8use League\CommonMark\Parser\Block\BlockStart;
9use League\CommonMark\Parser\Block\BlockStartParserInterface;
10use League\CommonMark\Parser\Cursor;
11use League\CommonMark\Parser\MarkdownParserStateInterface;
12
13class StartParser implements BlockStartParserInterface
14{
15 public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
16 {
17 $currentLine = $cursor->getLine();
18
19 if (!starts_with($currentLine, ':::')) {
20 return BlockStart::none();
21 }
22
23 // TODO: what happens to characters at the end of an opening block isn't well defined in the spec (e.g. markdown-it seems to behave differently),
24 // which can conflict with inline custom containers.
25 $className = mb_strtolower(str_replace(' ', '-', trim($currentLine, ' :')));
26
27 if (!present($className)) {
28 return BlockStart::none();
29 }
30
31 $cursor->advanceToEnd();
32
33 return BlockStart::of(new Parser($className))->at($cursor);
34 }
35}