aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark
diff options
context:
space:
mode:
Diffstat (limited to 'application/bookmark')
-rw-r--r--application/bookmark/LinkUtils.php34
1 files changed, 30 insertions, 4 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php
index 0ab2d213..8fa2953a 100644
--- a/application/bookmark/LinkUtils.php
+++ b/application/bookmark/LinkUtils.php
@@ -1,6 +1,7 @@
1<?php 1<?php
2 2
3use Shaarli\Bookmark\Bookmark; 3use Shaarli\Bookmark\Bookmark;
4use Shaarli\Formatter\BookmarkDefaultFormatter;
4 5
5/** 6/**
6 * Extract title from an HTML document. 7 * Extract title from an HTML document.
@@ -98,7 +99,18 @@ function html_extract_tag($tag, $html)
98function text2clickable($text) 99function text2clickable($text)
99{ 100{
100 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si'; 101 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
101 return preg_replace($regex, '<a href="$1">$1</a>', $text); 102 $format = function (array $match): string {
103 return '<a href="' .
104 str_replace(
105 BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN,
106 '',
107 str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[1])
108 ) .
109 '">' . $match[1] . '</a>'
110 ;
111 };
112
113 return preg_replace_callback($regex, $format, $text);
102} 114}
103 115
104/** 116/**
@@ -111,6 +123,9 @@ function text2clickable($text)
111 */ 123 */
112function hashtag_autolink($description, $indexUrl = '') 124function hashtag_autolink($description, $indexUrl = '')
113{ 125{
126 $tokens = '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN . ')' .
127 '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE . ')'
128 ;
114 /* 129 /*
115 * To support unicode: http://stackoverflow.com/a/35498078/1484919 130 * To support unicode: http://stackoverflow.com/a/35498078/1484919
116 * \p{Pc} - to match underscore 131 * \p{Pc} - to match underscore
@@ -118,9 +133,20 @@ function hashtag_autolink($description, $indexUrl = '')
118 * \p{L} - letter from any language 133 * \p{L} - letter from any language
119 * \p{Mn} - any non marking space (accents, umlauts, etc) 134 * \p{Mn} - any non marking space (accents, umlauts, etc)
120 */ 135 */
121 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; 136 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}' . $tokens . ']+)/mui';
122 $replacement = '$1<a href="' . $indexUrl . './add-tag/$2" title="Hashtag $2">#$2</a>'; 137 $format = function (array $match) use ($indexUrl): string {
123 return preg_replace($regex, $replacement, $description); 138 $cleanMatch = str_replace(
139 BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN,
140 '',
141 str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[2])
142 );
143 return $match[1] . '<a href="' . $indexUrl . './add-tag/' . $cleanMatch . '"' .
144 ' title="Hashtag ' . $cleanMatch . '">' .
145 '#' . $match[2] .
146 '</a>';
147 };
148
149 return preg_replace_callback($regex, $format, $description);
124} 150}
125 151
126/** 152/**