mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 00:50:28 +00:00
Add support for JSON files containing a single tiddler
At the moment, we support JSON files containing an array of tiddlers. With this change the core will import files containing a single tiddler. Also adding templates for saving individual tiddlers in JSON format
This commit is contained in:
parent
d65fd771e7
commit
779e62a30f
@ -1320,8 +1320,8 @@ $tw.modules.define("$:/boot/tiddlerdeserializer/html","tiddlerdeserializer",{
|
||||
});
|
||||
$tw.modules.define("$:/boot/tiddlerdeserializer/json","tiddlerdeserializer",{
|
||||
"application/json": function(text,fields) {
|
||||
var tiddlers = JSON.parse(text);
|
||||
return tiddlers;
|
||||
var data = JSON.parse(text);
|
||||
return $tw.utils.isArray(data) ? data : [data];
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -82,17 +82,18 @@ exports["application/json"] = function(text,fields) {
|
||||
text: ""
|
||||
}]
|
||||
}
|
||||
if($tw.utils.isArray(incoming)) {
|
||||
for(var t=0; t<incoming.length; t++) {
|
||||
var incomingFields = incoming[t],
|
||||
fields = {};
|
||||
for(var f in incomingFields) {
|
||||
if(typeof incomingFields[f] === "string") {
|
||||
fields[f] = incomingFields[f];
|
||||
}
|
||||
if(!$tw.utils.isArray(incoming)) {
|
||||
incoming = [incoming];
|
||||
}
|
||||
for(var t=0; t<incoming.length; t++) {
|
||||
var incomingFields = incoming[t],
|
||||
fields = {};
|
||||
for(var f in incomingFields) {
|
||||
if(typeof incomingFields[f] === "string") {
|
||||
fields[f] = incomingFields[f];
|
||||
}
|
||||
results.push(fields);
|
||||
}
|
||||
results.push(fields);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
40
core/modules/macros/jsontiddler.js
Normal file
40
core/modules/macros/jsontiddler.js
Normal file
@ -0,0 +1,40 @@
|
||||
/*\
|
||||
title: $:/core/modules/macros/jsontiddler.js
|
||||
type: application/javascript
|
||||
module-type: macro
|
||||
|
||||
Macro to output a single tiddler to JSON
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Information about this macro
|
||||
*/
|
||||
|
||||
exports.name = "jsontiddler";
|
||||
|
||||
exports.params = [
|
||||
{name: "title"}
|
||||
];
|
||||
|
||||
/*
|
||||
Run the macro
|
||||
*/
|
||||
exports.run = function(title) {
|
||||
title = title || this.getVariable("currentTiddler");
|
||||
var tiddler = !!title && this.wiki.getTiddler(title),
|
||||
fields = new Object();
|
||||
if(tiddler) {
|
||||
for(var field in tiddler.fields) {
|
||||
fields[field] = tiddler.getFieldString(field);
|
||||
}
|
||||
}
|
||||
return JSON.stringify(fields,null,$tw.config.preferences.jsonSpaces);
|
||||
};
|
||||
|
||||
})();
|
7
core/templates/json-tiddler.tid
Normal file
7
core/templates/json-tiddler.tid
Normal file
@ -0,0 +1,7 @@
|
||||
title: $:/core/templates/json-tiddler
|
||||
|
||||
<!--
|
||||
|
||||
This template is used for saving tiddlers as raw JSON
|
||||
|
||||
--><$text text=<<jsontiddler>>/>
|
13
editions/tw5.com/tiddlers/macros/jsontiddler Macro.tid
Normal file
13
editions/tw5.com/tiddlers/macros/jsontiddler Macro.tid
Normal file
@ -0,0 +1,13 @@
|
||||
caption: jsontiddlers
|
||||
created: 20170317140130417
|
||||
modified: 20170317140226040
|
||||
tags: Macros [[Core Macros]]
|
||||
title: jsontiddler Macro
|
||||
type: text/vnd.tiddlywiki
|
||||
|
||||
The <<.def jsontiddler>> [[macro|Macros]] returns the fields of a single tiddler in [[JSON|JavaScript Object Notation]] form.
|
||||
|
||||
!! Parameters
|
||||
|
||||
;title
|
||||
: The title of a tiddler
|
Loading…
Reference in New Issue
Block a user