Improved Page lik handling

This commit is contained in:
2026-02-02 22:36:38 +01:00
parent 47a8bfa50a
commit 1b6df4a9e4
8 changed files with 245 additions and 130 deletions

View File

@@ -211,6 +211,10 @@ abstract class syntax_plugin_luxtools_abstract extends SyntaxPlugin
try {
$pathConfig = (string)$this->getConf('paths');
$blobsRoot = $this->resolveBlobsRoot();
if ($blobsRoot === '' && $this->isBlobsPath($basePath)) {
$this->renderPageNotLinked($renderer);
return false;
}
if ($blobsRoot !== '') {
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
}
@@ -222,6 +226,55 @@ abstract class syntax_plugin_luxtools_abstract extends SyntaxPlugin
}
}
/**
* Check if the given path uses the blobs alias.
*/
protected function isBlobsPath(string $path): bool
{
$trimmed = ltrim($path, '/');
return preg_match('/^blobs(\/|$)/', $trimmed) === 1;
}
/**
* Render the "Page not linked" message with copy ID affordance.
*/
protected function renderPageNotLinked(\Doku_Renderer $renderer): void
{
$uuid = $this->getPageUuidSafe();
$text = (string)$this->getLang('pagelink_unlinked');
if ($renderer instanceof \Doku_Renderer_xhtml) {
$renderer->doc .= '<a href="#" class="luxtools-pagelink-copy" data-luxtools-pagelink-copy="1"'
. ' data-uuid="' . hsc($uuid) . '"'
. '>' . hsc($text) . '</a>';
return;
}
$renderer->cdata('[n/a: ' . $text . ']');
}
/**
* Read the current page UUID (if any).
*/
protected function getPageUuidSafe(): string
{
global $ID;
$pageId = is_string($ID) ? $ID : '';
if ($pageId === '') return '';
if (function_exists('cleanID')) {
$pageId = (string)cleanID($pageId);
}
if ($pageId === '') return '';
$depth = (int)$this->getConf('pagelink_search_depth');
if ($depth < 0) $depth = 0;
$pageLink = new PageLink((string)$this->getConf('paths'), $depth);
$uuid = $pageLink->getPageUuid($pageId);
return $uuid ?? '';
}
/**
* Resolve the current page's pagelink folder for the blobs alias.
*

View File

@@ -133,7 +133,18 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
}
try {
$pathHelper = new Path($this->buildPathConfigWithBlobs());
$blobsRoot = $this->resolveBlobsRoot();
if ($blobsRoot === '' && $this->isBlobsPath($data['path'] ?? '')) {
$this->renderPageNotLinked($renderer);
return true;
}
$pathConfig = (string)$this->getConf('paths');
if ($blobsRoot !== '') {
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
}
$pathHelper = new Path($pathConfig);
// Use addTrailingSlash=false since this is a file path, not a directory
$pathInfo = $pathHelper->getPathInfo($data['path'], false);
} catch (\Exception $e) {
@@ -216,16 +227,52 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
}
/**
* Build a path configuration string, adding the blobs alias if available.
* Check if the given path uses the blobs alias.
*/
protected function buildPathConfigWithBlobs(): string
protected function isBlobsPath(string $path): bool
{
$pathConfig = (string)$this->getConf('paths');
$blobsRoot = $this->resolveBlobsRoot();
if ($blobsRoot !== '') {
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
$trimmed = ltrim($path, '/');
return preg_match('/^blobs(\/|$)/', $trimmed) === 1;
}
/**
* Render the "Page not linked" message with copy ID affordance.
*/
protected function renderPageNotLinked(\Doku_Renderer $renderer): void
{
$uuid = $this->getPageUuidSafe();
$text = (string)$this->getLang('pagelink_unlinked');
if ($renderer instanceof \Doku_Renderer_xhtml) {
$renderer->doc .= '<a href="#" class="luxtools-pagelink-copy" data-luxtools-pagelink-copy="1"'
. ' data-uuid="' . hsc($uuid) . '"'
. '>' . hsc($text) . '</a>';
return;
}
return $pathConfig;
$renderer->cdata('[n/a: ' . $text . ']');
}
/**
* Read the current page UUID (if any).
*/
protected function getPageUuidSafe(): string
{
global $ID;
$pageId = is_string($ID) ? $ID : '';
if ($pageId === '') return '';
if (function_exists('cleanID')) {
$pageId = (string)cleanID($pageId);
}
if ($pageId === '') return '';
$depth = (int)$this->getConf('pagelink_search_depth');
if ($depth < 0) $depth = 0;
$pageLink = new PageLink((string)$this->getConf('paths'), $depth);
$uuid = $pageLink->getPageUuid($pageId);
return $uuid ?? '';
}
/**

View File

@@ -1,6 +1,10 @@
<?php
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\plugin\luxtools\PageLink;
use dokuwiki\plugin\luxtools\Path;
require_once(__DIR__ . '/../autoload.php');
/**
* luxtools Plugin: Open local path syntax.
@@ -73,6 +77,41 @@ class syntax_plugin_luxtools_open extends SyntaxPlugin
return true;
}
// Resolve blobs alias to the linked folder (if available)
if ($this->isBlobsPath($path)) {
$blobsRoot = $this->resolveBlobsRoot();
if ($blobsRoot === '') {
$this->renderPageNotLinked($renderer);
return true;
}
try {
$pathConfig = (string)$this->getConf('paths');
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
$pathHelper = new Path($pathConfig);
$resolvedPath = $path;
$isBlobsRoot = (rtrim($resolvedPath, '/') === 'blobs');
if ($isBlobsRoot) {
$resolvedPath = rtrim($resolvedPath, '/') . '/';
}
$pathInfo = $pathHelper->getPathInfo($resolvedPath, $isBlobsRoot);
$path = $pathInfo['path'];
} catch (\Exception $e) {
$renderer->cdata('[n/a: ' . $this->getLang('error_outsidejail') . ']');
return true;
}
}
// Map local paths back to their configured aliases before opening.
if (!preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:/', $path)) {
try {
$pathHelper = new Path((string)$this->getConf('paths'));
$path = $pathHelper->mapToAliasPath($path);
} catch (\Exception $e) {
// ignore mapping failures
}
}
$serviceUrl = trim((string)$this->getConf('open_service_url'));
$serviceToken = trim((string)$this->getConf('open_service_token'));
@@ -106,4 +145,81 @@ class syntax_plugin_luxtools_open extends SyntaxPlugin
$renderer->doc .= $renderer->_formatLink($link);
return true;
}
/**
* Check if the given path uses the blobs alias.
*/
protected function isBlobsPath(string $path): bool
{
$trimmed = ltrim($path, '/');
return preg_match('/^blobs(\/|$)/', $trimmed) === 1;
}
/**
* Resolve the current page's pagelink folder for the blobs alias.
*/
protected function resolveBlobsRoot(): string
{
global $ID;
$pageId = is_string($ID) ? $ID : '';
if ($pageId === '') return '';
if (function_exists('cleanID')) {
$pageId = (string)cleanID($pageId);
}
if ($pageId === '') return '';
$depth = (int)$this->getConf('pagelink_search_depth');
if ($depth < 0) $depth = 0;
$pageLink = new PageLink((string)$this->getConf('paths'), $depth);
$uuid = $pageLink->getPageUuid($pageId);
if ($uuid === null) return '';
$linkInfo = $pageLink->resolveUuid($uuid);
$folder = $linkInfo['folder'] ?? '';
if (!is_string($folder) || $folder === '') return '';
return $folder;
}
/**
* Render the "Page not linked" message with copy ID affordance.
*/
protected function renderPageNotLinked(\Doku_Renderer $renderer): void
{
$uuid = $this->getPageUuidSafe();
$text = (string)$this->getLang('pagelink_unlinked');
if ($renderer instanceof \Doku_Renderer_xhtml) {
$renderer->doc .= '<a href="#" class="luxtools-pagelink-copy" data-luxtools-pagelink-copy="1"'
. ' data-uuid="' . hsc($uuid) . '"'
. '>' . hsc($text) . '</a>';
return;
}
$renderer->cdata('[n/a: ' . $text . ']');
}
/**
* Read the current page UUID (if any).
*/
protected function getPageUuidSafe(): string
{
global $ID;
$pageId = is_string($ID) ? $ID : '';
if ($pageId === '') return '';
if (function_exists('cleanID')) {
$pageId = (string)cleanID($pageId);
}
if ($pageId === '') return '';
$depth = (int)$this->getConf('pagelink_search_depth');
if ($depth < 0) $depth = 0;
$pageLink = new PageLink((string)$this->getConf('paths'), $depth);
$uuid = $pageLink->getPageUuid($pageId);
return $uuid ?? '';
}
}