1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

Merge branch 'master' into multi-wiki-support

This commit is contained in:
Jeremy Ruston 2024-04-12 09:59:12 +01:00
commit 28a831489b
12 changed files with 211 additions and 19 deletions

View File

@ -43,7 +43,9 @@ Saves individual tiddlers in their raw text or binary format to the specified fi
directory: path.resolve(self.commander.outputPath), directory: path.resolve(self.commander.outputPath),
pathFilters: [filenameFilter], pathFilters: [filenameFilter],
wiki: wiki, wiki: wiki,
fileInfo: {} fileInfo: {
overwrite: true
}
}); });
if(self.commander.verbose) { if(self.commander.verbose) {
console.log("Saving \"" + title + "\" to \"" + fileInfo.filepath + "\""); console.log("Saving \"" + title + "\" to \"" + fileInfo.filepath + "\"");

View File

@ -140,6 +140,11 @@ function sendResponse(request,response,statusCode,headers,data,encoding) {
return; return;
} }
} }
} else {
// RFC 7231, 6.1. Overview of Status Codes:
// Browser clients may cache 200, 203, 204, 206, 300, 301,
// 404, 405, 410, 414, and 501 unless given explicit cache controls
headers["Cache-Control"] = headers["Cache-Control"] || "no-store";
} }
/* /*
If the gzip=yes is set, check if the user agent permits compression. If so, If the gzip=yes is set, check if the user agent permits compression. If so,

View File

@ -316,11 +316,13 @@ Options include:
pathFilters: optional array of filters to be used to generate the base path pathFilters: optional array of filters to be used to generate the base path
wiki: optional wiki for evaluating the pathFilters wiki: optional wiki for evaluating the pathFilters
fileInfo: an existing fileInfo object to check against fileInfo: an existing fileInfo object to check against
fileInfo.overwrite: if true, turns off filename clash numbers (defaults to false)
*/ */
exports.generateTiddlerFilepath = function(title,options) { exports.generateTiddlerFilepath = function(title,options) {
var directory = options.directory || "", var directory = options.directory || "",
extension = options.extension || "", extension = options.extension || "",
originalpath = (options.fileInfo && options.fileInfo.originalpath) ? options.fileInfo.originalpath : "", originalpath = (options.fileInfo && options.fileInfo.originalpath) ? options.fileInfo.originalpath : "",
overwrite = options.fileInfo && options.fileInfo.overwrite || false,
filepath; filepath;
// Check if any of the pathFilters applies // Check if any of the pathFilters applies
if(options.pathFilters && options.wiki) { if(options.pathFilters && options.wiki) {
@ -381,19 +383,20 @@ exports.generateTiddlerFilepath = function(title,options) {
filepath += char.charCodeAt(0).toString(); filepath += char.charCodeAt(0).toString();
}); });
} }
// Add a uniquifier if the file already exists // Add a uniquifier if the file already exists (default)
var fullPath, oldPath = (options.fileInfo) ? options.fileInfo.filepath : undefined, var fullPath = path.resolve(directory, filepath + extension);
if (!overwrite) {
var oldPath = (options.fileInfo) ? options.fileInfo.filepath : undefined,
count = 0; count = 0;
do { do {
fullPath = path.resolve(directory,filepath + (count ? "_" + count : "") + extension); fullPath = path.resolve(directory,filepath + (count ? "_" + count : "") + extension);
if(oldPath && oldPath == fullPath) { if(oldPath && oldPath == fullPath) break;
break; count++;
} } while(fs.existsSync(fullPath));
count++; }
} while(fs.existsSync(fullPath));
// If the last write failed with an error, or if path does not start with: // If the last write failed with an error, or if path does not start with:
// the resolved options.directory, the resolved wikiPath directory, the wikiTiddlersPath directory, // the resolved options.directory, the resolved wikiPath directory, the wikiTiddlersPath directory,
// or the 'originalpath' directory, then $tw.utils.encodeURIComponentExtended() and resolve to tiddler directory. // or the 'originalpath' directory, then $tw.utils.encodeURIComponentExtended() and resolve to options.directory.
var writePath = $tw.hooks.invokeHook("th-make-tiddler-path",fullPath,fullPath), var writePath = $tw.hooks.invokeHook("th-make-tiddler-path",fullPath,fullPath),
encode = (options.fileInfo || {writeError: false}).writeError == true; encode = (options.fileInfo || {writeError: false}).writeError == true;
if(!encode) { if(!encode) {

View File

@ -37,6 +37,7 @@ Compute the internal state of the widget
DeleteFieldWidget.prototype.execute = function() { DeleteFieldWidget.prototype.execute = function() {
this.actionTiddler = this.getAttribute("$tiddler",this.getVariable("currentTiddler")); this.actionTiddler = this.getAttribute("$tiddler",this.getVariable("currentTiddler"));
this.actionField = this.getAttribute("$field",null); this.actionField = this.getAttribute("$field",null);
this.actionTimestamp = this.getAttribute("$timestamp","yes") === "yes";
}; };
/* /*
@ -69,11 +70,15 @@ DeleteFieldWidget.prototype.invokeAction = function(triggeringWidget,event) {
$tw.utils.each(this.attributes,function(attribute,name) { $tw.utils.each(this.attributes,function(attribute,name) {
if(name.charAt(0) !== "$" && name !== "title") { if(name.charAt(0) !== "$" && name !== "title") {
removeFields[name] = undefined; removeFields[name] = undefined;
hasChanged = true; if(name in tiddler.fields) {
hasChanged = true;
}
} }
}); });
if(hasChanged) { if(hasChanged) {
this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getCreationFields(),tiddler,removeFields,this.wiki.getModificationFields())); var creationFields = this.actionTimestamp ? this.wiki.getCreationFields() : {};
var modificationFields = this.actionTimestamp ? this.wiki.getModificationFields() : {};
this.wiki.addTiddler(new $tw.Tiddler(creationFields,tiddler,removeFields,modificationFields));
} }
} }
return true; // Action was invoked return true; // Action was invoked

View File

@ -3,7 +3,7 @@ title: $:/core/save/all-external-js
\whitespace trim \whitespace trim
\import [subfilter{$:/core/config/GlobalImportFilter}] \import [subfilter{$:/core/config/GlobalImportFilter}]
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/core]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$ [is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/core]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$
\end \end
<!-- Important: core library is provided by serving URI encoded $:/core/templates/tiddlywiki5.js --> <!-- Important: core library is provided by serving URI encoded $:/core/templates/tiddlywiki5.js -->

View File

@ -3,7 +3,7 @@ title: $:/core/save/offline-external-js
\whitespace trim \whitespace trim
\import [subfilter{$:/core/config/GlobalImportFilter}] \import [subfilter{$:/core/config/GlobalImportFilter}]
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/core]] -[[$:/plugins/tiddlywiki/filesystem]] -[[$:/plugins/tiddlywiki/tiddlyweb]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$ [is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/core]] -[[$:/plugins/tiddlywiki/filesystem]] -[[$:/plugins/tiddlywiki/tiddlyweb]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$
\end \end
\define defaultCoreURL() tiddlywikicore-$(version)$.js \define defaultCoreURL() tiddlywikicore-$(version)$.js
<$let coreURL={{{ [[coreURL]is[variable]then<coreURL>else<defaultCoreURL>] }}}> <$let coreURL={{{ [[coreURL]is[variable]then<coreURL>else<defaultCoreURL>] }}}>

View File

@ -2,6 +2,6 @@ title: $:/core/save/all
\import [subfilter{$:/core/config/GlobalImportFilter}] \import [subfilter{$:/core/config/GlobalImportFilter}]
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$ [is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]] -[status[pending]plugin-type[import]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$
\end \end
{{$:/core/templates/tiddlywiki5.html}} {{$:/core/templates/tiddlywiki5.html}}

View File

@ -1,6 +1,6 @@
title: $:/core/save/empty title: $:/core/save/empty
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[system]] -[prefix[$:/state/popup/]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]] [is[system]] -[prefix[$:/state/popup/]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] +[sort[title]]
\end \end
{{$:/core/templates/tiddlywiki5.html}} {{$:/core/templates/tiddlywiki5.html}}

View File

@ -1,7 +1,7 @@
title: $:/core/save/lazy-all title: $:/core/save/lazy-all
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[system]] -[prefix[$:/state/popup/]] -[[$:/HistoryList]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] [is[tiddler]type[application/javascript]] +[sort[title]] [is[system]] -[prefix[$:/state/popup/]] -[[$:/HistoryList]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] [is[tiddler]type[application/javascript]] +[sort[title]]
\end \end
\define skinnySaveTiddlerFilter() \define skinnySaveTiddlerFilter()
[!is[system]] -[type[application/javascript]] [!is[system]] -[type[application/javascript]]

View File

@ -1,7 +1,7 @@
title: $:/core/save/lazy-images title: $:/core/save/lazy-images
\define saveTiddlerFilter() \define saveTiddlerFilter()
[is[tiddler]] -[prefix[$:/state/popup/]] -[[$:/HistoryList]] -[[$:/boot/boot.css]] -[type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] -[!is[system]is[image]] +[sort[title]] [is[tiddler]] -[prefix[$:/state/popup/]] -[[$:/HistoryList]] -[[$:/boot/boot.css]] -[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]] -[[$:/boot/bootprefix.js]] -[!is[system]is[image]] +[sort[title]]
\end \end
\define skinnySaveTiddlerFilter() \define skinnySaveTiddlerFilter()
[!is[system]is[image]] [!is[system]is[image]]

View File

@ -0,0 +1,176 @@
/*\
title: test-action-deletefield.js
type: application/javascript
tags: [[$:/tags/test-spec]]
Tests <$action-deletefield />.
\*/
(function(){
/* jslint node: true, browser: true */
/* eslint-env node, browser, jasmine */
/* eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/
/* global $tw, require */
"use strict";
describe("<$action-deletefield /> tests", function() {
const TEST_TIDDLER_TITLE = "TargetTiddler";
const TEST_TIDDLER_MODIFIED = "20240313114828368";
function setupWiki(condition, targetField, wikiOptions) {
// Create a wiki
var wiki = new $tw.Wiki({});
var tiddlers = [{
title: "Root",
text: "Some dummy content"
}];
var tiddler;
if(condition.targetTiddlerExists) {
var fields = {
title: TEST_TIDDLER_TITLE,
};
if(condition.modifiedFieldExists) {
fields.modified = TEST_TIDDLER_MODIFIED;
}
if(condition.targetFieldExists) {
fields[targetField] = "some text";
}
var tiddler = new $tw.Tiddler(fields);
tiddlers.push(tiddler);
}
wiki.addTiddlers(tiddlers);
wiki.addIndexersToWiki();
var widgetNode = wiki.makeTranscludeWidget("Root",{document: $tw.fakeDocument, parseAsInline: true});
var container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
return {
wiki: wiki,
widgetNode: widgetNode,
contaienr: container,
tiddler: tiddler,
};
}
function generateTestConditions() {
var conditions = [];
$tw.utils.each([true, false], function(tiddlerArgumentIsPresent) {
$tw.utils.each([true, false], function(targetTiddlerExists) {
$tw.utils.each([true, false], function(targetFieldExists) {
$tw.utils.each([true, false], function(fieldArgumentIsUsed) {
$tw.utils.each([true, false], function(modifiedFieldExists) {
$tw.utils.each(["", "yes", "no"], function(timestampArgument) {
conditions.push({
tiddlerArgumentIsPresent: tiddlerArgumentIsPresent,
targetTiddlerExists: targetTiddlerExists,
targetFieldExists: targetFieldExists,
fieldArgumentIsUsed: fieldArgumentIsUsed,
modifiedFieldExists: modifiedFieldExists,
timestampArgument: timestampArgument,
});
});
});
});
});
});
});
return conditions;
}
function generateActionWikitext(condition, targetField) {
var actionPieces = [
"<$action-deletefield",
(condition.tiddlerArgumentIsPresent ? "$tiddler='" + TEST_TIDDLER_TITLE + "'" : ""),
(condition.fieldArgumentIsUsed ? "$field='" + targetField + "'" : targetField),
(condition.timestampArgument !== "" ? "$timestamp='" + condition.timestampArgument + "'" : ""),
"/>",
];
return actionPieces.join(" ");
}
function generateTestContext(action, tiddler) {
var expectationContext = "action: " + action + "\ntiddler:\n\n";
if(tiddler) {
expectationContext += tiddler.getFieldStringBlock({exclude: ["text"]});
if(tiddler.text) {
expectationContext += "\n\n" + tiddler.text;
}
expectationContext += "\n\n";
} else {
expectationContext += "null";
}
return expectationContext;
}
it("should correctly delete fields", function() {
var fields = ['caption', 'description', 'text'];
var conditions = generateTestConditions();
$tw.utils.each(conditions, function(condition) {
$tw.utils.each(fields, function(field) {
var info = setupWiki(condition, field);
var originalTiddler = info.tiddler;
var invokeActions = function(actions) {
info.widgetNode.invokeActionString(actions,info.widgetNode,null,{
currentTiddler: TEST_TIDDLER_TITLE,
});
};
var action = generateActionWikitext(condition,field);
invokeActions(action);
var testContext = generateTestContext(action,originalTiddler);
var tiddler = info.wiki.getTiddler(TEST_TIDDLER_TITLE);
if(originalTiddler) {
// assert that the tiddler doesn't have the target field anymore
expect(tiddler.hasField(field)).withContext(testContext).toBeFalsy();
var targetFieldWasPresent = originalTiddler.hasField(field);
var updateTimestamps = condition.timestampArgument !== "no";
// "created" should exist if it did beforehand, or if the tiddler changed and we asked the widget to update timestamps
var createdFieldShouldExist = originalTiddler.hasField("created") || (targetFieldWasPresent && updateTimestamps);
// "created" should change only if it didn't exist beforehand and the tiddler changed and we asked the widget to update timestamps
var createdFieldShouldChange = !originalTiddler.hasField("created") && (targetFieldWasPresent && updateTimestamps);
// "modified" should exist if it did beforehand, or if the tiddler changed and we asked the widget to update timestamps
var modifiedFieldShouldExist = originalTiddler.hasField("modified") || (targetFieldWasPresent && updateTimestamps);
// "modified" should change if the tiddler changed and we asked the widget to update timestamps
var modifiedFieldShouldChange = targetFieldWasPresent && updateTimestamps;
expect(tiddler.hasField("created")).withContext(testContext).toBe(createdFieldShouldExist);
expect(tiddler.hasField("modified")).withContext(testContext).toBe(modifiedFieldShouldExist);
if(createdFieldShouldChange) {
expect(tiddler.fields.created).withContext(testContext).not.toEqual(originalTiddler.fields.created);
} else {
expect(tiddler.fields.created).withContext(testContext).toEqual(originalTiddler.fields.created);
}
if(modifiedFieldShouldChange) {
expect(tiddler.fields.modified).withContext(testContext).not.toEqual(originalTiddler.fields.modified);
} else {
expect(tiddler.fields.modified).withContext(testContext).toEqual(originalTiddler.fields.modified);
}
} else {
// assert that the tiddler didn't get created if it didn't exist already
expect(tiddler).withContext(testContext).toBeUndefined();
}
});
});
});
});
})();

View File

@ -16,6 +16,7 @@ The ''action-deletefield'' widget is invisible. Any content within it is ignored
|!Attribute |!Description | |!Attribute |!Description |
|$tiddler |The title of the tiddler whose fields are to be modified (if not provided defaults to the [[current tiddler|Current Tiddler]]) | |$tiddler |The title of the tiddler whose fields are to be modified (if not provided defaults to the [[current tiddler|Current Tiddler]]) |
|$field |Optional name of a field to delete | |$field |Optional name of a field to delete |
|$timestamp |<<.from-version "5.3.4">> Specifies whether the timestamp(s) of the target tiddler will be updated (''modified'' and ''modifier'', plus ''created'' and ''creator'' for newly created tiddlers). Can be "yes" (the default) or "no" |
|//{any attributes not starting with $}// |Each attribute name specifies a field to be deleted. The attribute value is ignored and need not be specified | |//{any attributes not starting with $}// |Each attribute name specifies a field to be deleted. The attribute value is ignored and need not be specified |
! Examples ! Examples