From 259b3dca1b605c53e7861eb1422af68700768500 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 18 Mar 2024 22:26:24 +0000 Subject: [PATCH] Add a new static index route --- .../modules/routes/handlers/get-bag.js | 15 +---- .../modules/routes/handlers/get-index.js | 44 +++++++++++++ .../multiwikiserver/templates/get-index.tid | 62 +++++++++++++++++++ .../multiwikiserver/templates/page.tid | 19 ++++++ 4 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-index.js create mode 100644 plugins/tiddlywiki/multiwikiserver/templates/get-index.tid create mode 100644 plugins/tiddlywiki/multiwikiserver/templates/page.tid diff --git a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag.js b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag.js index cc74f4341..d390ab217 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag.js @@ -38,26 +38,15 @@ exports.handler = function(request,response,state) { response.writeHead(200, "OK",{ "Content-Type": "text/html" }); - response.write(` - - - - - - `); - // Render the html - var html = $tw.mws.store.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bag",{ + var html = $tw.mws.store.adminWiki.renderTiddler("text/plain","$:/plugins/tiddlywiki/multiwikiserver/templates/page",{ variables: { + "page-content": "$:/plugins/tiddlywiki/multiwikiserver/templates/get-bag", "bag-name": bag_name, "bag-titles": JSON.stringify(bagTiddlers.map(bagTiddler => bagTiddler.title)), "bag-tiddlers": JSON.stringify(bagTiddlers) } }); response.write(html); - response.write(` - - - `); response.end(); } } else { diff --git a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-index.js b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-index.js new file mode 100644 index 000000000..21eced396 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-index.js @@ -0,0 +1,44 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/get-index.js +type: application/javascript +module-type: mws-route + +GET / + +\*/ +(function() { + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.method = "GET"; + +exports.path = /^\/$/; + +exports.handler = function(request,response,state) { + // Get the bag and recipe information + var bagList = $tw.mws.store.listBags(), + recipeList = $tw.mws.store.listRecipes(); + // 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) { + state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(recipes),"utf8"); + } else { + // This is not a JSON API request, we should return the raw tiddler content + response.writeHead(200, "OK",{ + "Content-Type": "text/html" + }); + // Render the html + var html = $tw.mws.store.adminWiki.renderTiddler("text/plain","$:/plugins/tiddlywiki/multiwikiserver/templates/page",{ + variables: { + "page-content": "$:/plugins/tiddlywiki/multiwikiserver/templates/get-index", + "bag-list": JSON.stringify(bagList), + "recipe-list": JSON.stringify(recipeList) + } + }); + response.write(html); + response.end(); + } +}; + +}()); diff --git a/plugins/tiddlywiki/multiwikiserver/templates/get-index.tid b/plugins/tiddlywiki/multiwikiserver/templates/get-index.tid new file mode 100644 index 000000000..3ea3970c9 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/templates/get-index.tid @@ -0,0 +1,62 @@ +title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index + +! Wikis Available Here + + + +! Bags + + diff --git a/plugins/tiddlywiki/multiwikiserver/templates/page.tid b/plugins/tiddlywiki/multiwikiserver/templates/page.tid new file mode 100644 index 000000000..91123a22f --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/templates/page.tid @@ -0,0 +1,19 @@ +title: $:/plugins/tiddlywiki/multiwikiserver/templates/page + + +` + + + + + +` +<$view tiddler=<> field="text" format="htmlwikified" /> +` + + +`