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/BookmarkMarkdownFormatter.php | |
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/BookmarkMarkdownFormatter.php')
-rw-r--r-- | application/formatter/BookmarkMarkdownFormatter.php | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/application/formatter/BookmarkMarkdownFormatter.php b/application/formatter/BookmarkMarkdownFormatter.php index ee4e8dca..d4dccee6 100644 --- a/application/formatter/BookmarkMarkdownFormatter.php +++ b/application/formatter/BookmarkMarkdownFormatter.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Shaarli\Formatter; | 3 | namespace Shaarli\Formatter; |
4 | 4 | ||
5 | use Shaarli\Config\ConfigManager; | 5 | use Shaarli\Config\ConfigManager; |
6 | use Shaarli\Formatter\Parsedown\ShaarliParsedown; | ||
6 | 7 | ||
7 | /** | 8 | /** |
8 | * Class BookmarkMarkdownFormatter | 9 | * Class BookmarkMarkdownFormatter |
@@ -42,7 +43,7 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter | |||
42 | { | 43 | { |
43 | parent::__construct($conf, $isLoggedIn); | 44 | parent::__construct($conf, $isLoggedIn); |
44 | 45 | ||
45 | $this->parsedown = new \Parsedown(); | 46 | $this->parsedown = new ShaarliParsedown(); |
46 | $this->escape = $conf->get('security.markdown_escape', true); | 47 | $this->escape = $conf->get('security.markdown_escape', true); |
47 | $this->allowedProtocols = $conf->get('security.allowed_protocols', []); | 48 | $this->allowedProtocols = $conf->get('security.allowed_protocols', []); |
48 | } | 49 | } |
@@ -128,6 +129,9 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter | |||
128 | protected function formatHashTags($description) | 129 | protected function formatHashTags($description) |
129 | { | 130 | { |
130 | $indexUrl = ! empty($this->contextData['index_url']) ? $this->contextData['index_url'] : ''; | 131 | $indexUrl = ! empty($this->contextData['index_url']) ? $this->contextData['index_url'] : ''; |
132 | $tokens = '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN . ')' . | ||
133 | '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE . ')' | ||
134 | ; | ||
131 | 135 | ||
132 | /* | 136 | /* |
133 | * To support unicode: http://stackoverflow.com/a/35498078/1484919 | 137 | * To support unicode: http://stackoverflow.com/a/35498078/1484919 |
@@ -136,8 +140,15 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter | |||
136 | * \p{L} - letter from any language | 140 | * \p{L} - letter from any language |
137 | * \p{Mn} - any non marking space (accents, umlauts, etc) | 141 | * \p{Mn} - any non marking space (accents, umlauts, etc) |
138 | */ | 142 | */ |
139 | $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; | 143 | $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}' . $tokens . ']+)/mui'; |
140 | $replacement = '$1[#$2](' . $indexUrl . './add-tag/$2)'; | 144 | $replacement = function (array $match) use ($indexUrl): string { |
145 | $cleanMatch = str_replace( | ||
146 | BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN, | ||
147 | '', | ||
148 | str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[2]) | ||
149 | ); | ||
150 | return $match[1] . '[#' . $match[2] . '](' . $indexUrl . './add-tag/' . $cleanMatch . ')'; | ||
151 | }; | ||
141 | 152 | ||
142 | $descriptionLines = explode(PHP_EOL, $description); | 153 | $descriptionLines = explode(PHP_EOL, $description); |
143 | $descriptionOut = ''; | 154 | $descriptionOut = ''; |
@@ -156,7 +167,7 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter | |||
156 | } | 167 | } |
157 | 168 | ||
158 | if (!$codeBlockOn && !$codeLineOn) { | 169 | if (!$codeBlockOn && !$codeLineOn) { |
159 | $descriptionLine = preg_replace($regex, $replacement, $descriptionLine); | 170 | $descriptionLine = preg_replace_callback($regex, $replacement, $descriptionLine); |
160 | } | 171 | } |
161 | 172 | ||
162 | $descriptionOut .= $descriptionLine; | 173 | $descriptionOut .= $descriptionLine; |