diff options
author | ArthurHoaro <arthur@hoa.ro> | 2021-02-04 10:57:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-04 10:57:12 +0100 |
commit | 11edc143b42a7be09c0c9dc02730c83e8cbb73c2 (patch) | |
tree | 55d6d35896c600066981f4cf3f49d6563ffda7d0 /application/formatter/Parsedown | |
parent | 83b4eb17958c0c03bea637db7e394f6d622aeb00 (diff) | |
parent | a1cd7a3b2ff32bf6a0f6083007f59104a85eb4bf (diff) | |
download | Shaarli-11edc143b42a7be09c0c9dc02730c83e8cbb73c2.tar.gz Shaarli-11edc143b42a7be09c0c9dc02730c83e8cbb73c2.tar.zst Shaarli-11edc143b42a7be09c0c9dc02730c83e8cbb73c2.zip |
Merge pull request #1696 from ArthurHoaro/fix/search-highlight-url
Diffstat (limited to 'application/formatter/Parsedown')
3 files changed, 111 insertions, 0 deletions
diff --git a/application/formatter/Parsedown/ShaarliParsedown.php b/application/formatter/Parsedown/ShaarliParsedown.php new file mode 100644 index 00000000..8eb48fda --- /dev/null +++ b/application/formatter/Parsedown/ShaarliParsedown.php | |||
@@ -0,0 +1,15 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Formatter\Parsedown; | ||
6 | |||
7 | /** | ||
8 | * Parsedown extension for Shaarli. | ||
9 | * | ||
10 | * Extension for both Parsedown and ParsedownExtra centralized in ShaarliParsedownTrait. | ||
11 | */ | ||
12 | class ShaarliParsedown extends \Parsedown | ||
13 | { | ||
14 | use ShaarliParsedownTrait; | ||
15 | } | ||
diff --git a/application/formatter/Parsedown/ShaarliParsedownExtra.php b/application/formatter/Parsedown/ShaarliParsedownExtra.php new file mode 100644 index 00000000..15a35da4 --- /dev/null +++ b/application/formatter/Parsedown/ShaarliParsedownExtra.php | |||
@@ -0,0 +1,15 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Formatter\Parsedown; | ||
6 | |||
7 | /** | ||
8 | * ParsedownExtra extension for Shaarli. | ||
9 | * | ||
10 | * Extension for both Parsedown and ParsedownExtra centralized in ShaarliParsedownTrait. | ||
11 | */ | ||
12 | class ShaarliParsedownExtra extends \ParsedownExtra | ||
13 | { | ||
14 | use ShaarliParsedownTrait; | ||
15 | } | ||
diff --git a/application/formatter/Parsedown/ShaarliParsedownTrait.php b/application/formatter/Parsedown/ShaarliParsedownTrait.php new file mode 100644 index 00000000..ed7b1747 --- /dev/null +++ b/application/formatter/Parsedown/ShaarliParsedownTrait.php | |||
@@ -0,0 +1,81 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Formatter\Parsedown; | ||
6 | |||
7 | use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter; | ||
8 | |||
9 | /** | ||
10 | * Trait used for Parsedown and ParsedownExtra extension. | ||
11 | * | ||
12 | * Extended: | ||
13 | * - Format links properly in search context | ||
14 | */ | ||
15 | trait ShaarliParsedownTrait | ||
16 | { | ||
17 | /** | ||
18 | * @inheritDoc | ||
19 | */ | ||
20 | protected function inlineLink($excerpt) | ||
21 | { | ||
22 | return $this->shaarliFormatLink(parent::inlineLink($excerpt), true); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * @inheritDoc | ||
27 | */ | ||
28 | protected function inlineUrl($excerpt) | ||
29 | { | ||
30 | return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false); | ||
31 | } | ||
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 | */ | ||
44 | protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array | ||
45 | { | ||
46 | // If open and clean search tokens are found in the link, process. | ||
47 | if ( | ||
48 | is_array($link) | ||
49 | && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_OPEN) !== false | ||
50 | && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false | ||
51 | ) { | ||
52 | $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens( | ||
53 | $link['element']['attributes']['href'] | ||
54 | ); | ||
55 | |||
56 | if ($fullWrap) { | ||
57 | $link['element']['text'] = Formatter::SEARCH_HIGHLIGHT_OPEN . | ||
58 | $link['element']['text'] . | ||
59 | Formatter::SEARCH_HIGHLIGHT_CLOSE | ||
60 | ; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | return $link; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Remove open and close tags from provided string. | ||
69 | * | ||
70 | * @param string $entry input | ||
71 | * | ||
72 | * @return string Striped input | ||
73 | */ | ||
74 | protected function shaarliRemoveSearchTokens(string $entry): string | ||
75 | { | ||
76 | $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry); | ||
77 | $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_CLOSE, '', $entry); | ||
78 | |||
79 | return $entry; | ||
80 | } | ||
81 | } | ||