diff --git a/core/modules/macros/unusedtitle.js b/core/modules/macros/unusedtitle.js index 952bd0264..8a61c425e 100644 --- a/core/modules/macros/unusedtitle.js +++ b/core/modules/macros/unusedtitle.js @@ -16,20 +16,22 @@ exports.name = "unusedtitle"; exports.params = [ {name: "baseName"}, {name: "separator"}, - {name: "template"} + {name: "template"}, + {name: "startCount"} ]; /* Run the macro */ -exports.run = function(baseName,separator,template) { +exports.run = function(baseName,separator,template,startCount) { separator = separator || " "; + startCount = startCount || 0; if(!baseName) { baseName = $tw.language.getString("DefaultNewTiddlerTitle"); } // $tw.wiki.generateNewTitle = function(baseTitle,options) - // options.prefix must be a string! - return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template}); + // options.prefix must be a string! + return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template, "startCount": startCount}); }; })(); diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index 878f83fbb..234de0c75 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -330,16 +330,18 @@ exports.formatTitleString = function(template,options) { }] ]; while(t.length){ - var matchString = ""; + var matchString = "", + found = false; $tw.utils.each(matches, function(m) { var match = m[0].exec(t); if(match) { + found = true; matchString = m[1].call(null,match); t = t.substr(match[0].length); return false; } }); - if(matchString) { + if(found) { result += matchString; } else { result += t.charAt(0); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 5673c9e3b..29a839c7a 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -194,18 +194,24 @@ options.prefix must be a string */ exports.generateNewTitle = function(baseTitle,options) { options = options || {}; - var c = 0, - title = baseTitle, - template = options.template, + var title = baseTitle, + template = options.template || "", + // test if .startCount is a positive integer. If not set to 0 + c = (parseInt(options.startCount,10) > 0) ? parseInt(options.startCount,10) : 0, prefix = (typeof(options.prefix) === "string") ? options.prefix : " "; + if (template) { // "count" is important to avoid an endless loop in while(...)!! template = (/\$count:?(\d+)?\$/i.test(template)) ? template : template + "$count$"; - title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":c}); + // .formatTitleString() expects strings as input + title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":c+""}); while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { - title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":(++c)}); + title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":(++c)+""}); } } else { + if (c > 0) { + title = baseTitle + prefix + c; + } while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { title = baseTitle + prefix + (++c); } diff --git a/editions/test/tiddlers/tests/data/macros/unusedtitle/basic-params.tid b/editions/test/tiddlers/tests/data/macros/unusedtitle/basic-params.tid index 5e1ae482d..c0ec65ec0 100644 --- a/editions/test/tiddlers/tests/data/macros/unusedtitle/basic-params.tid +++ b/editions/test/tiddlers/tests/data/macros/unusedtitle/basic-params.tid @@ -14,7 +14,15 @@ title: Output <> + + +<> + +<> + +<> + + title: ExpectedResult -

New Tiddler

New Tiddler

anotherBase

About

\ No newline at end of file +

New Tiddler

New Tiddler

anotherBase

About

New Tiddler 3

invalid start

invalid count

\ No newline at end of file diff --git a/editions/test/tiddlers/tests/data/macros/unusedtitle/template-startCount.tid b/editions/test/tiddlers/tests/data/macros/unusedtitle/template-startCount.tid new file mode 100644 index 000000000..83fc1686b --- /dev/null +++ b/editions/test/tiddlers/tests/data/macros/unusedtitle/template-startCount.tid @@ -0,0 +1,27 @@ +title: Macros/unusedtitle/template-startCount +description: test <> with templates and startCount +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + + + +<> + +<> + +<> + + + +<> + +<> + +<> + ++ +title: ExpectedResult + +

New Tiddler 0xx

New Tiddler 4xx

New Tiddler 11xx

emptyCount 00xx

invalid start 0xx

invalid count 0xx

\ No newline at end of file diff --git a/editions/test/tiddlers/tests/data/macros/unusedtitle/template.tid b/editions/test/tiddlers/tests/data/macros/unusedtitle/template.tid index 66bd0f763..183ad42da 100644 --- a/editions/test/tiddlers/tests/data/macros/unusedtitle/template.tid +++ b/editions/test/tiddlers/tests/data/macros/unusedtitle/template.tid @@ -25,4 +25,4 @@ title: Output + title: ExpectedResult -

New Tiddler

count-missing

00-new

00-base

00-New Tiddler

00-asdf

00 asdf

\ No newline at end of file +

New Tiddler

count-missing0

00-new

00-base

00-New Tiddler

00-asdf

00 asdf

\ No newline at end of file diff --git a/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid b/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid index e0cc00433..b39bb5eb1 100644 --- a/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid +++ b/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid @@ -1,6 +1,6 @@ caption: unusedtitle created: 20210104143546885 -modified: 20210427184035684 +modified: 20240119224103283 tags: Macros [[Core Macros]] title: unusedtitle Macro type: text/vnd.tiddlywiki @@ -18,7 +18,10 @@ It uses the same method as the create new tiddler button, a number is appended t : <<.from-version "5.2.0">> An ''optional'' string specifying the separator between baseName and the unique number. eg: `separator:"-"`. Defaults to a space: `" "`. If you need an empty separator use the ''template''! ; template -: <<.from-version "5.2.0">> A ''optional'' template string can be used to allow you maximum flexibility. If the template string is used, there will always be a counter value. +: <<.from-version "5.2.0">> An ''optional'' template string can be used to allow you maximum flexibility. If the template string is used, there will always be a counter value. + +; startCount +: <<.from-version "5.3.6">> An ''optional'' parameter, that sets the initial value for the new tiddler counter. !! Template String diff --git a/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid b/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid index 22ece3997..3853299bf 100644 --- a/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid +++ b/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid @@ -1,5 +1,5 @@ created: 20210227212730299 -modified: 20211118025545823 +modified: 20240119151636562 tags: [[Macro Examples]] title: unusedtitle Macro (Examples 1) type: text/vnd.tiddlywiki @@ -16,6 +16,10 @@ type: text/vnd.tiddlywiki <$action-sendmessage $message="tm-new-tiddler" title=<> /> \end +\define testStartCount() +<$action-createtiddler $basetitle=<>/> +\end + ``` <> ``` @@ -43,6 +47,16 @@ New Tiddler Create Tiddler + +``` +<> +``` + +<$button actions=<>> +<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> +Create Tiddler + + ---
diff --git a/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid b/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid index a86f0d16e..fc9ed29ed 100644 --- a/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid +++ b/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid @@ -1,5 +1,5 @@ created: 20210104143940715 -modified: 20210228141241657 +modified: 20240119150720917 tags: [[unusedtitle Macro]] [[Macro Examples]] title: unusedtitle Macro (Examples) type: text/vnd.tiddlywiki @@ -9,7 +9,8 @@ type: text/vnd.tiddlywiki <$macrocall $name=".example" n="2" eg="""<>"""/> <$macrocall $name=".example" n="3" eg="""<>"""/> <$macrocall $name=".example" n="4" eg="""<>"""/> -<$macrocall $name=".example" n="5" eg="""<>"""/> +<$macrocall $name=".example" n="5" eg="""<>"""/> +<$macrocall $name=".example" n="6" eg="""<>"""/> ---