1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 03:19:56 +00:00
TiddlyWiki5/plugins/tiddlywiki/multiwikiserver/modules/route-get-wiki.js
Jeremy Ruston a980390870 Implement APIs for client wikis to sync with the server
It is now possible to create and edit tiddlers, using the existing tiddlywebadaptor syncing mechanism. There are a lot of hacks and lumpiness to make things compatible, so I think I will end up with an independent implementation
2024-01-03 16:27:13 +00:00

65 lines
1.8 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/multiwikiserver/route-get-wiki.js
type: application/javascript
module-type: route
GET /wikis/:recipe_name
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.method = "GET";
exports.path = /^\/wiki\/([^\/]+)$/;
exports.handler = function(request,response,state) {
// Get the recipe name from the parameters
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]);
// Get the tiddlers in the recipe
var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
// Render the template
var template = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
variables: {
saveTiddlerFilter: `
$:/boot/boot.css
$:/boot/boot.js
$:/boot/bootprefix.js
$:/core
$:/library/sjcl.js
$:/plugins/tiddlywiki/tiddlyweb
$:/themes/tiddlywiki/snowwhite
$:/themes/tiddlywiki/vanilla
`
}
});
// Splice in our tiddlers
var marker = `<` + `script class="tiddlywiki-tiddler-store" type="application/json">[`,
markerPos = template.indexOf(marker);
if(markerPos === -1) {
throw new Error("Cannot find tiddler store in template");
}
var htmlParts = [];
htmlParts.push(template.substring(0,markerPos + marker.length));
$tw.utils.each(titles,function(title) {
var tiddler = $tw.sqlTiddlerStore.getTiddler(title,recipe_name);
htmlParts.push(JSON.stringify(Object.assign({},tiddler,{revision: "0", bag: "bag-gamma"})));
htmlParts.push(",")
});
htmlParts.push(JSON.stringify({title: "$:/config/tiddlyweb/host",text: "$protocol$//$host$$pathname$/"}));
htmlParts.push(",")
htmlParts.push(template.substring(markerPos + marker.length))
// Send response
if(htmlParts) {
state.sendResponse(200,{"Content-Type": "text/html"},htmlParts.join("\n"),"utf8");
} else {
response.writeHead(404);
response.end();
}
};
}());