RoamJS / smartblocks

MIT License
23 stars 6 forks source link

Parsing and docs error for command: <%QUERYBUILDER%> #120

Open MarketHubb opened 1 month ago

MarketHubb commented 1 month ago

According to the docs, the second argument for the <%QUERYBUILDER%> command is for the output format, which is not always the case.

Overview of issue

If the second argument is one of the predefined output formats (i.e., {text} or {uid}), the command will return the first selection (i.e., column) of the Query in that format.

However, if you pass the name of any other selection as the second argument - or the first selection if it's been renamed - the command will return the {text} of that selection.

Secondary issue

When purposefully passing a selection - and not an output format - as the second argument, the command does not parse the <%GET%> command.

Returns the uid of the first selection, regardless of it's name

<%QUERYBUILDER:queryId,{uid}%>

Returns undefined even when columnName is set to a selection found on the query

<%QUERYBUILDER:queryId,{<%GET:columnName%>}%>

Returns the text of the "Client" selection, regardless of it's position

<%QUERYBUILDER:queryId,{Client}%>

Proposed solution

Update the command to take 4 arguments:

  1. Block ref or alias
  2. Format of output
  3. Selection (default to Text)
  4. Number of results

Ensure the arguments are parsed before the command is run so <%GET%> can be used for any of the arguments

mdroidian commented 1 month ago

Thank you for the report @MarketHubb!

First Issue

In terms of the first issue, I believe the output format is intended to support a wide range of use cases and not limited to just the predefined output formats.

For example, the following would be valid (where {description} is the title of the second column (selection)):

<%QUERYBUILDER:myQuery,The Result is {text} - {description}%>

Secondary Issue

Similarly, regarding the Secondary Issue, it seems that SmartBlock commands are intended to be accepted in the output format so that they may be applied to the output itself. Which is why the SmartBlock commands in the arguments are explicitly delayed. For example:

<%QUERYBUILDER:myQueryBlock,Random Block from {text}: <%RANDOMBLOCKFROM:{uid}%>%>

<%GET%>

There is, however, an undocumented feature that could solve the <%GET%> issue for you. The second argument actually accepts a block reference. If the second argument is just a block reference, it will use that block as the output format. And in that block you may use SmartBlock Commands.

eg: <%QUERYBUILDER:myQuery,((ITe51brrn))%>

- Title:: <%GET:text%>
  - Description:: <%GET:description%>

To handle your use case you would wrap the variable set to the column name in another <%GET%>, eg:

- <%GET:<%GET:columnNameVar%>%>

More info here:

https://www.loom.com/share/d0492dfdbe5f4e89a4b996fe85c7317b

Selection uid

While not explicitly mentioned, in case you would like to return a selection uid instead of text, you can actually append -uid to the column name, eg: {columnName-uid} or <%GET:columName-uid%> if the second argument is a block reference. (and <%GET:<%GET:columnNameVar%>-uid%> if you are passing the column name as a variable.

Additional Remarks

I know there is a lot going on here and it can be quite convoluted. 😅 If anything isn't clear or if you have any additional questions, don't hesitate to let me know. Otherwise feel free to close this issue.

I'll see to it that the Query Builder docs get updated to reflect some of the more standard and undocumented features that I've laid out here.