Skip to content

Commit

Permalink
Add startCount to unusedtitle macro, new tests, new docs (#7941)
Browse files Browse the repository at this point in the history
* add startCount to unusedtitle macro, new tests, new docs

* rename test files

* update .from-version in docs
  • Loading branch information
pmario committed Sep 10, 2024
1 parent b12625c commit 8a8dcf8
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 18 deletions.
10 changes: 6 additions & 4 deletions core/modules/macros/unusedtitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
};

})();
6 changes: 4 additions & 2 deletions core/modules/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 11 additions & 5 deletions core/modules/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ title: Output

<<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

<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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ title: Output
+
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>
7 changes: 5 additions & 2 deletions editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
caption: unusedtitle
created: 20210104143546885
modified: 20210427184035684
modified: 20240119224103283
tags: Macros [[Core Macros]]
title: unusedtitle Macro
type: text/vnd.tiddlywiki
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
created: 20210227212730299
modified: 20211118025545823
modified: 20240119151636562
tags: [[Macro Examples]]
title: unusedtitle Macro (Examples 1)
type: text/vnd.tiddlywiki
Expand All @@ -16,6 +16,10 @@ type: text/vnd.tiddlywiki
<$action-sendmessage $message="tm-new-tiddler" title=<<unusedtitle baseName:"new" template:"$count:2$-$basename$">> />
\end

\define testStartCount()
<$action-createtiddler $basetitle=<<unusedtitle startCount:"10" baseName:"new" separator:"-" template:"$count:2$$separator$$basename$">>/>
\end

```
<<unusedtitle template:"$count:2$-new">>
```
Expand Down Expand Up @@ -43,6 +47,16 @@ New Tiddler
Create Tiddler
</$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>
Expand Down
5 changes: 3 additions & 2 deletions editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
created: 20210104143940715
modified: 20210228141241657
modified: 20240119150720917
tags: [[unusedtitle Macro]] [[Macro Examples]]
title: unusedtitle Macro (Examples)
type: text/vnd.tiddlywiki
Expand All @@ -9,7 +9,8 @@ type: text/vnd.tiddlywiki
<$macrocall $name=".example" n="2" eg="""<<unusedtitle separator:"-">>"""/>
<$macrocall $name=".example" n="3" eg="""<<unusedtitle baseName:"anotherBase">>"""/>
<$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">>"""/>

---

Expand Down

0 comments on commit 8a8dcf8

Please sign in to comment.