1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 00:50:28 +00:00

Format filter to create formatted date strings (#4785)

* asdate filter to create formatted date strings

* Added format filter operator
This commit is contained in:
saqimtiaz 2020-09-24 19:35:10 +02:00 committed by GitHub
parent bdaf3a4502
commit 72c07a3f81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 0 deletions

View File

@ -27,6 +27,7 @@ Error/EditConflict: File changed on server
Error/Filter: Filter error
Error/FilterSyntax: Syntax error in filter expression
Error/IsFilterOperator: Filter Error: Unknown operand for the 'is' filter operator
Error/FormatFilterOperator: Filter Error: Unknown suffix for the 'format' filter operator
Error/LoadingPluginLibrary: Error loading plugin library
Error/NetworkErrorAlert: `<h2>''Network Error''</h2>It looks like the connection to the server has been lost. This may indicate a problem with your network connection. Please attempt to restore network connectivity before continuing.<br><br>''Any unsaved changes will be automatically synchronised when connectivity is restored''.`
Error/RecursiveTransclusion: Recursive transclusion error in transclude widget

View File

@ -0,0 +1,46 @@
/*\
title: $:/core/modules/filters/format.js
type: application/javascript
module-type: filteroperator
Filter operator for formatting strings
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var formatFilterOperators;
function getFormatFilterOperators() {
if(!formatFilterOperators) {
formatFilterOperators = {};
$tw.modules.applyMethods("formatfilteroperator",formatFilterOperators);
}
return formatFilterOperators;
}
/*
Export our filter function
*/
exports.format = function(source,operator,options) {
// Dispatch to the correct formatfilteroperator
var formatFilterOperators = getFormatFilterOperators();
if(operator.suffix) {
var formatFilterOperator = formatFilterOperators[operator.suffix];
if(formatFilterOperator) {
return formatFilterOperator(source,operator.operand,options);
} else {
return [$tw.language.getString("Error/FormatFilterOperator")];
}
} else {
// Return all unchanged if the suffix is missing
var results = [];
source(function(tiddler,title) {
results.push(title);
});
return results;
}
};
})();

View File

@ -0,0 +1,26 @@
/*\
title: $:/core/modules/filters/format/date.js
type: application/javascript
module-type: formatfilteroperator
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.date = function(source,operand,options) {
var results = [];
source(function(tiddler,title) {
var value = $tw.utils.parseDate(title);
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
results.push($tw.utils.formatDateString(value,operand || "YYYY MM DD 0hh:0mm"));
}
});
return results;
};
})();

View File

@ -0,0 +1,26 @@
/*\
title: $:/core/modules/filters/format/relativedate.js
type: application/javascript
module-type: formatfilteroperator
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.relativedate = function(source,operand,options) {
var results = [];
source(function(tiddler,title) {
var value = $tw.utils.parseDate(title);
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
results.push($tw.utils.getRelativeDate((new Date()) - (new Date(value))).description);
}
});
return results;
};
})();