Skip to content

Commit c66e5be

Browse files
authored
Add ability to pass smartblock cmd to REPEAT to iterate over (#104)
* refactor * adjust delayArgs to accept array * add ITERATION and ITERATIONVALUE to REPEAT cmd * 1.7.0 * set count to - to iterate over values * PR Feedback * revert delayArgs changes * handle array passed to repeatArg * update docs * revert delayArgs type definition
1 parent 79d7127 commit c66e5be

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

docs/050-command-reference.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Serendipity commands are commands that help resolve to a random block from your
129129
**Parameters**:
130130

131131
1. Page to pull a child from. Could be either:
132-
- Option 1: **Page name **or **tag name** (brackets `[[]]` or hashtag `#` are optional)
132+
- Option 1: **Page name** or **tag name** (brackets `[[]]` or hashtag `#` are optional)
133133
- Option 2: Parent block UID
134134
2. Levels within the page to include. Specifying 0 includes all blocks
135135
3. Format to output the block in. See our [Formatting](050-command-reference.md#formatting) section for more info.
@@ -155,7 +155,7 @@ Serendipity commands are commands that help resolve to a random block from your
155155

156156
**Parameters**: One parameter that could be either:
157157

158-
- Option 1: **Page name **or **tag name** (brackets `[[]]` or hashtag `#` are optional)
158+
- Option 1: **Page name** or **tag name** (brackets `[[]]` or hashtag `#` are optional)
159159
- Option 2: Parent block UID
160160

161161
**Example**:
@@ -511,8 +511,8 @@ Special note: if you want a comma to be a part of the output, put a \ in front o
511511

512512
**Example**:
513513

514-
- `<%RESOLVEBLOCKREF:**abcdefghi**%>`
515-
- `<%RESOLVEBLOCKREF:**`((abcdefghi))`**%>`
514+
- `<%RESOLVEBLOCKREF:abcdefghi%>`
515+
- `<%RESOLVEBLOCKREF:((abcdefghi))%>`
516516

517517
## SEARCH
518518

@@ -735,7 +735,7 @@ If no value is specified, it's the same as checking if the block is empty
735735

736736
**Example**:
737737

738-
- ``<%IFMATCH:foo,true%>`
738+
- `<%IFMATCH:foo,true%>`
739739
- - checks if variable `foo` has value `true`. If no variable `foo` exists, it then checks to see if `foo` is equal in value to `true`, which would keep the block
740740

741741
## IFDATEOFYEAR
@@ -1012,14 +1012,25 @@ If it isn't, the block is not inserted
10121012

10131013
**Purpose**: Repeats the second argument a specified amount of times.
10141014

1015+
If the first argument is another command (eg `<%CHILDREN%>`), then `Count of repeats` will be set to the number of results from that command.
1016+
1017+
This also passes SmartBlock variables:
1018+
1019+
- `ITERATION`: returns the current loop (eg: `1`, `2`, etc).
1020+
- `ITERATIONVALUE`: returns the item being iterated over from the first argument.
1021+
10151022
**Parameters**:
10161023

1017-
1. Count of repeats
1024+
1. Count of repeats (or a SmartBlock command that returns items to iterate over)
10181025
2. Content to repeat
10191026

10201027
**Example**:
10211028

10221029
- `<%REPEAT:5,hello%>`
1030+
- `<%REPEAT:<%CHILDREN:((someUid))%>,<%SMARTBLOCK:runMe%>%>`
1031+
- runMe #SmartBlock
1032+
- `<%GET:ITERATION%>`
1033+
- `<%GET:ITERATIONVALUE%>`
10231034

10241035
# **Cursor Commands**
10251036

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smartblocks",
3-
"version": "1.6.13",
3+
"version": "1.7.0",
44
"description": "Create custom and programmable templates from within Roam!",
55
"main": "./build/main.js",
66
"scripts": {

src/utils/core.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -1731,24 +1731,26 @@ export const COMMANDS: {
17311731
{
17321732
text: "REPEAT",
17331733
delayArgs: true,
1734-
help: "Repeats the current block a number of specified times\n\n1. Number of times for repeat",
1735-
handler: (repeatArg = "1", content = "") =>
1736-
proccessBlockText(repeatArg)
1737-
.then(([{ text }]) =>
1738-
Array(Number(text) || 1)
1739-
.fill(content)
1740-
.map((s) => () => proccessBlockText(s))
1741-
.reduce(
1742-
(prev, cur) =>
1743-
prev.then((p) =>
1744-
cur().then((c) => {
1745-
return [...p, c];
1746-
})
1747-
),
1748-
Promise.resolve([] as InputTextNode[][])
1749-
)
1750-
)
1751-
.then((s) => s.flatMap((c) => c)),
1734+
help: "Repeats the current block a specified number of times\n\n1. Number of times for repeat",
1735+
handler: async (repeatArg = "1", content = "") => {
1736+
const argResult = await proccessBlockText(repeatArg);
1737+
const argResultNumber = Number(argResult[0].text);
1738+
const isNumberPassed = argResult.length === 1 && !isNaN(argResultNumber);
1739+
const repeatCount = isNumberPassed
1740+
? argResultNumber
1741+
: argResult.length || 1;
1742+
1743+
const results = [];
1744+
for (let i = 0; i < repeatCount; i++) {
1745+
smartBlocksContext.variables["ITERATION"] = (i + 1).toString();
1746+
smartBlocksContext.variables["ITERATIONVALUE"] = isNumberPassed
1747+
? ""
1748+
: argResult[i].text;
1749+
const result = await proccessBlockText(content);
1750+
results.push(result);
1751+
}
1752+
return results.flat();
1753+
},
17521754
},
17531755
{
17541756
text: "DATEBASIS",

0 commit comments

Comments
 (0)