1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-17 19:09:55 +00:00

Docs: format:json

Also tweak to the behaviour of format:json if the input string is not valid JSON
This commit is contained in:
jeremy@jermolene.com 2022-06-11 17:29:20 +01:00
parent 688c3bc5f6
commit 3e33d309ed
3 changed files with 18 additions and 7 deletions

View File

@ -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;
};

View File

@ -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 <<tag TableOfContents>> formatted as a title list :
All tiddler titles tagged with <<tag TableOfContents>> 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]]:

View File

@ -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.""">>