1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-07 07:50:26 +00:00

Get rid of those annoying missing image icons

By adding a `?fallback=url` parameter to tiddler requests
This commit is contained in:
Jeremy Ruston 2024-03-20 15:40:38 +00:00
parent 9b3ca525ee
commit ae8ef305fa
4 changed files with 49 additions and 21 deletions

View File

@ -5,6 +5,9 @@ module-type: mws-route
GET /bags/:bag_name/tiddler/:title GET /bags/:bag_name/tiddler/:title
Parameters:
fallback=<url> // Optional redirect if the tiddler is not found
\*/ \*/
(function() { (function() {
@ -19,17 +22,17 @@ exports.path = /^\/bags\/([^\/]+)\/tiddlers\/(.+)$/;
exports.handler = function(request,response,state) { exports.handler = function(request,response,state) {
// Get the parameters // Get the parameters
var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]), const bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
title = $tw.utils.decodeURIComponentSafe(state.params[1]), title = $tw.utils.decodeURIComponentSafe(state.params[1]),
result = bag_name && $tw.mws.store.getBagTiddler(title,bag_name); tiddlerInfo = $tw.mws.store.getBagTiddler(title,bag_name);
if(bag_name && result) { if(tiddlerInfo && tiddlerInfo.tiddler) {
// If application/json is requested then this is an API request, and gets the response in JSON // If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
var tiddlerFields = {}, var tiddlerFields = {},
knownFields = [ knownFields = [
"bag", "created", "creator", "modified", "modifier", "permissions", "recipe", "revision", "tags", "text", "title", "type", "uri" "bag", "created", "creator", "modified", "modifier", "permissions", "recipe", "revision", "tags", "text", "title", "type", "uri"
]; ];
$tw.utils.each(result.tiddler,function(value,name) { $tw.utils.each(tiddlerInfo.tiddler,function(value,name) {
if(knownFields.indexOf(name) !== -1) { if(knownFields.indexOf(name) !== -1) {
tiddlerFields[name] = value; tiddlerFields[name] = value;
} else { } else {
@ -49,11 +52,26 @@ exports.handler = function(request,response,state) {
}); });
result.stream.pipe(response); result.stream.pipe(response);
return; return;
} else {
response.writeHead(404);
response.end();
return;
} }
} }
} else {
// Redirect to fallback URL if tiddler not found
if(state.queryParameters.fallback) {
response.writeHead(302, "OK",{
"Location": state.queryParameters.fallback
});
response.end();
return;
} else {
response.writeHead(404);
response.end();
return;
}
} }
response.writeHead(404);
response.end();
}; };
}()); }());

View File

@ -5,6 +5,10 @@ module-type: mws-route
GET /recipes/:recipe_name/tiddler/:title GET /recipes/:recipe_name/tiddler/:title
Parameters:
fallback=<url> // Optional redirect if the tiddler is not found
\*/ \*/
(function() { (function() {
@ -20,8 +24,8 @@ exports.handler = function(request,response,state) {
// Get the parameters // Get the parameters
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]), var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
title = $tw.utils.decodeURIComponentSafe(state.params[1]), title = $tw.utils.decodeURIComponentSafe(state.params[1]),
tiddlerInfo = recipe_name && $tw.mws.store.getRecipeTiddler(title,recipe_name); tiddlerInfo = $tw.mws.store.getRecipeTiddler(title,recipe_name);
if(recipe_name && tiddlerInfo && tiddlerInfo.tiddler) { if(tiddlerInfo && tiddlerInfo.tiddler) {
// If application/json is requested then this is an API request, and gets the response in JSON // If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
var tiddlerFields = {}, var tiddlerFields = {},
@ -38,6 +42,7 @@ exports.handler = function(request,response,state) {
}); });
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki"; tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8"); state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
return;
} else { } else {
// This is not a JSON API request, we should return the raw tiddler content // This is not a JSON API request, we should return the raw tiddler content
var type = tiddlerInfo.tiddler.type || "text/plain"; var type = tiddlerInfo.tiddler.type || "text/plain";
@ -46,10 +51,21 @@ exports.handler = function(request,response,state) {
}); });
response.write(tiddlerInfo.tiddler.text || "",($tw.config.contentTypeInfo[type] ||{encoding: "utf8"}).encoding); response.write(tiddlerInfo.tiddler.text || "",($tw.config.contentTypeInfo[type] ||{encoding: "utf8"}).encoding);
response.end();; response.end();;
return;
} }
} else { } else {
response.writeHead(404); // Redirect to fallback URL if tiddler not found
response.end(); if(state.queryParameters.fallback) {
response.writeHead(302, "OK",{
"Location": state.queryParameters.fallback
});
response.end();
return;
} else {
response.writeHead(404);
response.end();
return;
}
} }
}; };

View File

@ -1,16 +1,10 @@
title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bag title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bag
! <$image ! <img
source=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` src=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png`
class="mws-favicon-small" class="mws-favicon-small"
width="32px" width="32px"
> /> Bag <$text text={{{ [<bag-name>]}}}/>
<$image
source="$:/plugins/multiwikiserver/images/missing-favicon.png"
class="mws-favicon-small"
width="32px"
/>
</$image> Bag <$text text={{{ [<bag-name>]}}}/>
<form <form
method="post" method="post"

View File

@ -5,7 +5,7 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index
<$genesis $type=<<element-tag>> class={{{ mws-bag-pill [<is-topmost>match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}> <$genesis $type=<<element-tag>> class={{{ mws-bag-pill [<is-topmost>match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}>
<a class="mws-bag-pill-link" href=`/bags/${ [<bag-name>encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank"> <a class="mws-bag-pill-link" href=`/bags/${ [<bag-name>encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank">
<img <img
src=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` src=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png`
class="mws-favicon-small" class="mws-favicon-small"
/> />
<span class="mws-bag-pill-label"> <span class="mws-bag-pill-label">
@ -29,7 +29,7 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index
> >
<div class="mws-wiki-card-image"> <div class="mws-wiki-card-image">
<img <img
src=`/recipes/${ [<recipe-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` src=`/recipes/${ [<recipe-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png`
class="mws-favicon" class="mws-favicon"
/> />
</div> </div>