diff --git a/core/modules/filters/format/titlelist.js b/core/modules/filters/format/titlelist.js new file mode 100644 index 000000000..47608323d --- /dev/null +++ b/core/modules/filters/format/titlelist.js @@ -0,0 +1,25 @@ +/*\ +title: $:/core/modules/filters/format/titlelist.js +type: application/javascript +module-type: formatfilteroperator +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.titlelist = function(source,operand,options) { + var results = []; + source(function(tiddler,title) { + if(title && title.length) { + results.push($tw.utils.stringifyList([title])); + } + }); + return results; +}; + +})(); diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index a02114f2b..ee428b1a0 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -808,6 +808,12 @@ function runTests(wiki) { expect(wiki.filterTiddlers("'-25' +[escapecss[]]").join(",")).toBe("-\\32 5"); expect(wiki.filterTiddlers("'-' +[escapecss[]]").join(",")).toBe("\\-"); }); + + it("should handle the format operator", function() { + expect(wiki.filterTiddlers("[[Hello There]] [[GettingStarted]] +[format:titlelist[]]").join(" ")).toBe("[[Hello There]] GettingStarted"); + expect(wiki.filterTiddlers("[title[Hello There]] +[format:titlelist[]]").join(" ")).toBe("[[Hello There]]"); + expect(wiki.filterTiddlers("[title[HelloThere]] +[format:titlelist[]]").join(" ")).toBe("HelloThere"); + }); it("should handle the deserializers operator", function() { expect(wiki.filterTiddlers("[deserializers[]]").join(",")).toBe("application/javascript,application/json,application/x-tiddler,application/x-tiddler-html-div,application/x-tiddlers,text/css,text/html,text/plain"); 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 071d4506d..d7bbc1271 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: 20201020104004306 +modified: 20210524044020645 tags: [[Operator Examples]] [[format Operator]] title: format Operator (Examples) type: text/vnd.tiddlywiki @@ -14,3 +14,18 @@ Created date with a format string supplied as operator parameter: Modified date shown as a relative date: <<.operator-example 3 "[[HelloThere]get[modified]format:relativedate[]]">> + +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 : +<<.operator-example 5 """[tag[TableOfContents]format:titlelist[]]""">> + +<<.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]]: + +<$macrocall $name='wikitext-example-without-html' +src='<$button> test +<$action-setfield $tiddler="format titlelist test" titles={{{ [tag[TableOfContents]format:titlelist[]join[ ]] }}}/> +'/> diff --git a/editions/tw5.com/tiddlers/filters/format.tid b/editions/tw5.com/tiddlers/filters/format.tid index 8cf6f9826..138eb018f 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: 20201020104004271 +modified: 20210524090002126 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 @@ -19,5 +19,6 @@ 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]]. | <<.operator-examples "format">>