Skip to content

Commit

Permalink
Add ability to pass smartblock cmd to REPEAT to iterate over (#104)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdroidian authored Nov 13, 2023
1 parent 79d7127 commit c66e5be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
23 changes: 17 additions & 6 deletions docs/050-command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Serendipity commands are commands that help resolve to a random block from your
**Parameters**:

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

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

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

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

**Example**:

- `<%RESOLVEBLOCKREF:**abcdefghi**%>`
- `<%RESOLVEBLOCKREF:**`((abcdefghi))`**%>`
- `<%RESOLVEBLOCKREF:abcdefghi%>`
- `<%RESOLVEBLOCKREF:((abcdefghi))%>`

## SEARCH

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

**Example**:

- ``<%IFMATCH:foo,true%>`
- `<%IFMATCH:foo,true%>`
- - 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

## IFDATEOFYEAR
Expand Down Expand Up @@ -1012,14 +1012,25 @@ If it isn't, the block is not inserted

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

If the first argument is another command (eg `<%CHILDREN%>`), then `Count of repeats` will be set to the number of results from that command.

This also passes SmartBlock variables:

- `ITERATION`: returns the current loop (eg: `1`, `2`, etc).
- `ITERATIONVALUE`: returns the item being iterated over from the first argument.

**Parameters**:

1. Count of repeats
1. Count of repeats (or a SmartBlock command that returns items to iterate over)
2. Content to repeat

**Example**:

- `<%REPEAT:5,hello%>`
- `<%REPEAT:<%CHILDREN:((someUid))%>,<%SMARTBLOCK:runMe%>%>`
- runMe #SmartBlock
- `<%GET:ITERATION%>`
- `<%GET:ITERATIONVALUE%>`

# **Cursor Commands**

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smartblocks",
"version": "1.6.13",
"version": "1.7.0",
"description": "Create custom and programmable templates from within Roam!",
"main": "./build/main.js",
"scripts": {
Expand Down
38 changes: 20 additions & 18 deletions src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1731,24 +1731,26 @@ export const COMMANDS: {
{
text: "REPEAT",
delayArgs: true,
help: "Repeats the current block a number of specified times\n\n1. Number of times for repeat",
handler: (repeatArg = "1", content = "") =>
proccessBlockText(repeatArg)
.then(([{ text }]) =>
Array(Number(text) || 1)
.fill(content)
.map((s) => () => proccessBlockText(s))
.reduce(
(prev, cur) =>
prev.then((p) =>
cur().then((c) => {
return [...p, c];
})
),
Promise.resolve([] as InputTextNode[][])
)
)
.then((s) => s.flatMap((c) => c)),
help: "Repeats the current block a specified number of times\n\n1. Number of times for repeat",
handler: async (repeatArg = "1", content = "") => {
const argResult = await proccessBlockText(repeatArg);
const argResultNumber = Number(argResult[0].text);
const isNumberPassed = argResult.length === 1 && !isNaN(argResultNumber);
const repeatCount = isNumberPassed
? argResultNumber
: argResult.length || 1;

const results = [];
for (let i = 0; i < repeatCount; i++) {
smartBlocksContext.variables["ITERATION"] = (i + 1).toString();
smartBlocksContext.variables["ITERATIONVALUE"] = isNumberPassed
? ""
: argResult[i].text;
const result = await proccessBlockText(content);
results.push(result);
}
return results.flat();
},
},
{
text: "DATEBASIS",
Expand Down

0 comments on commit c66e5be

Please sign in to comment.