X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fformatter%2FBookmarkDefaultFormatter.php;h=51bea0f15082ee2ed168446f7fd15f207f7cdd54;hb=b3bd8c3e8d367975980043e772f7cd78b7f96bc6;hp=7550c5566103116dd828b6b8a5c518feece4c091;hpb=3fb29fdda04ca86e04422d49b86cf646d53c4f9d;p=github%2Fshaarli%2FShaarli.git diff --git a/application/formatter/BookmarkDefaultFormatter.php b/application/formatter/BookmarkDefaultFormatter.php index 7550c556..51bea0f1 100644 --- a/application/formatter/BookmarkDefaultFormatter.php +++ b/application/formatter/BookmarkDefaultFormatter.php @@ -12,10 +12,13 @@ namespace Shaarli\Formatter; */ class BookmarkDefaultFormatter extends BookmarkFormatter { + const SEARCH_HIGHLIGHT_OPEN = '|@@HIGHLIGHT'; + const SEARCH_HIGHLIGHT_CLOSE = 'HIGHLIGHT@@|'; + /** * @inheritdoc */ - public function formatTitle($bookmark) + protected function formatTitle($bookmark) { return escape($bookmark->getTitle()); } @@ -23,10 +26,33 @@ class BookmarkDefaultFormatter extends BookmarkFormatter /** * @inheritdoc */ - public function formatDescription($bookmark) + protected function formatTitleHtml($bookmark) + { + $title = $this->tokenizeSearchHighlightField( + $bookmark->getTitle() ?? '', + $bookmark->getAdditionalContentEntry('search_highlight')['title'] ?? [] + ); + + return $this->replaceTokens(escape($title)); + } + + /** + * @inheritdoc + */ + protected function formatDescription($bookmark) { $indexUrl = ! empty($this->contextData['index_url']) ? $this->contextData['index_url'] : ''; - return format_description(escape($bookmark->getDescription()), $indexUrl); + $description = $this->tokenizeSearchHighlightField( + $bookmark->getDescription() ?? '', + $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? [] + ); + $description = format_description( + escape($description), + $indexUrl, + $this->conf->get('formatter_settings.autolink', true) + ); + + return $this->replaceTokens($description); } /** @@ -34,27 +60,47 @@ class BookmarkDefaultFormatter extends BookmarkFormatter */ protected function formatTagList($bookmark) { - return escape($bookmark->getTags()); + return escape(parent::formatTagList($bookmark)); } /** * @inheritdoc */ - public function formatTagString($bookmark) + protected function formatTagListHtml($bookmark) { - return implode(' ', $this->formatTagList($bookmark)); + $tagsSeparator = $this->conf->get('general.tags_separator', ' '); + if (empty($bookmark->getAdditionalContentEntry('search_highlight')['tags'])) { + return $this->formatTagList($bookmark); + } + + $tags = $this->tokenizeSearchHighlightField( + $bookmark->getTagsString($tagsSeparator), + $bookmark->getAdditionalContentEntry('search_highlight')['tags'] + ); + $tags = $this->filterTagList(tags_str2array($tags, $tagsSeparator)); + $tags = escape($tags); + $tags = $this->replaceTokensArray($tags); + + return $tags; } /** * @inheritdoc */ - public function formatUrl($bookmark) + protected function formatTagString($bookmark) { - if (! empty($this->contextData['index_url']) && ( - startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') - )) { - return $this->contextData['index_url'] . escape($bookmark->getUrl()); + return implode($this->conf->get('general.tags_separator'), $this->formatTagList($bookmark)); + } + + /** + * @inheritdoc + */ + protected function formatUrl($bookmark) + { + if ($bookmark->isNote() && isset($this->contextData['index_url'])) { + return rtrim($this->contextData['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/')); } + return escape($bookmark->getUrl()); } @@ -63,14 +109,34 @@ class BookmarkDefaultFormatter extends BookmarkFormatter */ protected function formatRealUrl($bookmark) { - if (! empty($this->contextData['index_url']) && ( - startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') - )) { - return $this->contextData['index_url'] . escape($bookmark->getUrl()); + if ($bookmark->isNote()) { + if (isset($this->contextData['index_url'])) { + $prefix = rtrim($this->contextData['index_url'], '/') . '/'; + } + + if (isset($this->contextData['base_path'])) { + $prefix = rtrim($this->contextData['base_path'], '/') . '/'; + } + + return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/')); } + return escape($bookmark->getUrl()); } + /** + * @inheritdoc + */ + protected function formatUrlHtml($bookmark) + { + $url = $this->tokenizeSearchHighlightField( + $bookmark->getUrl() ?? '', + $bookmark->getAdditionalContentEntry('search_highlight')['url'] ?? [] + ); + + return $this->replaceTokens(escape($url)); + } + /** * @inheritdoc */ @@ -78,4 +144,72 @@ class BookmarkDefaultFormatter extends BookmarkFormatter { return escape($bookmark->getThumbnail()); } + + /** + * Insert search highlight token in provided field content based on a list of search result positions + * + * @param string $fieldContent + * @param array|null $positions List of of search results with 'start' and 'end' positions. + * + * @return string Updated $fieldContent. + */ + protected function tokenizeSearchHighlightField(string $fieldContent, ?array $positions): string + { + if (empty($positions)) { + return $fieldContent; + } + + $insertedTokens = 0; + $tokenLength = strlen(static::SEARCH_HIGHLIGHT_OPEN); + foreach ($positions as $position) { + $position = [ + 'start' => $position['start'] + ($insertedTokens * $tokenLength), + 'end' => $position['end'] + ($insertedTokens * $tokenLength), + ]; + + $content = mb_substr($fieldContent, 0, $position['start']); + $content .= static::SEARCH_HIGHLIGHT_OPEN; + $content .= mb_substr($fieldContent, $position['start'], $position['end'] - $position['start']); + $content .= static::SEARCH_HIGHLIGHT_CLOSE; + $content .= mb_substr($fieldContent, $position['end']); + + $fieldContent = $content; + + $insertedTokens += 2; + } + + return $fieldContent; + } + + /** + * Replace search highlight tokens with HTML highlighted span. + * + * @param string $fieldContent + * + * @return string updated content. + */ + protected function replaceTokens(string $fieldContent): string + { + return str_replace( + [static::SEARCH_HIGHLIGHT_OPEN, static::SEARCH_HIGHLIGHT_CLOSE], + ['', ''], + $fieldContent + ); + } + + /** + * Apply replaceTokens to an array of content strings. + * + * @param string[] $fieldContents + * + * @return array + */ + protected function replaceTokensArray(array $fieldContents): array + { + foreach ($fieldContents as &$entry) { + $entry = $this->replaceTokens($entry); + } + + return $fieldContents; + } }