diff options
author | ArthurHoaro <arthur@hoa.ro> | 2021-01-19 17:49:19 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2021-01-19 17:49:19 +0100 |
commit | 9ef8555ad298668bcb8537ccdd2ab6560f44177f (patch) | |
tree | ba23e5d76d3d1f9eb95231ea6504d283f86d7722 /application/formatter/Parsedown/ShaarliParsedownTrait.php | |
parent | ffa39719a17982e6a6cac9bc3f758aa12fa69973 (diff) | |
download | Shaarli-9ef8555ad298668bcb8537ccdd2ab6560f44177f.tar.gz Shaarli-9ef8555ad298668bcb8537ccdd2ab6560f44177f.tar.zst Shaarli-9ef8555ad298668bcb8537ccdd2ab6560f44177f.zip |
Support search highlights when matching URL content
DefaultFormatter:
- format 'a' tag content and not href attribute
- format hashtags properly
Markdown(Extra)Formatter:
- Extend Parsedown to format highlight properly: https://github.com/erusev/parsedown/wiki/Tutorial:-Create-Extensions
Fixes #1681
Diffstat (limited to 'application/formatter/Parsedown/ShaarliParsedownTrait.php')
-rw-r--r-- | application/formatter/Parsedown/ShaarliParsedownTrait.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/application/formatter/Parsedown/ShaarliParsedownTrait.php b/application/formatter/Parsedown/ShaarliParsedownTrait.php new file mode 100644 index 00000000..e6f4dabb --- /dev/null +++ b/application/formatter/Parsedown/ShaarliParsedownTrait.php | |||
@@ -0,0 +1,50 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Formatter\Parsedown; | ||
6 | |||
7 | use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter; | ||
8 | |||
9 | trait 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 | } | ||