Skip to content

Commit 4983f74

Browse files
authored
Add order to SMARTBLOCK (#131)
* Handle order as third SMARTBLOCK parameter * Enhance SMARTBLOCK command documentation and handler to support order parameter in the format `order=value`. Update examples to reflect new usage and improve argument parsing for page names and order specification. * Refactor order parameter handling in SMARTBLOCK command to improve argument parsing. Use regex for order extraction and validate numeric values, ensuring robust title extraction from page names. * 1.10.0
1 parent 7119529 commit 4983f74

4 files changed

Lines changed: 76 additions & 23 deletions

File tree

docs/050-command-reference.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,11 +1098,16 @@ To set the default text of `embed`, set a variable named the same as the name of
10981098

10991099
1. The workflow name
11001100
2. An optional page name to execute the workflow on another page. The command will return a block reference to the other page if this parameter is set.
1101+
3. When the second parameter is set, you may optionally pass an additional parameter in the format `order=value` to specify the order of the new block on that page. The value can be a number (0 to insert at the first position) or `last` to append to the end of the page.
11011102

11021103
**Example:**
11031104

1104-
- `<%SMARTBLOCK:Daily%>`
1105-
- `<%SMARTBLOCK:Daily,<%DATE:Tomorrow%>%>`
1105+
- `<%SMARTBLOCK:MyWorkflow%>`
1106+
- `<%SMARTBLOCK:MyWorkflow,<%DATE:Tomorrow%>%>`
1107+
- `<%SMARTBLOCK:MyWorkflow,V_MTxadSW%>`
1108+
- `<%SMARTBLOCK:MyWorkflow,((V_MTxadSW)),order=0%>`
1109+
- `<%SMARTBLOCK:MyWorkflow,Some Page,order=last%>`
1110+
- `<%SMARTBLOCK:MyWorkflow,,order=3%>`
11061111

11071112
## REPEAT
11081113

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/utils/core.ts

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,28 +1766,70 @@ export const COMMANDS: {
17661766
},
17671767
{
17681768
text: "SMARTBLOCK",
1769-
help: "Runs another SmartBlock\n\n1. SmartBlock name\n\n2. Optional page name or ref to execute the workflow remotely",
1769+
help: "Runs another SmartBlock\n\n1. SmartBlock name\n\n2. Optional page name or ref to execute the workflow remotely\n\n3. Optional order for the new block when running remotely in the format order=value (use a number or 'last' to append to the end)",
17701770
handler: (inputName = "", ...pageNameOrUid) => {
17711771
const srcUid = getCleanCustomWorkflows().find(
17721772
({ name }) => name === inputName.trim()
17731773
)?.uid;
17741774
if (srcUid) {
17751775
if (pageNameOrUid.length) {
1776-
const title = extractTag(pageNameOrUid.join(","));
1777-
const targetUid = getUidFromText(title);
1778-
return (
1779-
targetUid ? Promise.resolve(targetUid) : createPage({ title })
1780-
).then((targetUid) => {
1781-
const parentContext = { ...smartBlocksContext };
1782-
return sbBomb({
1783-
srcUid,
1784-
target: { uid: targetUid, isParent: true },
1785-
variables: smartBlocksContext.variables,
1786-
}).then((uid) => {
1787-
resetContext(parentContext);
1788-
return uid ? `((${uid}))` : "";
1776+
// Parse arguments to handle page name with potential order parameter
1777+
let title: string;
1778+
let order: number | "last" | undefined;
1779+
1780+
if (pageNameOrUid.length === 1) {
1781+
// Single argument - assume it is a page name
1782+
const arg = pageNameOrUid[0];
1783+
title = extractTag(arg);
1784+
} else {
1785+
// Multiple arguments - last one might be order=value
1786+
const lastArg = pageNameOrUid[pageNameOrUid.length - 1];
1787+
const orderMatch = /^order=(.+)$/i.exec(lastArg);
1788+
if (orderMatch) {
1789+
const orderValue = orderMatch[1];
1790+
title = extractTag(pageNameOrUid.slice(0, -1).join(","));
1791+
if (orderValue.toLowerCase() === "last") {
1792+
order = "last";
1793+
} else {
1794+
const numOrder = Number(orderValue);
1795+
if (
1796+
!isNaN(numOrder) &&
1797+
Number.isInteger(numOrder) &&
1798+
numOrder >= 0
1799+
) {
1800+
order = numOrder;
1801+
} else {
1802+
// Invalid order value, treat as part of page name
1803+
title = extractTag(pageNameOrUid.join(","));
1804+
}
1805+
}
1806+
} else {
1807+
// All arguments are page name
1808+
title = extractTag(pageNameOrUid.join(" "));
1809+
}
1810+
}
1811+
1812+
if (title.trim() !== "") {
1813+
const targetUid = getUidFromText(title);
1814+
1815+
return (
1816+
targetUid ? Promise.resolve(targetUid) : createPage({ title })
1817+
).then((targetUid) => {
1818+
const parentContext = { ...smartBlocksContext };
1819+
return sbBomb({
1820+
srcUid,
1821+
target: {
1822+
uid: targetUid,
1823+
isParent: true,
1824+
...(order !== undefined ? { order } : {}),
1825+
},
1826+
variables: smartBlocksContext.variables,
1827+
}).then((uid) => {
1828+
resetContext(parentContext);
1829+
return uid ? `((${uid}))` : "";
1830+
});
17891831
});
1790-
});
1832+
}
17911833
}
17921834
const nodes = getFullTreeByParentUid(srcUid).children;
17931835
const parentContext = { ...smartBlocksContext };
@@ -2641,7 +2683,7 @@ const count = (t: InputTextNode[] = []): number =>
26412683

26422684
export const sbBomb = async ({
26432685
srcUid,
2644-
target: { uid, start = 0, end = start, isParent = false, windowId },
2686+
target: { uid, start = 0, end = start, isParent = false, order, windowId },
26452687
variables = {},
26462688
mutableCursor,
26472689
triggerUid = uid,
@@ -2653,6 +2695,7 @@ export const sbBomb = async ({
26532695
start?: number;
26542696
end?: number;
26552697
isParent?: boolean;
2698+
order?: number | "last";
26562699
windowId?: string;
26572700
};
26582701
variables?: Record<string, string>;
@@ -2688,7 +2731,9 @@ export const sbBomb = async ({
26882731
const [firstChild, ...next] = tree;
26892732
if (firstChild) {
26902733
const startingOrder = isParent
2691-
? getChildrenLengthByPageUid(uid)
2734+
? typeof order === "number" || order === "last"
2735+
? order
2736+
: getChildrenLengthByPageUid(uid)
26922737
: getOrderByBlockUid(uid);
26932738
const parentUid = isParent ? uid : getParentUidByBlockUid(uid);
26942739
const outputUid = await (isParent
@@ -2730,7 +2775,10 @@ export const sbBomb = async ({
27302775
next.map((node, i) =>
27312776
createBlock({
27322777
parentUid,
2733-
order: startingOrder + 1 + i,
2778+
order:
2779+
startingOrder === "last"
2780+
? startingOrder
2781+
: startingOrder + 1 + i,
27342782
node,
27352783
})
27362784
)

0 commit comments

Comments
 (0)