Default formatter: add a setting to disable auto-linkification

+ update documentation
  + single parameter for both URL and hashtags

Fixes #1094
This commit is contained in:
ArthurHoaro 2020-11-03 12:38:38 +01:00
parent 38b55fbf3d
commit 740b32b520
4 changed files with 50 additions and 4 deletions

View file

@ -138,12 +138,17 @@ function space2nbsp($text)
* *
* @param string $description shaare's description. * @param string $description shaare's description.
* @param string $indexUrl URL to Shaarli's index. * @param string $indexUrl URL to Shaarli's index.
* @param bool $autolink Turn on/off automatic linkifications of URLs and hashtags
*
* @return string formatted description. * @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));
} }
/** /**

View file

@ -46,8 +46,13 @@ protected function formatDescription($bookmark)
$bookmark->getDescription() ?? '', $bookmark->getDescription() ?? '',
$bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? [] $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);
} }
/** /**

View 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. - **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"]`). - **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 ### Resources
- **data_dir**: Data directory. - **data_dir**: Data directory.

View file

@ -289,4 +289,24 @@ public function testFormatTagListHtmlWithSearchHighlight(): void
$link['taglist_html'] $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']
);
}
} }