1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-08 10:59:57 +00:00
TiddlyWiki5/js/macros/view.js
Jeremy Ruston a0524c2144 Added protection against wikitext recursion
Now attempts to view a tiddler recursively fail gracefully
2012-02-19 17:20:16 +00:00

76 lines
1.9 KiB
JavaScript

/*\
title: js/macros/view.js
\*/
(function(){
/*jslint node: true */
"use strict";
var Renderer = require("../Renderer.js").Renderer,
Dependencies = require("../Dependencies.js").Dependencies,
utils = require("../Utils.js");
exports.macro = {
name: "view",
types: ["text/html","text/plain"],
params: {
field: {byPos: 0, type: "text"},
format: {byPos: 1, type: "text"},
template: {byPos: 2, type: "text"}
},
execute: function(macroNode,tiddler,store) {
if(!tiddler) {
return Renderer.TextNode("{{** Missing tiddler **}}");
} else {
var v = tiddler[macroNode.params.field],
content,
t,
contentClone = [],
parents = macroNode.parents;
if(v !== undefined) {
switch(macroNode.params.format) {
case "link":
var dependencies = new Dependencies();
dependencies.addDependency(v,false);
var link = Renderer.MacroNode("link",
{target: v},
[Renderer.TextNode(v)],
dependencies,
store);
link.execute(parents,tiddler);
return [link];
case "wikified":
if(macroNode.params.field === "text") {
if(parents.indexOf(tiddler.title) === -1) {
content = store.parseTiddler(tiddler.title).tree;
} else {
content = [Renderer.TextNode("{{** Tiddler recursion error in <<view>> macro **}}")];
}
parents = parents.slice(0);
parents.push(tiddler.title);
} else {
content = store.parseText("text/x-tiddlywiki",v).tree;
}
for(t=0; t<content.length; t++) {
contentClone.push(content[t].clone());
}
for(t=0; t<contentClone.length; t++) {
contentClone[t].execute(parents,tiddler);
}
return contentClone;
case "date":
var template = macroNode.params.template || "DD MMM YYYY";
return [Renderer.TextNode(utils.formatDateString(v,template))];
default: // "text"
return [Renderer.TextNode(v)];
}
}
}
return [];
}
};
})();