diff options
Diffstat (limited to 'application/formatter/Parsedown/ShaarliParsedownTrait.php')
-rw-r--r-- | application/formatter/Parsedown/ShaarliParsedownTrait.php | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/application/formatter/Parsedown/ShaarliParsedownTrait.php b/application/formatter/Parsedown/ShaarliParsedownTrait.php index e6f4dabb..ed7b1747 100644 --- a/application/formatter/Parsedown/ShaarliParsedownTrait.php +++ b/application/formatter/Parsedown/ShaarliParsedownTrait.php | |||
@@ -6,24 +6,48 @@ namespace Shaarli\Formatter\Parsedown; | |||
6 | 6 | ||
7 | use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter; | 7 | use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter; |
8 | 8 | ||
9 | /** | ||
10 | * Trait used for Parsedown and ParsedownExtra extension. | ||
11 | * | ||
12 | * Extended: | ||
13 | * - Format links properly in search context | ||
14 | */ | ||
9 | trait ShaarliParsedownTrait | 15 | trait ShaarliParsedownTrait |
10 | { | 16 | { |
17 | /** | ||
18 | * @inheritDoc | ||
19 | */ | ||
11 | protected function inlineLink($excerpt) | 20 | protected function inlineLink($excerpt) |
12 | { | 21 | { |
13 | return $this->shaarliFormatLink(parent::inlineLink($excerpt), true); | 22 | return $this->shaarliFormatLink(parent::inlineLink($excerpt), true); |
14 | } | 23 | } |
15 | 24 | ||
25 | /** | ||
26 | * @inheritDoc | ||
27 | */ | ||
16 | protected function inlineUrl($excerpt) | 28 | protected function inlineUrl($excerpt) |
17 | { | 29 | { |
18 | return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false); | 30 | return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false); |
19 | } | 31 | } |
20 | 32 | ||
33 | /** | ||
34 | * Properly format markdown link: | ||
35 | * - remove highlight tags from HREF attribute | ||
36 | * - (optional) add highlight tags to link caption | ||
37 | * | ||
38 | * @param array|null $link Parsedown formatted link array. | ||
39 | * It can be empty. | ||
40 | * @param bool $fullWrap Add highlight tags the whole link caption | ||
41 | * | ||
42 | * @return array|null | ||
43 | */ | ||
21 | protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array | 44 | protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array |
22 | { | 45 | { |
46 | // If open and clean search tokens are found in the link, process. | ||
23 | if ( | 47 | if ( |
24 | is_array($link) | 48 | is_array($link) |
25 | && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_OPEN) !== false | 49 | && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_OPEN) !== false |
26 | && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false | 50 | && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false |
27 | ) { | 51 | ) { |
28 | $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens( | 52 | $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens( |
29 | $link['element']['attributes']['href'] | 53 | $link['element']['attributes']['href'] |
@@ -40,6 +64,13 @@ trait ShaarliParsedownTrait | |||
40 | return $link; | 64 | return $link; |
41 | } | 65 | } |
42 | 66 | ||
67 | /** | ||
68 | * Remove open and close tags from provided string. | ||
69 | * | ||
70 | * @param string $entry input | ||
71 | * | ||
72 | * @return string Striped input | ||
73 | */ | ||
43 | protected function shaarliRemoveSearchTokens(string $entry): string | 74 | protected function shaarliRemoveSearchTokens(string $entry): string |
44 | { | 75 | { |
45 | $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry); | 76 | $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry); |