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 @@ class ApiUtils
if (! $bookmark->isNote()) {
$out['url'] = $bookmark->getUrl();
} else {
$out['url'] = $indexUrl . $bookmark->getUrl();
$out['url'] = rtrim($indexUrl, '/') . '/' . ltrim($bookmark->getUrl(), '/');
}
$out['shorturl'] = $bookmark->getShortUrl();
$out['title'] = $bookmark->getTitle();

View file

@ -106,7 +106,7 @@ class Bookmark
throw new InvalidBookmarkException($this);
}
if (empty($this->url)) {
$this->url = '?'. $this->shortUrl;
$this->url = '/shaare/'. $this->shortUrl;
}
if (empty($this->title)) {
$this->title = $this->url;
@ -406,7 +406,7 @@ class Bookmark
public function isNote()
{
// 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 @@ class ContainerBuilder
};
$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 {

View file

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

View file

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

View file

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

View file

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

View file

@ -32,6 +32,7 @@ class BookmarkListController extends ShaarliVisitorController
}
$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
$searchTags = escape(normalize_spaces($request->getParam('searchtags') ?? ''));
$searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));;
@ -145,11 +146,14 @@ class BookmarkListController extends ShaarliVisitorController
$this->updateThumbnail($bookmark);
$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
$data = array_merge(
$this->initializeTemplateVars(),
[
'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 @@ class DailyController extends ShaarliVisitorController
}
$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
// We pre-format some fields for proper output.
foreach ($linksToDisplay as $key => $bookmark) {
$linksToDisplay[$key] = $formatter->format($bookmark);

View file

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

View file

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

View file

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

View file

@ -114,8 +114,8 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(self::NB_FIELDS_LINK, count($data));
$this->assertEquals($id, $data['id']);
$this->assertEquals('WDWyig', $data['shorturl']);
$this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
$this->assertEquals('?WDWyig', $data['title']);
$this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
$this->assertEquals('/shaare/WDWyig', $data['title']);
$this->assertEquals('', $data['description']);
$this->assertEquals([], $data['tags']);
$this->assertEquals(true, $data['private']);

View file

@ -200,7 +200,7 @@ class BookmarkFileServiceTest extends TestCase
$bookmark = $this->privateLinkDB->get(43);
$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->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
$this->assertEmpty($bookmark->getDescription());
@ -216,7 +216,7 @@ class BookmarkFileServiceTest extends TestCase
$bookmark = $this->privateLinkDB->get(43);
$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->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
$this->assertEmpty($bookmark->getDescription());

View file

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

View file

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