3 declare(strict_types
=1);
5 namespace Shaarli\Formatter\Parsedown
;
7 use Shaarli\Formatter\BookmarkDefaultFormatter
as Formatter
;
10 * Trait used for Parsedown and ParsedownExtra extension.
13 * - Format links properly in search context
15 trait ShaarliParsedownTrait
20 protected function inlineLink($excerpt)
22 return $this->shaarliFormatLink(parent
::inlineLink($excerpt), true);
28 protected function inlineUrl($excerpt)
30 return $this->shaarliFormatLink(parent
::inlineUrl($excerpt), false);
34 * Properly format markdown link:
35 * - remove highlight tags from HREF attribute
36 * - (optional) add highlight tags to link caption
38 * @param array|null $link Parsedown formatted link array.
40 * @param bool $fullWrap Add highlight tags the whole link caption
44 protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array
46 // If open and clean search tokens are found in the link, process.
49 && strpos($link['element']['attributes']['href'] ?? '', Formatter
::SEARCH_HIGHLIGHT_OPEN
) !== false
50 && strpos($link['element']['attributes']['href'] ?? '', Formatter
::SEARCH_HIGHLIGHT_CLOSE
) !== false
52 $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens(
53 $link['element']['attributes']['href']
57 $link['element']['text'] = Formatter
::SEARCH_HIGHLIGHT_OPEN
.
58 $link['element']['text'] .
59 Formatter
::SEARCH_HIGHLIGHT_CLOSE
68 * Remove open and close tags from provided string.
70 * @param string $entry input
72 * @return string Striped input
74 protected function shaarliRemoveSearchTokens(string $entry): string
76 $entry = str_replace(Formatter
::SEARCH_HIGHLIGHT_OPEN
, '', $entry);
77 $entry = str_replace(Formatter
::SEARCH_HIGHLIGHT_CLOSE
, '', $entry);