Better support for notes permalink

This commit is contained in:
ArthurHoaro 2020-07-28 20:46:11 +02:00
parent b725eb047d
commit 301c7ab1a0
16 changed files with 49 additions and 29 deletions

View file

@ -67,7 +67,7 @@ public static function formatLink($bookmark, $indexUrl)
if (! $bookmark->isNote()) { if (! $bookmark->isNote()) {
$out['url'] = $bookmark->getUrl(); $out['url'] = $bookmark->getUrl();
} else { } else {
$out['url'] = $indexUrl . $bookmark->getUrl(); $out['url'] = rtrim($indexUrl, '/') . '/' . ltrim($bookmark->getUrl(), '/');
} }
$out['shorturl'] = $bookmark->getShortUrl(); $out['shorturl'] = $bookmark->getShortUrl();
$out['title'] = $bookmark->getTitle(); $out['title'] = $bookmark->getTitle();

View file

@ -106,7 +106,7 @@ public function validate()
throw new InvalidBookmarkException($this); throw new InvalidBookmarkException($this);
} }
if (empty($this->url)) { if (empty($this->url)) {
$this->url = '?'. $this->shortUrl; $this->url = '/shaare/'. $this->shortUrl;
} }
if (empty($this->title)) { if (empty($this->title)) {
$this->title = $this->url; $this->title = $this->url;
@ -406,7 +406,7 @@ public function getTagsString()
public function isNote() public function isNote()
{ {
// We check empty value to get a valid result if the link has not been saved yet // We check empty value to get a valid result if the link has not been saved yet
return empty($this->url) || $this->url[0] === '?'; return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?';
} }
/** /**

View file

@ -105,7 +105,10 @@ public function build(): ShaarliContainer
}; };
$container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory { $container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory {
return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn()); return new FormatterFactory(
$container->conf,
$container->loginManager->isLoggedIn()
);
}; };
$container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager { $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager {

View file

@ -174,7 +174,7 @@ public function setLocale($locale)
protected function buildItem(string $feedType, $link, $pageaddr) protected function buildItem(string $feedType, $link, $pageaddr)
{ {
$data = $this->formatter->format($link); $data = $this->formatter->format($link);
$data['guid'] = $pageaddr . '?' . $data['shorturl']; $data['guid'] = rtrim($pageaddr, '/') . '/shaare/' . $data['shorturl'];
if ($this->usePermalinks === true) { if ($this->usePermalinks === true) {
$permalink = '<a href="'. $data['url'] .'" title="'. t('Direct link') .'">'. t('Direct link') .'</a>'; $permalink = '<a href="'. $data['url'] .'" title="'. t('Direct link') .'">'. t('Direct link') .'</a>';
} else { } else {

View file

@ -50,11 +50,10 @@ public function formatTagString($bookmark)
*/ */
public function formatUrl($bookmark) public function formatUrl($bookmark)
{ {
if (! empty($this->contextData['index_url']) && ( if ($bookmark->isNote() && !empty($this->contextData['index_url'])) {
startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') return rtrim($this->contextData['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/'));
)) {
return $this->contextData['index_url'] . escape($bookmark->getUrl());
} }
return escape($bookmark->getUrl()); return escape($bookmark->getUrl());
} }
@ -63,11 +62,18 @@ public function formatUrl($bookmark)
*/ */
protected function formatRealUrl($bookmark) protected function formatRealUrl($bookmark)
{ {
if (! empty($this->contextData['index_url']) && ( if ($bookmark->isNote()) {
startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') if (!empty($this->contextData['index_url'])) {
)) { $prefix = rtrim($this->contextData['index_url'], '/') . '/';
return $this->contextData['index_url'] . escape($bookmark->getUrl());
} }
if (!empty($this->contextData['base_path'])) {
$prefix = rtrim($this->contextData['base_path'], '/') . '/';
}
return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/'));
}
return escape($bookmark->getUrl()); return escape($bookmark->getUrl());
} }

View file

@ -3,8 +3,8 @@
namespace Shaarli\Formatter; namespace Shaarli\Formatter;
use DateTime; use DateTime;
use Shaarli\Config\ConfigManager;
use Shaarli\Bookmark\Bookmark; use Shaarli\Bookmark\Bookmark;
use Shaarli\Config\ConfigManager;
/** /**
* Class BookmarkFormatter * Class BookmarkFormatter
@ -80,6 +80,8 @@ public function format($bookmark)
public function addContextData($key, $value) public function addContextData($key, $value)
{ {
$this->contextData[$key] = $value; $this->contextData[$key] = $value;
return $this;
} }
/** /**
@ -128,7 +130,7 @@ protected function formatUrl($bookmark)
*/ */
protected function formatRealUrl($bookmark) protected function formatRealUrl($bookmark)
{ {
return $bookmark->getUrl(); return $this->formatUrl($bookmark);
} }
/** /**

View file

@ -38,7 +38,7 @@ public function __construct(ConfigManager $conf, bool $isLoggedIn)
* *
* @return BookmarkFormatter instance. * @return BookmarkFormatter instance.
*/ */
public function getFormatter(string $type = null) public function getFormatter(string $type = null): BookmarkFormatter
{ {
$type = $type ? $type : $this->conf->get('formatter', 'default'); $type = $type ? $type : $this->conf->get('formatter', 'default');
$className = '\\Shaarli\\Formatter\\Bookmark'. ucfirst($type) .'Formatter'; $className = '\\Shaarli\\Formatter\\Bookmark'. ucfirst($type) .'Formatter';

View file

@ -32,6 +32,7 @@ public function index(Request $request, Response $response): Response
} }
$formatter = $this->container->formatterFactory->getFormatter(); $formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
$searchTags = escape(normalize_spaces($request->getParam('searchtags') ?? '')); $searchTags = escape(normalize_spaces($request->getParam('searchtags') ?? ''));
$searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));; $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));;
@ -145,11 +146,14 @@ public function permalink(Request $request, Response $response, array $args): Re
$this->updateThumbnail($bookmark); $this->updateThumbnail($bookmark);
$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
$data = array_merge( $data = array_merge(
$this->initializeTemplateVars(), $this->initializeTemplateVars(),
[ [
'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'), 'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'),
'links' => [$this->container->formatterFactory->getFormatter()->format($bookmark)], 'links' => [$formatter->format($bookmark)],
] ]
); );

