1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-13 05:19:58 +00:00

Add startCount to unusedtitle macro, new tests, new docs (#7941)

* add startCount to unusedtitle macro, new tests, new docs

* rename test files

* update .from-version in docs
This commit is contained in:
Mario Pietsch 2024-09-10 11:49:41 +02:00 committed by GitHub
parent b12625c9f6
commit 8a8dcf8255
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 81 additions and 18 deletions

View File

@ -16,20 +16,22 @@ exports.name = "unusedtitle";
exports.params = [ exports.params = [
{name: "baseName"}, {name: "baseName"},
{name: "separator"}, {name: "separator"},
{name: "template"} {name: "template"},
{name: "startCount"}
]; ];
/* /*
Run the macro Run the macro
*/ */
exports.run = function(baseName,separator,template) { exports.run = function(baseName,separator,template,startCount) {
separator = separator || " "; separator = separator || " ";
startCount = startCount || 0;
if(!baseName) { if(!baseName) {
baseName = $tw.language.getString("DefaultNewTiddlerTitle"); baseName = $tw.language.getString("DefaultNewTiddlerTitle");
} }
// $tw.wiki.generateNewTitle = function(baseTitle,options) // $tw.wiki.generateNewTitle = function(baseTitle,options)
// options.prefix must be a string! // options.prefix must be a string!
return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template}); return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template, "startCount": startCount});
}; };
})(); })();

View File

