Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ The `label`, which gets specified after **AS**, denotes the name of the column t
| `node:{node}:{data}` | Specify one of the metadata `{data}` options or an `{attribute}` to return for an intermediary node. | [Link](examples.md#data) |
| `node:{node}:/regular_expression/` | Returns a match according to a regular expression between `/`'s. | [Link](examples.md#regular-expression) |
| `node` | Use the label to edit the column header of the first column | [Link](examples.md#first-column-header) |
| `text({node})` | Returns plain text for a node by stripping page refs, tags, and button markup. | N/A |
| `add({label1}, {label2})` | Add the values of two columns. Supports adding values to dates. | [Link](examples.md#add-or-subtract) |
| `subtract({label1}, {label2})` | Subtract the values betweenn two columns. Supports adding values to dates. | [Link](examples.md#add-or-subtract) |

Expand Down
25 changes: 25 additions & 0 deletions src/utils/predefinedSelections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IconNames } from "@blueprintjs/icons";
import parseQuery from "./parseQuery";
import toCellValue from "./toCellValue";
import createBlock from "roamjs-components/writes/createBlock";
import toTextOnlyValue from "./toTextOnlyValue";

const ALIAS_TEST = /^node$/i;
const REGEX_TEST = /\/([^}]*)\//;
Expand All @@ -25,6 +26,7 @@ const CREATE_BY_TEST = /^\s*(author|create(d)?\s*by)\s*$/i;
const EDIT_BY_TEST = /^\s*(last\s*)?edit(ed)?\s*by\s*$/i;
const SUBTRACT_TEST = /^subtract\(([^,)]+),([^,)]+)\)$/i;
const ADD_TEST = /^add\(([^,)]+),([^,)]+)\)$/i;
const TEXT_ONLY_TEST = /^text\(\s*([^)]+?)\s*\)$/i;
const NODE_TEST = /^node:(\s*[^:]+\s*)(:.*)?$/i;
const ACTION_TEST = /^action:\s*([^:]+)\s*(?::(.*))?$/i;
const DATE_FORMAT_TEST = /^date-format\(([^,)]+),([^,)]+)\)$/i;
Expand Down Expand Up @@ -216,6 +218,9 @@ const EDIT_DATE_SUGGESTIONS: SelectionSuggestion[] = [
];
const CREATE_BY_SUGGESTIONS: SelectionSuggestion[] = [{ text: "created by" }];
const EDIT_BY_SUGGESTIONS: SelectionSuggestion[] = [{ text: "edited by" }];
const TEXT_ONLY_SUGGESTIONS: SelectionSuggestion[] = [
{ text: "text({{node}})" },
];
// TOO SLOW
// const ATTR_SUGGESTIONS: SelectionSuggestion[] = (
// window.roamAlphaAPI.data.fast.q(
Expand Down Expand Up @@ -402,6 +407,26 @@ const predefinedSelections: PredefinedSelection[] = [
},
suggestions: [{ text: "node" }],
},
{
test: TEXT_ONLY_TEST,
pull: ({ match, where }) => {
const node = (match?.[1] || "").trim().replace(/^\?/, "");
return node && isVariableExposed(where, node)
? `(pull ?${node} [:block/string :node/title :block/uid])`
: "";
},
mapper: (r) => {
const uid = r?.[":block/uid"] || "";
return {
"": toTextOnlyValue({
value: r?.[":node/title"] || r?.[":block/string"] || "",
uid,
}),
"-uid": uid,
};
},
suggestions: TEXT_ONLY_SUGGESTIONS,
},
{
test: SUBTRACT_TEST,
pull: ({ returnNode }) => `(pull ?${returnNode} [:db/id])`,
Expand Down
28 changes: 28 additions & 0 deletions src/utils/toTextOnlyValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import toCellValue from "./toCellValue";

const BUTTON_MARKUP_REGEX = /\{\{([^{}]*)\}\}/g;
const BUTTON_PREFIX_REGEX = /^\s*(?:\[\[)?button(?:\]\])?\s*:/i;

const stripButtonMarkup = (value: string): string => {
let output = value;
let previous = "";
while (output !== previous) {
previous = output;
output = output.replace(BUTTON_MARKUP_REGEX, (_, inner: string) =>
inner.replace(BUTTON_PREFIX_REGEX, "").trim()
);
}
return output;
};

const toTextOnlyValue = ({
value,
uid,
defaultValue = "",
}: {
value: number | Date | string;
uid: string;
defaultValue?: string;
}) => stripButtonMarkup(toCellValue({ value, uid, defaultValue }));

export default toTextOnlyValue;