View file

@ -54,6 +54,7 @@ public function index(Request $request, Response $response): Response
} }
$formatter = $this->container->formatterFactory->getFormatter(); $formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
// We pre-format some fields for proper output. // We pre-format some fields for proper output.
foreach ($linksToDisplay as $key => $bookmark) { foreach ($linksToDisplay as $key => $bookmark) {
$linksToDisplay[$key] = $formatter->format($bookmark); $linksToDisplay[$key] = $formatter->format($bookmark);

View file

@ -68,7 +68,7 @@ public function filterAndFormat(
$link = $formatter->format($bookmark); $link = $formatter->format($bookmark);
$link['taglist'] = implode(',', $bookmark->getTags()); $link['taglist'] = implode(',', $bookmark->getTags());
if ($bookmark->isNote() && $prependNoteUrl) { if ($bookmark->isNote() && $prependNoteUrl) {
$link['url'] = $indexUrl . $link['url']; $link['url'] = rtrim($indexUrl, '/') . '/' . ltrim($link['url'], '/');
} }
$bookmarkLinks[] = $link; $bookmarkLinks[] = $link;

View file

@ -26,11 +26,15 @@ function findParent(element, tagName, attributes) {
* Ajax request to refresh the CSRF token. * Ajax request to refresh the CSRF token.
*/ */
function refreshToken(basePath) { function refreshToken(basePath) {
console.log('refresh');
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('GET', `${basePath}/admin/token`); xhr.open('GET', `${basePath}/admin/token`);
xhr.onload = () => { xhr.onload = () => {
const token = document.getElementById('token'); const elements = document.querySelectorAll('input[name="token"]');
token.setAttribute('value', xhr.responseText); [...elements].forEach((element) => {
console.log(element);
element.setAttribute('value', xhr.responseText);
});
}; };
xhr.send(); xhr.send();
} }

View file

@ -131,8 +131,8 @@ public function testPostLinkMinimal()
$this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals(self::NB_FIELDS_LINK, count($data));
$this->assertEquals(43, $data['id']); $this->assertEquals(43, $data['id']);
$this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
$this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']); $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']);
$this->assertEquals('?' . $data['shorturl'], $data['title']); $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']);
$this->assertEquals('', $data['description']); $this->assertEquals('', $data['description']);
$this->assertEquals([], $data['tags']); $this->assertEquals([], $data['tags']);
$this->assertEquals(true, $data['private']); $this->assertEquals(true, $data['private']);

View file

@ -114,8 +114,8 @@ public function testPutLinkMinimal()
$this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals(self::NB_FIELDS_LINK, count($data));
$this->assertEquals($id, $data['id']); $this->assertEquals($id, $data['id']);
$this->assertEquals('WDWyig', $data['shorturl']); $this->assertEquals('WDWyig', $data['shorturl']);
$this->assertEquals('http://domain.tld/?WDWyig', $data['url']); $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
$this->assertEquals('?WDWyig', $data['title']); $this->assertEquals('/shaare/WDWyig', $data['title']);
$this->assertEquals('', $data['description']); $this->assertEquals('', $data['description']);
$this->assertEquals([], $data['tags']); $this->assertEquals([], $data['tags']);
$this->assertEquals(true, $data['private']); $this->assertEquals(true, $data['private']);

View file

@ -200,7 +200,7 @@ public function testAddMinimal()
$bookmark = $this->privateLinkDB->get(43); $bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId()); $this->assertEquals(43, $bookmark->getId());
$this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
$this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
$this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
$this->assertEmpty($bookmark->getDescription()); $this->assertEmpty($bookmark->getDescription());
@ -216,7 +216,7 @@ public function testAddMinimal()
$bookmark = $this->privateLinkDB->get(43); $bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId()); $this->assertEquals(43, $bookmark->getId());
$this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
$this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
$this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
$this->assertEmpty($bookmark->getDescription()); $this->assertEmpty($bookmark->getDescription());

