1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 03:19:56 +00:00
TiddlyWiki5/js/macros/view.js
Jeremy Ruston b898afe3e5 Refactor the Tiddler object to enforce immutability
And in the process move the fields out of the fields member
2012-01-17 13:01:55 +00:00

42 lines
939 B
JavaScript

/*\
title: js/macros/view.js
\*/
(function(){
/*jslint node: true */
"use strict";
var utils = require("../Utils.js");
exports.macro = {
name: "view",
types: ["text/html","text/plain"],
params: {
field: {byPos: 0, type: "text", optional: false},
format: {byPos: 1, type: "text", optional: true},
template: {byPos: 2, type: "text", optional: true}
},
handler: function(type,tiddler,store,params) {
var v = tiddler[params.field],
encoder = type === "text/html" ? utils.htmlEncode : function(x) {return x;};
if(v) {
switch(params.format) {
case "link":
return store.renderMacro("link",type,tiddler,{target: v},encoder(v));
case "wikified":
return store.renderTiddler(type,tiddler.title);
case "date":
var template = params.template || "DD MMM YYYY";
return encoder(utils.formatDateString(v,template));
default: // "text"
return encoder(v);
}
}
return "";
}
};
})();