]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/formatter/Parsedown/ShaarliParsedownTrait.php
Support search highlights when matching URL content
[github/shaarli/Shaarli.git] / application / formatter / Parsedown / ShaarliParsedownTrait.php
CommitLineData
9ef8555a
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Formatter\Parsedown;
6
7use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter;
8
9trait ShaarliParsedownTrait
10{
11 protected function inlineLink($excerpt)
12 {
13 return $this->shaarliFormatLink(parent::inlineLink($excerpt), true);
14 }
15
16 protected function inlineUrl($excerpt)
17 {
18 return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false);
19 }
20
21 protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array
22 {
23 if (
24 is_array($link)
25 && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_OPEN) !== false
26 && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false
27 ) {
28 $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens(
29 $link['element']['attributes']['href']
30 );
31
32 if ($fullWrap) {
33 $link['element']['text'] = Formatter::SEARCH_HIGHLIGHT_OPEN .
34 $link['element']['text'] .
35 Formatter::SEARCH_HIGHLIGHT_CLOSE
36 ;
37 }
38 }
39
40 return $link;
41 }
42
43 protected function shaarliRemoveSearchTokens(string $entry): string
44 {
45 $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry);
46 $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_CLOSE, '', $entry);
47
48 return $entry;
49 }
50}