@ -330,16 +330,18 @@ exports.formatTitleString = function(template,options) {
}] }]
]; ];
while(t.length){ while(t.length){
var matchString = ""; var matchString = "",
found = false;
$tw.utils.each(matches, function(m) { $tw.utils.each(matches, function(m) {
var match = m[0].exec(t); var match = m[0].exec(t);
if(match) { if(match) {
found = true;
matchString = m[1].call(null,match); matchString = m[1].call(null,match);
t = t.substr(match[0].length); t = t.substr(match[0].length);
return false; return false;
} }
}); });
if(matchString) { if(found) {
result += matchString; result += matchString;
} else { } else {
result += t.charAt(0); result += t.charAt(0);

View File

@ -194,18 +194,24 @@ options.prefix must be a string
*/ */
exports.generateNewTitle = function(baseTitle,options) { exports.generateNewTitle = function(baseTitle,options) {
options = options || {}; options = options || {};
var c = 0, var title = baseTitle,
title = baseTitle, template = options.template || "",
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 : " "; prefix = (typeof(options.prefix) === "string") ? options.prefix : " ";
if (template) { if (template) {
// "count" is important to avoid an endless loop in while(...)!! // "count" is important to avoid an endless loop in while(...)!!
template = (/\$count:?(\d+)?\$/i.test(template)) ? template : template + "$count$"; 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)) { 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 { } else {
if (c > 0) {
title = baseTitle + prefix + c;
}
while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) {
title = baseTitle + prefix + (++c); title = baseTitle + prefix + (++c);
} }

View File

@ -14,7 +14,15 @@ title: Output
<<unusedtitle baseName:"About" separator:"-">> <<unusedtitle baseName:"About" separator:"-">>
<!-- v5.3.6 startCount -->
<<unusedtitle startCount:"3">>
<<unusedtitle startCount:"-1" baseName:"invalid start">>
<<unusedtitle startCount:"aaa" baseName:"invalid count">>
+ +
title: ExpectedResult title: ExpectedResult
<p>New Tiddler</p><p>New Tiddler</p><p>anotherBase</p><p>About</p> <p>New Tiddler</p><p>New Tiddler</p><p>anotherBase</p><p>About</p><p>New Tiddler 3</p><p>invalid start</p><p>invalid count</p>

View File

@ -0,0 +1,27 @@
title: Macros/unusedtitle/template-startCount
description: test <<unusedtitle>> with templates and startCount
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
<!-- test template basics -->
<<unusedtitle template:"$basename$$separator$$count$xx" >>
<<unusedtitle template:"$basename$$separator$$count$xx" startCount:"4" >>
<<unusedtitle template:"$basename$$separator$$count:1$xx" startCount:"11">>
<!-- test parameter edgecases -->
<<unusedtitle template:"$basename$$separator$$count:2$xx" startCount:"" baseName:"emptyCount">>
<<unusedtitle template:"$basename$$separator$$count$xx" startCount:"-1" separator:"" baseName:"invalid start" >>
<<unusedtitle template:"$basename$$separator$$count$xx" startCount:"bbb" separator:"" baseName:"invalid count" >>
+
title: ExpectedResult
<p>New Tiddler 0xx</p><p>New Tiddler 4xx</p><p>New Tiddler 11xx</p><p>emptyCount 00xx</p><p>invalid start 0xx</p><p>invalid count 0xx</p>

View File

@ -25,4 +25,4 @@ title: Output
+ +
title: ExpectedResult title: ExpectedResult
<p>New Tiddler</p><p>count-missing</p><p>00-new</p><p>00-base</p><p>00-New Tiddler</p><p>00-asdf</p><p>00 asdf</p> <p>New Tiddler</p><p>count-missing0</p><p>00-new</p><p>00-base</p><p>00-New Tiddler</p><p>00-asdf</p><p>00 asdf</p>

View File

@ -1,6 +1,6 @@
caption: unusedtitle caption: unusedtitle
created: 20210104143546885 created: 20210104143546885
modified: 20210427184035684 modified: 20240119224103283
tags: Macros [[Core Macros]] tags: Macros [[Core Macros]]
title: unusedtitle Macro title: unusedtitle Macro
type: text/vnd.tiddlywiki 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''! : <<.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 ; 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 !! Template String

View File

@ -1,5 +1,5 @@
created: 20210227212730299 created: 20210227212730299
modified: 20211118025545823 modified: 20240119151636562
tags: [[Macro Examples]] tags: [[Macro Examples]]
title: unusedtitle Macro (Examples 1) title: unusedtitle Macro (Examples 1)
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -16,6 +16,10 @@ type: text/vnd.tiddlywiki
<$action-sendmessage $message="tm-new-tiddler" title=<<unusedtitle baseName:"new" template:"$count:2$-$basename$">> /> <$action-sendmessage $message="tm-new-tiddler" title=<<unusedtitle baseName:"new" template:"$count:2$-$basename$">> />
\end \end
\define testStartCount()
<$action-createtiddler $basetitle=<<unusedtitle startCount:"10" baseName:"new" separator:"-" template:"$count:2$$separator$$basename$">>/>
\end
``` ```
<<unusedtitle template:"$count:2$-new">> <<unusedtitle template:"$count:2$-new">>
``` ```
@ -43,6 +47,16 @@ New Tiddler
Create Tiddler Create Tiddler
</$button> </$button>
```
<<unusedtitle startCount:"10" baseName:"new" separator:"-" template:"$count:2$$separator$$basename$">>
```
<$button actions=<<testStartCount>>>
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
Create Tiddler
</$button>
--- ---
<details> <details>

View File

@ -1,5 +1,5 @@
created: 20210104143940715 created: 20210104143940715
modified: 20210228141241657 modified: 20240119150720917
tags: [[unusedtitle Macro]] [[Macro Examples]] tags: [[unusedtitle Macro]] [[Macro Examples]]
title: unusedtitle Macro (Examples) title: unusedtitle Macro (Examples)
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -9,7 +9,8 @@ type: text/vnd.tiddlywiki
<$macrocall $name=".example" n="2" eg="""<<unusedtitle separator:"-">>"""/> <$macrocall $name=".example" n="2" eg="""<<unusedtitle separator:"-">>"""/>
<$macrocall $name=".example" n="3" eg="""<<unusedtitle baseName:"anotherBase">>"""/> <$macrocall $name=".example" n="3" eg="""<<unusedtitle baseName:"anotherBase">>"""/>
<$macrocall $name=".example" n="4" eg="""<<unusedtitle baseName:"About" separator:"-">>"""/> <$macrocall $name=".example" n="4" eg="""<<unusedtitle baseName:"About" separator:"-">>"""/>
<$macrocall $name=".example" n="5" eg="""<<unusedtitle template:"$count:2$-test">>"""/> <$macrocall $name=".example" n="5" eg="""<<unusedtitle baseName:"Count" startCount:"3">>"""/>
<$macrocall $name=".example" n="6" eg="""<<unusedtitle template:"$count:2$-test">>"""/>
--- ---