1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-12 03:00:28 +00:00
TiddlyWiki5/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-wiki.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-01-02 14:39:14 +00:00
/*\
2024-03-20 15:53:49 +00:00
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/get-wiki.js
2024-01-02 14:39:14 +00:00
type: application/javascript
module-type: mws-route
2024-01-02 14:39:14 +00:00
GET /wiki/:recipe_name
2024-01-02 14:39:14 +00:00
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.method = "GET";
exports.path = /^\/wiki\/([^\/]+)$/;
2024-01-02 14:39:14 +00:00
exports.handler = function(request,response,state) {
// Get the recipe name from the parameters
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
recipeTiddlers = recipe_name && $tw.mws.store.getRecipeTiddlers(recipe_name);
// Check request is valid
if(recipe_name && recipeTiddlers) {
// Start the response
response.writeHead(200, "OK",{
"Content-Type": "text/html"
});
// Get the tiddlers in the recipe
// Render the template
2024-02-05 14:49:08 +00:00
var template = $tw.mws.store.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
variables: {
saveTiddlerFilter: `
$:/boot/boot.css
$:/boot/boot.js
$:/boot/bootprefix.js
$:/core
$:/library/sjcl.js
$:/plugins/tiddlywiki/multiwikiclient
$:/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");
2024-01-02 14:39:14 +00:00
}
2024-03-20 17:56:47 +00:00
function writeTiddler(tiddlerFields) {
response.write(JSON.stringify(tiddlerFields).replace(/</g,"\\u003c"));
response.write(",\n");
}
response.write(template.substring(0,markerPos + marker.length));
2024-03-20 17:56:47 +00:00
const bagInfo = {},
revisionInfo = {};
$tw.utils.each(recipeTiddlers,function(recipeTiddlerInfo) {
2024-02-05 14:49:08 +00:00
var result = $tw.mws.store.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
2024-01-26 15:48:39 +00:00
if(result) {
2024-03-20 17:56:47 +00:00
bagInfo[result.tiddler.title] = result.bag_name;
revisionInfo[result.tiddler.title] = result.tiddler_id.toString();
writeTiddler(result.tiddler);
2024-01-26 15:48:39 +00:00
}
});
2024-03-20 17:56:47 +00:00
writeTiddler({
title: "$:/state/multiwikiclient/tiddlers/bag",
2024-03-20 17:56:47 +00:00
text: JSON.stringify(bagInfo),
type: "application/json"
});
writeTiddler({
title: "$:/state/multiwikiclient/tiddlers/revision",
2024-03-20 17:56:47 +00:00
text: JSON.stringify(revisionInfo),
type: "application/json"
});
writeTiddler({
title: "$:/config/multiwikiclient/recipe",
text: recipe_name
});
writeTiddler({
title: "$:/state/multiwikiclient/recipe/last_tiddler_id",
text: ($tw.mws.store.getRecipeLastTiddlerId(recipe_name) || 0).toString()
});
response.write(template.substring(markerPos + marker.length))
// Finish response
response.end();
2024-01-02 14:39:14 +00:00
} else {
response.writeHead(404);
response.end();
}
};
}());