Merge pull request #1620 from ArthurHoaro/feature/no-auto-link

Default formatter: add a setting to disable auto-linkification
This commit is contained in:
ArthurHoaro 2020-11-05 16:47:17 +01:00 committed by GitHub
commit 8bbf57a2d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 $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));
}
/**

View file

@ -46,8 +46,13 @@ protected function formatDescription($bookmark)
$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);
}
/**

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.
- **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.

View file

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