Refactor #5786 for backwards compatibility

new-json-store-area
jeremy@jermolene.com 2021-06-13 14:08:18 +01:00
rodzic 4110083d94
commit 275e3abe06
3 zmienionych plików z 20 dodań i 15 usunięć

Wyświetl plik

@ -702,7 +702,7 @@ exports.getTiddlerAsJson = function(title) {
/* /*
Options: Options:
spaces: number of spaces spaces: number of spaces. The special value -1 results in a line break in between each tiddler
escapeUnsafeScriptCharacters: true to force escaping of characters that cannot be embedded in HTML escapeUnsafeScriptCharacters: true to force escaping of characters that cannot be embedded in HTML
*/ */
exports.getTiddlersAsJson = function(filter,options) { exports.getTiddlersAsJson = function(filter,options) {
@ -710,21 +710,26 @@ exports.getTiddlersAsJson = function(filter,options) {
if(typeof options === "string") { if(typeof options === "string") {
options = {spaces: options}; options = {spaces: options};
} }
var tiddlers = this.filterTiddlers(filter), var titles = this.filterTiddlers(filter),
spaces = (options.spaces === undefined) ? $tw.config.preferences.jsonSpaces : options.spaces, spaces = (options.spaces === undefined) ? $tw.config.preferences.jsonSpaces : options.spaces,
jsonLines = [], data = [],
t,fields,tiddler; t;
for(t=0;t<tiddlers.length; t++) { for(t=0;t<titles.length; t++) {
tiddler = this.getTiddler(tiddlers[t]); var tiddler = this.getTiddler(titles[t]);
if(tiddler) { if(tiddler) {
fields = new Object(); data.push(tiddler.getFieldStrings());
for(var field in tiddler.fields) {
fields[field] = tiddler.getFieldString(field);
}
jsonLines.push(JSON.stringify(fields,null,spaces));
} }
} }
var json = "[\n" + jsonLines.join(",\n") + "\n]"; var json;
if(spaces === -1) {
var jsonLines = [];
for(t=0;t<data.length; t++) {
jsonLines.push(JSON.stringify(data[t]));
}
json = "[\n" + jsonLines.join(",\n") + "\n]";
} else {
json = JSON.stringify(data,null,spaces);
}
if(options.escapeUnsafeScriptCharacters) { if(options.escapeUnsafeScriptCharacters) {
function escapeUnsafeChars(unsafeChar) { function escapeUnsafeChars(unsafeChar) {
return { return {

Wyświetl plik

@ -6,7 +6,7 @@ title: $:/core/templates/store.area.template.html
<$list filter="[[storeAreaFormat]is[variable]getvariable[]else[json]match[json]]"> <$list filter="[[storeAreaFormat]is[variable]getvariable[]else[json]match[json]]">
<!-- New-style JSON store area, with an old-style store area for compatibility with v5.1.x tooling --> <!-- New-style JSON store area, with an old-style store area for compatibility with v5.1.x tooling -->
`<script class="tiddlywiki-tiddler-store" type="application/json">` `<script class="tiddlywiki-tiddler-store" type="application/json">`
<$macrocall $name="jsontiddlers" filter=<<saveTiddlerFilter>> escapeUnsafeScriptCharacters="yes" $output="text/raw"/> <$macrocall $name="jsontiddlers" filter=<<saveTiddlerFilter>> spaces="-1" escapeUnsafeScriptCharacters="yes" $output="text/raw"/>
`</script>` `</script>`
`<div id="storeArea" style="display:none;">` `<div id="storeArea" style="display:none;">`
`</div>` `</div>`

Wyświetl plik

@ -1,5 +1,5 @@
created: 20150221152226000 created: 20150221152226000
modified: 20200204135513721 modified: 20210613140528007
tags: Macros [[Core Macros]] tags: Macros [[Core Macros]]
title: jsontiddlers Macro title: jsontiddlers Macro
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -14,4 +14,4 @@ An example can be seen in the [[template tiddler for JSON exports|$:/core/templa
;filter ;filter
: A [[filter|Filters]] selecting which tiddlers to include : A [[filter|Filters]] selecting which tiddlers to include
;spaces ;spaces
: An optional number of spaces to use for formatting the JSON. Defaults to 4, with blank or zero resulting in packed JSON with no formatting spaces : An optional number of spaces to use for formatting the JSON. Defaults to 4, with blank or zero resulting in packed JSON with no formatting spaces. <<.from-version "5.2.0">> The special value -1 results in a line break in between each tiddler