From 9ef8555ad298668bcb8537ccdd2ab6560f44177f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 19 Jan 2021 17:49:19 +0100 Subject: 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 --- .../formatter/Parsedown/ShaarliParsedownTrait.php | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 application/formatter/Parsedown/ShaarliParsedownTrait.php (limited to 'application/formatter/Parsedown/ShaarliParsedownTrait.php') 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 @@ +shaarliFormatLink(parent::inlineLink($excerpt), true); + } + + protected function inlineUrl($excerpt) + { + return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false); + } + + protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array + { + if ( + is_array($link) + && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_OPEN) !== false + && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false + ) { + $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens( + $link['element']['attributes']['href'] + ); + + if ($fullWrap) { + $link['element']['text'] = Formatter::SEARCH_HIGHLIGHT_OPEN . + $link['element']['text'] . + Formatter::SEARCH_HIGHLIGHT_CLOSE + ; + } + } + + return $link; + } + + protected function shaarliRemoveSearchTokens(string $entry): string + { + $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry); + $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_CLOSE, '', $entry); + + return $entry; + } +} -- cgit v1.2.3