]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Default formatter: add a setting to disable auto-linkification 1620/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 3 Nov 2020 11:38:38 +0000 (12:38 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 3 Nov 2020 11:43:35 +0000 (12:43 +0100)
  + update documentation
  + single parameter for both URL and hashtags

Fixes #1094

application/bookmark/LinkUtils.php
application/formatter/BookmarkDefaultFormatter.php
doc/md/Shaarli-configuration.md
tests/formatter/BookmarkDefaultFormatterTest.php

index faf5dbfd4fe24906bf980d8f4cc72e0472b7e008..17c379796cd39b239be09459d78e3cad22fdfb0b 100644 (file)
@@ -138,12 +138,17 @@ function space2nbsp($text)
  *
  * @param string $description shaare's description.
  * @param string $indexUrl    URL to Shaarli's index.
-
+ * @param bool   $autolink    Turn on/off automatic linkifications of URLs and hashtags
+ *
  * @return string formatted description.
  */
-function format_description($description, $indexUrl = '')
+function format_description($description, $indexUrl = '', $autolink = true)
 {
-    return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl)));
+    if ($autolink) {
+        $description = hashtag_autolink(text2clickable($description), $indexUrl);
+    }
+
+    return nl2br(space2nbsp($description));
 }
 
 /**
index d58a5e39dde46ca5f5f0c71ac80e1f60fde8e55b..149a3eb9fc854fed6404859eb6b51083b995703a 100644 (file)
@@ -46,8 +46,13 @@ class BookmarkDefaultFormatter extends BookmarkFormatter
             $bookmark->getDescription() ?? '',
             $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? []
         );
+        $description = format_description(
+            escape($description),
+            $indexUrl,
+            $this->conf->get('formatter_settings.autolink', true)
+        );
 
-        return $this->replaceTokens(format_description(escape($description), $indexUrl));
+        return $this->replaceTokens($description);
     }
 
     /**
index dbfc3da953fc220bfccaa86bb8fc33454933c123..9908472804d889d3e375aac1ed02ba0355f3e223 100644 (file)
@@ -164,6 +164,22 @@ _These settings should not be edited_
 - **trusted_proxies**: List of trusted IP which won't be banned after failed login attemps. Useful if Shaarli is behind a reverse proxy.
 - **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"]`).
 
+### Formatter
+
+Single string value. Default available:
+
+  - `default`: supports line breaks, URL and hashtag auto-links.
+  - `markdown`: supports [Markdown](https://daringfireball.net/projects/markdown/syntax).
+  - `markdownExtra`: adds [extra](https://michelf.ca/projects/php-markdown/extra/) flavor to Markdown.
+
+### Formatter Settings
+
+Additional settings applied to formatters.
+
+#### default
+
+  - **autolink**: boolean to enable or disable automatic linkification of URL and hashtags.
+
 ### Resources
 
 - **data_dir**: Data directory.
index 3fc6f8dc58f4c8f38e877f1c49c18d2d083cbcde..4fcc5dd19cc35356498f6acc7270b80ad4aef9bc 100644 (file)
@@ -289,4 +289,24 @@ class BookmarkDefaultFormatterTest extends TestCase
             $link['taglist_html']
         );
     }
+
+    /**
+     * Test default formatting with formatter_settings.autolink set to false:
+     *   URLs and hashtags should not be transformed
+     */
+    public function testFormatDescriptionWithoutLinkification(): void
+    {
+        $this->conf->set('formatter_settings.autolink', false);
+        $this->formatter = new BookmarkDefaultFormatter($this->conf, false);
+
+        $bookmark = new Bookmark();
+        $bookmark->setDescription('Hi!' . PHP_EOL . 'https://thisisaurl.tld  #hashtag');
+
+        $link = $this->formatter->format($bookmark);
+
+        static::assertSame(
+            'Hi!<br />' . PHP_EOL . 'https://thisisaurl.tld &nbsp;#hashtag',
+            $link['description']
+        );
+    }
 }