View file

@ -124,8 +124,8 @@ public function testValidateValidMinimalBookmark()
$this->assertEquals(1, $bookmark->getId()); $this->assertEquals(1, $bookmark->getId());
$this->assertEquals('abc', $bookmark->getShortUrl()); $this->assertEquals('abc', $bookmark->getShortUrl());
$this->assertEquals($date, $bookmark->getCreated()); $this->assertEquals($date, $bookmark->getCreated());
$this->assertEquals('?abc', $bookmark->getUrl()); $this->assertEquals('/shaare/abc', $bookmark->getUrl());
$this->assertEquals('?abc', $bookmark->getTitle()); $this->assertEquals('/shaare/abc', $bookmark->getTitle());
$this->assertEquals('', $bookmark->getDescription()); $this->assertEquals('', $bookmark->getDescription());
$this->assertEquals([], $bookmark->getTags()); $this->assertEquals([], $bookmark->getTags());
$this->assertEquals('', $bookmark->getTagsString()); $this->assertEquals('', $bookmark->getTagsString());

View file

@ -85,7 +85,7 @@ <h3 class="window-subtitle">
{if="$link.tags"} {if="$link.tags"}
<div class="daily-entry-tags center"> <div class="daily-entry-tags center">
{loop="link.taglist"} {loop="link.taglist"}
<span class="label label-tag" title="Add tag"> <span class="label label-tag">
{$value} {$value}
</span> </span>
{/loop} {/loop}