|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Roromedia\Parsedown; |
|
4
|
|
|
|
|
5
|
|
|
use Roromedia\Parsedown\Helper\Arr; |
|
6
|
|
|
use ParsedownExtra; |
|
7
|
|
|
use Symfony\Component\String\UnicodeString; |
|
8
|
|
|
|
|
9
|
|
|
class Parsedown extends ParsedownExtra { |
|
10
|
|
|
|
|
11
|
1 |
|
public function text($text): string { |
|
12
|
1 |
|
$text = parent::text($text); |
|
13
|
1 |
|
$lines = $this->explodeInLines($text); |
|
14
|
|
|
|
|
15
|
1 |
|
foreach ($lines as $number => $line) { |
|
16
|
1 |
|
$this->injectIdIntoHeadlineIfValid($line, $text, $lines, $number); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
return $this->implodeLines($lines); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
private function getMenulinks(string $text): array { |
|
23
|
1 |
|
$lines = $this->explodeInLines($text); |
|
24
|
|
|
|
|
25
|
1 |
|
return array_map( |
|
26
|
1 |
|
fn(string $line) => $this->getNameOfHeadline($line), |
|
27
|
1 |
|
array_filter($lines, fn(string $line) => $this->getNameOfHeadline($line)) |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
private function implodeLines(array $lines): string { |
|
32
|
1 |
|
return implode("\n", $lines); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
private function getNameOfHeadline(string $line): string|null { |
|
36
|
1 |
|
preg_match('/<li><a href="(.+)">/', $line, $matches); |
|
37
|
1 |
|
return $matches[1] ?? NULL; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
private function explodeInLines(string|null $text): array { |
|
41
|
1 |
|
return $text ? explode("\n", $text) : []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
private function isHeadline(string $line): bool { |
|
45
|
1 |
|
return (new UnicodeString($line))->startsWith('<h'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
private static function normalizeName(string $name): string { |
|
49
|
1 |
|
return substr($name, 1); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
private static function extractHeadlineNameFromLine(string $line): string { |
|
53
|
1 |
|
return strtolower(str_replace('_', '-', ltrim(rtrim(preg_replace('/\W/', '_', substr($line, 4, -5)), '_'), '_'))); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
private function headlineContainsOneOf(string $line, array $menulinks): string|bool { |
|
57
|
1 |
|
$result = array_filter($menulinks, fn(string $link) => static::headlineContainsName($line, $link)); |
|
58
|
1 |
|
return !empty($result) ? static::normalizeName(Arr::firstValue($result)) : FALSE; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
private static function headlineContainsName(string $line, $name): bool { |
|
62
|
1 |
|
return static::extractHeadlineNameFromLine($line) === static::normalizeName($name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
private function injectIdIntoHeadline(string $line, bool|string $name, array &$lines, int|string $number): void { |
|
66
|
1 |
|
$lines[$number] = substr_replace($line, sprintf(' id="%s"', $name), 3, 0); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
private function isValidHeadline(mixed $line, string $text): bool { |
|
70
|
1 |
|
return $this->isHeadline($line) && $this->headlineContainsOneOf($line, $this->getMenulinks($text)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
private function getMenulinkName(string $line, string $text): bool|string { |
|
74
|
1 |
|
return $this->headlineContainsOneOf($line, $this->getMenulinks($text)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
private function injectIdIntoHeadlineIfValid(mixed $line, string $text, array &$lines, int|string $number): void { |
|
78
|
1 |
|
$this->isValidHeadline($line, $text) && $this->injectIdIntoHeadline($line, $this->getMenulinkName($line, $text), $lines, $number); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|