From 3e33d309ed15941dc1ea019ae30ae7bf5bb59341 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Sat, 11 Jun 2022 17:29:20 +0100 Subject: [PATCH] Docs: format:json Also tweak to the behaviour of format:json if the input string is not valid JSON --- core/modules/filters/format/json.js | 9 ++++++++- .../filters/examples/format Operator (Examples).tid | 7 +++++-- editions/tw5.com/tiddlers/filters/format.tid | 9 +++++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/modules/filters/format/json.js b/core/modules/filters/format/json.js index 82acee0af..2130a76ed 100644 --- a/core/modules/filters/format/json.js +++ b/core/modules/filters/format/json.js @@ -20,7 +20,14 @@ exports.json = function(source,operand,options) { } source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title); - results.push(JSON.stringify(data,null,spaces)); + try { + data = JSON.parse(title); + } catch(e) { + data = undefined; + } + if(data !== undefined) { + results.push(JSON.stringify(data,null,spaces)); + } }); return results; }; diff --git a/editions/tw5.com/tiddlers/filters/examples/format Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/format Operator (Examples).tid index d7bbc1271..c1d79ea8c 100644 --- a/editions/tw5.com/tiddlers/filters/examples/format Operator (Examples).tid +++ b/editions/tw5.com/tiddlers/filters/examples/format Operator (Examples).tid @@ -1,5 +1,5 @@ created: 20201020102735123 -modified: 20210524044020645 +modified: 20220611104737314 tags: [[Operator Examples]] [[format Operator]] title: format Operator (Examples) type: text/vnd.tiddlywiki @@ -18,9 +18,12 @@ Modified date shown as a relative date: A tiddler title with spaces formatted as a title list: <<.operator-example 4 """[[Hello There]format:titlelist[]]""">> -All tiddler titles tagged with <> formatted as a title list : +All tiddler titles tagged with <> formatted as a title list: <<.operator-example 5 """[tag[TableOfContents]format:titlelist[]]""">> +A JSON string formatted as JSON – note how the JSON string is normalised to remove the duplicated properties: +<<.operator-example 6 """[[{"one":"first","one":"another","two":"second"}]format:json[]]""">> + <<.tip "To create a string to save a [[title list|Title List]] into a list field, use `format:titlelist[]` with the [[join operator|join Operator]]">> <<.operator-example 6 """[tag[TableOfContents]format:titlelist[]join[ ]]""">> For example, to save titles tagged `TableOfContents` to the titles field of the tiddler [[format titlelist test]]: diff --git a/editions/tw5.com/tiddlers/filters/format.tid b/editions/tw5.com/tiddlers/filters/format.tid index 9cc846139..e222e9d50 100644 --- a/editions/tw5.com/tiddlers/filters/format.tid +++ b/editions/tw5.com/tiddlers/filters/format.tid @@ -1,6 +1,6 @@ caption: format created: 20201020100834443 -modified: 20220523075550449 +modified: 20220611104737314 op-input: a [[selection of titles|Title Selection]] op-output: input strings formatted according to the specified suffix <<.place B>> op-parameter: optional format string for the formats @@ -17,9 +17,10 @@ type: text/vnd.tiddlywiki The suffix <<.place B>> is one of the following supported string formats: |!Format |!Description | -|^`date` |The input string is interpreted as a UTC date and displayed according to the DateFormat specified in the optional operator parameter. (Defaults to "YYYY MM DD 0hh:0mm") | -|^`relativedate` |The input string is interpreted as a UTC date and displayed as the interval from the present instant. Any operator parameters are ignored. | -|^`titlelist` |<<.from-version "5.2.0">>The input string wrapped in double square brackets if it contains a space. Appropriate for use in a [[title list|Title List]]. | +|^`date` |The input string is interpreted as a UTC date and displayed according to the DateFormat specified in the optional operator operand. (Defaults to "YYYY MM DD 0hh:0mm") | +|^`json` |<<.from-version "5.3.0">> The input string is interpreted as JSON and displayed with standard formatting. The optional operator operand specifies the number of spaces to use for indenting, or a string to use for indenting. Nothing is returned if the input string is not valid JSON | +|^`relativedate` |The input string is interpreted as a UTC date and displayed as the interval from the present instant. Any operator parameters are ignored | +|^`titlelist` |<<.from-version "5.2.0">> The input string wrapped in double square brackets if it contains a space. Appropriate for use in a [[title list|Title List]]. | <<.warning """The [[Title List]] format cannot reliably represent items that contain certain specific character sequences such as `]] `. Thus it should not be used where there is a possibility of such sequences occurring.""">>