Skip to content

Commit 4a9ed9d

Browse files
authored
add regex support to 'with text', handle case insensitive regex (#241)
* add regex support to 'with text', handle case insensitive regex * 1.28.0
1 parent 7a40cd2 commit 4a9ed9d

File tree

3 files changed

+85
-26
lines changed

3 files changed

+85
-26
lines changed

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": "query-builder",
3-
"version": "1.27.1",
3+
"version": "1.28.0",
44
"description": "Introduces new user interfaces for building queries in Roam",
55
"main": "./build/main.js",
66
"author": {

src/utils/conditionToDatalog.ts

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ type ConditionToDatalog = (condition: Condition) => DatalogClause[];
2121

2222
const INPUT_REGEX = /^:in /;
2323

24+
const isRegex = (str: string) => /^\/.+\/(i)?$/.test(str);
25+
const regexRePatternValue = (str: string) => {
26+
const isCaseInsensitive = str.endsWith("/i");
27+
return isCaseInsensitive
28+
? `"(?i)${str.slice(1, -2).replace(/\\/g, "\\\\")}"`
29+
: `"${str.slice(1, -1).replace(/\\/g, "\\\\")}"`;
30+
};
2431
const getTitleDatalog = ({
2532
source,
2633
target,
@@ -100,7 +107,8 @@ const getTitleDatalog = ({
100107
},
101108
];
102109
}
103-
if (target.startsWith("/") && target.endsWith("/")) {
110+
if (isRegex(target)) {
111+
const rePattern = regexRePatternValue(target);
104112
return [
105113
{
106114
type: "data-pattern",
@@ -116,7 +124,7 @@ const getTitleDatalog = ({
116124
arguments: [
117125
{
118126
type: "constant",
119-
value: `"${target.slice(1, -1).replace(/\\/g, "\\\\")}"`,
127+
value: rePattern,
120128
},
121129
],
122130
binding: {
@@ -388,37 +396,88 @@ const translator: Record<string, Translator> = {
388396
isVariable: true,
389397
},
390398
"with text": {
391-
callback: ({ source, target }) => [
392-
{
393-
type: "or-clause",
394-
clauses: [
399+
callback: ({ source, target }) => {
400+
if (isRegex(target)) {
401+
const rePattern = regexRePatternValue(target);
402+
return [
395403
{
396-
type: "data-pattern",
404+
type: "or-clause",
405+
clauses: [
406+
{
407+
type: "data-pattern",
408+
arguments: [
409+
{ type: "variable", value: source },
410+
{ type: "constant", value: ":block/string" },
411+
{ type: "variable", value: `${source}-String` },
412+
],
413+
},
414+
{
415+
type: "data-pattern",
416+
arguments: [
417+
{ type: "variable", value: source },
418+
{ type: "constant", value: ":node/title" },
419+
{ type: "variable", value: `${source}-String` },
420+
],
421+
},
422+
],
423+
},
424+
{
425+
type: "fn-expr",
426+
fn: "re-pattern",
397427
arguments: [
398-
{ type: "variable", value: source },
399-
{ type: "constant", value: ":block/string" },
428+
{
429+
type: "constant",
430+
value: rePattern,
431+
},
432+
],
433+
binding: {
434+
type: "bind-scalar",
435+
variable: { type: "variable", value: `${target}-regex` },
436+
},
437+
},
438+
{
439+
type: "pred-expr",
440+
pred: "re-find",
441+
arguments: [
442+
{ type: "variable", value: `${target}-regex` },
400443
{ type: "variable", value: `${source}-String` },
401444
],
402445
},
446+
];
447+
} else {
448+
return [
449+
{
450+
type: "or-clause",
451+
clauses: [
452+
{
453+
type: "data-pattern",
454+
arguments: [
455+
{ type: "variable", value: source },
456+
{ type: "constant", value: ":block/string" },
457+
{ type: "variable", value: `${source}-String` },
458+
],
459+
},
460+
{
461+
type: "data-pattern",
462+
arguments: [
463+
{ type: "variable", value: source },
464+
{ type: "constant", value: ":node/title" },
465+
{ type: "variable", value: `${source}-String` },
466+
],
467+
},
468+
],
469+
},
403470
{
404-
type: "data-pattern",
471+
type: "pred-expr",
472+
pred: "clojure.string/includes?",
405473
arguments: [
406-
{ type: "variable", value: source },
407-
{ type: "constant", value: ":node/title" },
408474
{ type: "variable", value: `${source}-String` },
475+
{ type: "constant", value: `"${normalizePageTitle(target)}"` },
409476
],
410477
},
411-
],
412-
},
413-
{
414-
type: "pred-expr",
415-
pred: "clojure.string/includes?",
416-
arguments: [
417-
{ type: "variable", value: `${source}-String` },
418-
{ type: "constant", value: `"${normalizePageTitle(target)}"` },
419-
],
420-
},
421-
],
478+
];
479+
}
480+
},
422481
placeholder: "Enter any text",
423482
},
424483
"created by": {

0 commit comments

Comments
 (0)