diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-11-03 12:38:38 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-11-03 12:43:35 +0100 |
commit | 740b32b520e6b1723512c6f9b78cef6575b1725b (patch) | |
tree | 01d976a30db84d4aa3cd2c0e6a429cc5ac15a187 | |
parent | 38b55fbf3d4fdb515d0e82e5a3126d4671652b3e (diff) | |
download | Shaarli-740b32b520e6b1723512c6f9b78cef6575b1725b.tar.gz Shaarli-740b32b520e6b1723512c6f9b78cef6575b1725b.tar.zst Shaarli-740b32b520e6b1723512c6f9b78cef6575b1725b.zip |
Default formatter: add a setting to disable auto-linkification
+ update documentation
+ single parameter for both URL and hashtags
Fixes #1094
-rw-r--r-- | application/bookmark/LinkUtils.php | 11 | ||||
-rw-r--r-- | application/formatter/BookmarkDefaultFormatter.php | 7 | ||||
-rw-r--r-- | doc/md/Shaarli-configuration.md | 16 | ||||
-rw-r--r-- | tests/formatter/BookmarkDefaultFormatterTest.php | 20 |
4 files changed, 50 insertions, 4 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php index faf5dbfd..17c37979 100644 --- a/application/bookmark/LinkUtils.php +++ b/application/bookmark/LinkUtils.php | |||
@@ -138,12 +138,17 @@ function space2nbsp($text) | |||
138 | * | 138 | * |
139 | * @param string $description shaare's description. | 139 | * @param string $description shaare's description. |
140 | * @param string $indexUrl URL to Shaarli's index. | 140 | * @param string $indexUrl URL to Shaarli's index. |
141 | 141 | * @param bool $autolink Turn on/off automatic linkifications of URLs and hashtags | |
142 | * | ||
142 | * @return string formatted description. | 143 | * @return string formatted description. |
143 | */ | 144 | */ |
144 | function format_description($description, $indexUrl = '') | 145 | function format_description($description, $indexUrl = '', $autolink = true) |
145 | { | 146 | { |
146 | return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl))); | 147 | if ($autolink) { |
148 | $description = hashtag_autolink(text2clickable($description), $indexUrl); | ||
149 | } | ||
150 | |||
151 | return nl2br(space2nbsp($description)); | ||
147 | } | 152 | } |
148 | 153 | ||
149 | /** | 154 | /** |
diff --git a/application/formatter/BookmarkDefaultFormatter.php b/application/formatter/BookmarkDefaultFormatter.php index d58a5e39..149a3eb9 100644 --- a/application/formatter/BookmarkDefaultFormatter.php +++ b/application/formatter/BookmarkDefaultFormatter.php | |||
@@ -46,8 +46,13 @@ class BookmarkDefaultFormatter extends BookmarkFormatter | |||
46 | $bookmark->getDescription() ?? '', | 46 | $bookmark->getDescription() ?? '', |
47 | $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? [] | 47 | $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? [] |
48 | ); | 48 | ); |
49 | $description = format_description( | ||
50 | escape($description), | ||
51 | $indexUrl, | ||
52 | $this->conf->get('formatter_settings.autolink', true) | ||
53 | ); | ||
49 | 54 | ||
50 | return $this->replaceTokens(format_description(escape($description), $indexUrl)); | 55 | return $this->replaceTokens($description); |
51 | } | 56 | } |
52 | 57 | ||
53 | /** | 58 | /** |
diff --git a/doc/md/Shaarli-configuration.md b/doc/md/Shaarli-configuration.md index dbfc3da9..99084728 100644 --- a/doc/md/Shaarli-configuration.md +++ b/doc/md/Shaarli-configuration.md | |||
@@ -164,6 +164,22 @@ _These settings should not be edited_ | |||
164 | - **trusted_proxies**: List of trusted IP which won't be banned after failed login attemps. Useful if Shaarli is behind a reverse proxy. | 164 | - **trusted_proxies**: List of trusted IP which won't be banned after failed login attemps. Useful if Shaarli is behind a reverse proxy. |
165 | - **allowed_protocols**: List of allowed protocols in shaare URLs or markdown-rendered descriptions. Useful if you want to store `javascript:` links (bookmarklets) in Shaarli (default: `["ftp", "ftps", "magnet"]`). | 165 | - **allowed_protocols**: List of allowed protocols in shaare URLs or markdown-rendered descriptions. Useful if you want to store `javascript:` links (bookmarklets) in Shaarli (default: `["ftp", "ftps", "magnet"]`). |
166 | 166 | ||
167 | ### Formatter | ||
168 | |||
169 | Single string value. Default available: | ||
170 | |||
171 | - `default`: supports line breaks, URL and hashtag auto-links. | ||
172 | - `markdown`: supports [Markdown](https://daringfireball.net/projects/markdown/syntax). | ||
173 | - `markdownExtra`: adds [extra](https://michelf.ca/projects/php-markdown/extra/) flavor to Markdown. | ||
174 | |||
175 | ### Formatter Settings | ||
176 | |||
177 | Additional settings applied to formatters. | ||
178 | |||
179 | #### default | ||
180 | |||
181 | - **autolink**: boolean to enable or disable automatic linkification of URL and hashtags. | ||
182 | |||
167 | ### Resources | 183 | ### Resources |
168 | 184 | ||
169 | - **data_dir**: Data directory. | 185 | - **data_dir**: Data directory. |
diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php index 3fc6f8dc..4fcc5dd1 100644 --- a/tests/formatter/BookmarkDefaultFormatterTest.php +++ b/tests/formatter/BookmarkDefaultFormatterTest.php | |||
@@ -289,4 +289,24 @@ class BookmarkDefaultFormatterTest extends TestCase | |||
289 | $link['taglist_html'] | 289 | $link['taglist_html'] |
290 | ); | 290 | ); |
291 | } | 291 | } |
292 | |||
293 | /** | ||
294 | * Test default formatting with formatter_settings.autolink set to false: | ||
295 | * URLs and hashtags should not be transformed | ||
296 | */ | ||
297 | public function testFormatDescriptionWithoutLinkification(): void | ||
298 | { | ||
299 | $this->conf->set('formatter_settings.autolink', false); | ||
300 | $this->formatter = new BookmarkDefaultFormatter($this->conf, false); | ||
301 | |||
302 | $bookmark = new Bookmark(); | ||
303 | $bookmark->setDescription('Hi!' . PHP_EOL . 'https://thisisaurl.tld #hashtag'); | ||
304 | |||
305 | $link = $this->formatter->format($bookmark); | ||
306 | |||
307 | static::assertSame( | ||
308 | 'Hi!<br />' . PHP_EOL . 'https://thisisaurl.tld #hashtag', | ||
309 | $link['description'] | ||
310 | ); | ||
311 | } | ||
292 | } | 312 | } |