|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Markdown\Processing; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Markdown\Contracts\MarkdownShortcodeContract; |
|
8
|
|
|
use Hyde\Markdown\Models\Markdown; |
|
9
|
|
|
|
|
10
|
|
|
use function ltrim; |
|
11
|
|
|
use function explode; |
|
12
|
|
|
use function view; |
|
13
|
|
|
use function str_starts_with; |
|
14
|
|
|
use function trim; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @internal This class may be refactored further, thus extending this class is discouraged. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ColoredBlockquotes implements MarkdownShortcodeContract |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string The core signature. We combine this with an additional check for color later. */ |
|
22
|
|
|
protected static string $signature = '>'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array<string> */ |
|
25
|
|
|
protected static array $signatures = ['>danger', '>info', '>success', '>warning']; |
|
26
|
|
|
|
|
27
|
|
|
public static function signature(): string |
|
28
|
|
|
{ |
|
29
|
|
|
return static::$signature; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function resolve(string $input): string |
|
33
|
|
|
{ |
|
34
|
|
|
return self::stringStartsWithSignature($input) |
|
35
|
|
|
? static::expand($input) |
|
36
|
|
|
: $input; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @internal |
|
41
|
|
|
* |
|
42
|
|
|
* @return array<string> |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getSignatures(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return self::$signatures; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected static function expand(string $input): string |
|
50
|
|
|
{ |
|
51
|
|
|
$parts = explode(' ', $input, 2); |
|
52
|
|
|
$class = ltrim($parts[0], '>'); |
|
53
|
|
|
$contents = trim($parts[1] ?? '', ' '); |
|
54
|
|
|
|
|
55
|
|
|
return view('hyde::components.colored-blockquote', [ |
|
56
|
|
|
'class' => $class, |
|
57
|
|
|
'contents' => trim(Markdown::render($contents)), |
|
58
|
|
|
])->render(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected static function stringStartsWithSignature(string $input): bool |
|
62
|
|
|
{ |
|
63
|
|
|
foreach (static::$signatures as $signature) { |
|
64
|
|
|
if (str_starts_with($input, $signature)) { |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|