@seed-hypermedia/client 0.0.90 → 0.0.92
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-link.mjs +3 -3
- package/dist/blocks-to-markdown.mjs +2 -2
- package/dist/capability.mjs +2 -2
- package/dist/{chunk-T7WF36LU.mjs → chunk-7RJYOV4D.mjs} +1 -1
- package/dist/{chunk-ZNV6NTUU.mjs → chunk-BRMAKS6Z.mjs} +1 -1
- package/dist/{chunk-XXUI3VKA.mjs → chunk-EB45YSE7.mjs} +1 -1
- package/dist/{chunk-Z72PGJKG.mjs → chunk-HEATDM6O.mjs} +1 -1
- package/dist/{chunk-W7AGZEC2.mjs → chunk-IB26JMU5.mjs} +1 -1
- package/dist/{chunk-4HP5N4I7.mjs → chunk-IJOMB2VD.mjs} +3 -3
- package/dist/{chunk-HNQDRHGQ.mjs → chunk-IO44LDKK.mjs} +2 -2
- package/dist/{chunk-Z2S7TL5U.mjs → chunk-J6LPZ3QP.mjs} +1 -1
- package/dist/{chunk-RRXP7SCF.mjs → chunk-J6N6AF3R.mjs} +1 -1
- package/dist/{chunk-UTZ42CJC.mjs → chunk-JOFKLKIC.mjs} +1 -1
- package/dist/chunk-JSNDXUPG.mjs +407 -0
- package/dist/{chunk-JODNNMBZ.mjs → chunk-M6GKXROC.mjs} +2 -2
- package/dist/{chunk-VNZHLLJE.mjs → chunk-MK35Y35R.mjs} +148 -2
- package/dist/{chunk-XB6E7EVC.mjs → chunk-QVRJGK77.mjs} +1 -1
- package/dist/chunk-RWKSRJA3.mjs +230 -0
- package/dist/chunk-VWSAFDOL.mjs +110 -0
- package/dist/{chunk-UFLGAJWJ.mjs → chunk-WY7T2TES.mjs} +3 -3
- package/dist/chunk-ZWKMAGDU.mjs +10 -0
- package/dist/client.mjs +2 -2
- package/dist/comment.mjs +2 -2
- package/dist/dag-json.d.ts +52 -0
- package/dist/dag-json.mjs +23 -0
- package/dist/document-state.mjs +2 -2
- package/dist/editorblock-to-hmblock.mjs +2 -2
- package/dist/explore-query.d.ts +112 -0
- package/dist/explore-query.mjs +455 -0
- package/dist/hm-resolver.mjs +2 -2
- package/dist/hm-types.d.ts +2953 -0
- package/dist/hm-types.mjs +33 -1
- package/dist/hmblock-to-editorblock.mjs +2 -2
- package/dist/index.mjs +27 -27
- package/dist/markdown-to-blocks.mjs +1 -1
- package/dist/pdf-to-blocks.mjs +3 -3
- package/dist/resource-read.mjs +4 -4
- package/dist/schema-engine.d.ts +127 -0
- package/dist/schema-engine.mjs +83 -0
- package/dist/schema-registry.generated.d.ts +8 -0
- package/dist/schema-registry.generated.mjs +11 -0
- package/dist/schema-resolve.d.ts +135 -0
- package/dist/schema-resolve.mjs +45 -0
- package/dist/schema-subtypes.d.ts +36 -0
- package/dist/schema-subtypes.mjs +94 -0
- package/dist/schema-types.generated.d.ts +1598 -0
- package/dist/schema-types.generated.mjs +0 -0
- package/dist/signed-blob.d.ts +63 -0
- package/dist/signed-blob.mjs +123 -0
- package/dist/tei-to-blocks.mjs +2 -2
- package/dist/unicode.mjs +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { HMDocumentFilter } from './hm-types';
|
|
2
|
+
export type ExploreScalar = string | number | boolean;
|
|
3
|
+
/** The result types the Explore surface can show; `type:` predicates name one. */
|
|
4
|
+
export type HMExploreResultType = 'document' | 'block' | 'comment' | 'space' | 'contact';
|
|
5
|
+
/** Attribute predicates in the Explore query AST. */
|
|
6
|
+
export type ExploreAttributePredicate = {
|
|
7
|
+
kind: 'attribute';
|
|
8
|
+
key: string;
|
|
9
|
+
operator: 'comparison';
|
|
10
|
+
comparison: ExploreComparisonOperator;
|
|
11
|
+
value: ExploreScalar;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'attribute';
|
|
14
|
+
key: string;
|
|
15
|
+
operator: 'contains' | 'prefix';
|
|
16
|
+
value: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: 'attribute';
|
|
19
|
+
key: string;
|
|
20
|
+
operator: 'exists' | 'missing';
|
|
21
|
+
};
|
|
22
|
+
/** Scope predicates in the Explore query AST. */
|
|
23
|
+
export type ExploreScopePredicate = {
|
|
24
|
+
kind: 'scope';
|
|
25
|
+
scope: 'space' | 'url';
|
|
26
|
+
value: string;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'scope';
|
|
29
|
+
scope: 'path';
|
|
30
|
+
value: string;
|
|
31
|
+
prefix: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** Leaf predicates in the Explore query AST. */
|
|
34
|
+
export type ExplorePredicate = ExploreAttributePredicate | ExploreScopePredicate | {
|
|
35
|
+
kind: 'type';
|
|
36
|
+
value: HMExploreResultType;
|
|
37
|
+
};
|
|
38
|
+
/** Boolean Explore query AST. */
|
|
39
|
+
export type ExploreQueryNode = {
|
|
40
|
+
kind: 'text';
|
|
41
|
+
value: string;
|
|
42
|
+
phrase: boolean;
|
|
43
|
+
} | {
|
|
44
|
+
kind: 'predicate';
|
|
45
|
+
predicate: ExplorePredicate;
|
|
46
|
+
} | {
|
|
47
|
+
kind: 'and' | 'or';
|
|
48
|
+
children: ExploreQueryNode[];
|
|
49
|
+
} | {
|
|
50
|
+
kind: 'not';
|
|
51
|
+
child: ExploreQueryNode;
|
|
52
|
+
};
|
|
53
|
+
/** A multi-key document sort directive. */
|
|
54
|
+
export type ExploreSortRule = {
|
|
55
|
+
key: string;
|
|
56
|
+
direction: 'asc' | 'desc';
|
|
57
|
+
};
|
|
58
|
+
/** Presentation directives kept separate from the boolean query tree. */
|
|
59
|
+
/** Result view names. */
|
|
60
|
+
export type ExploreView = 'list' | 'card' | 'table';
|
|
61
|
+
/** Presentation directives kept separate from the boolean query tree. */
|
|
62
|
+
export type ExplorePresentation = {
|
|
63
|
+
view?: ExploreView;
|
|
64
|
+
columns?: string[];
|
|
65
|
+
sort?: ExploreSortRule[];
|
|
66
|
+
};
|
|
67
|
+
/** A recoverable parser diagnostic. */
|
|
68
|
+
export type ExploreDiagnostic = {
|
|
69
|
+
message: string;
|
|
70
|
+
start: number;
|
|
71
|
+
end: number;
|
|
72
|
+
severity: 'warning' | 'error';
|
|
73
|
+
};
|
|
74
|
+
/** The parsed query and its presentation directives. */
|
|
75
|
+
export type ParsedExploreQuery = {
|
|
76
|
+
ast: ExploreQueryNode | null;
|
|
77
|
+
presentation: ExplorePresentation;
|
|
78
|
+
diagnostics: ExploreDiagnostic[];
|
|
79
|
+
};
|
|
80
|
+
export type ExploreComparisonOperator = '=' | '!=' | '<' | '<=' | '>' | '>=';
|
|
81
|
+
/** Where a query runs: the whole node, or one site (a document subtree, by its canonical `hm://` URL). */
|
|
82
|
+
export type ExploreQueryContext = {
|
|
83
|
+
type: 'node';
|
|
84
|
+
} | {
|
|
85
|
+
type: 'site';
|
|
86
|
+
url: string;
|
|
87
|
+
};
|
|
88
|
+
/** Quotes a value for the grammar when it contains whitespace, parentheses, quotes, or operators. */
|
|
89
|
+
export declare function quoteExploreValue(value: string): string;
|
|
90
|
+
/** Parses an Explore query into a forgiving boolean AST and presentation directives. */
|
|
91
|
+
export declare function parseExploreQuery(query: string): ParsedExploreQuery;
|
|
92
|
+
/** Serializes a parsed query's AST and presentation directives into a stable q string. */
|
|
93
|
+
export declare function serializeExploreQuery(query: ParsedExploreQuery | ExploreQueryNode | null, presentation?: ExplorePresentation): string;
|
|
94
|
+
/** The compiled document-side query and the projections the Explore surface needs. */
|
|
95
|
+
export type ExploreCompilation = {
|
|
96
|
+
/** The `DocumentFilter` for `QueryDocuments`, in protobuf JSON; undefined when nothing constrains documents. */
|
|
97
|
+
filter?: HMDocumentFilter;
|
|
98
|
+
documentPredicates: ExplorePredicate[];
|
|
99
|
+
textTerms: Array<{
|
|
100
|
+
value: string;
|
|
101
|
+
phrase: boolean;
|
|
102
|
+
}>;
|
|
103
|
+
requestedTypes: HMExploreResultType[];
|
|
104
|
+
excludedTypes: HMExploreResultType[];
|
|
105
|
+
positiveScopes: Array<Extract<ExplorePredicate, {
|
|
106
|
+
kind: 'scope';
|
|
107
|
+
}>>;
|
|
108
|
+
presentation: ExplorePresentation;
|
|
109
|
+
diagnostics: ExploreDiagnostic[];
|
|
110
|
+
};
|
|
111
|
+
/** Compiles the AST while preserving boolean structure and reporting dropped search terms. */
|
|
112
|
+
export declare function compileExploreQuery(parsed: ParsedExploreQuery, context: ExploreQueryContext): ExploreCompilation;
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
2
|
+
|
|
3
|
+
// src/explore-query.ts
|
|
4
|
+
var resultTypes = /* @__PURE__ */ new Set(["document", "block", "comment", "space", "contact"]);
|
|
5
|
+
var comparisonOperators = /* @__PURE__ */ new Set(["=", "!=", "<", "<=", ">", ">="]);
|
|
6
|
+
function diagnostic(message, start, end, severity = "warning") {
|
|
7
|
+
return { message, start, end, severity };
|
|
8
|
+
}
|
|
9
|
+
function extractPresentation(query, tokens, diagnostics) {
|
|
10
|
+
const presentation = {};
|
|
11
|
+
const ranges = [];
|
|
12
|
+
for (let index = 0; index < tokens.length - 2; index++) {
|
|
13
|
+
const nameToken = tokens[index];
|
|
14
|
+
const operatorToken = tokens[index + 1];
|
|
15
|
+
const valueToken = tokens[index + 2];
|
|
16
|
+
if ((nameToken == null ? void 0 : nameToken.kind) !== "word" || !["view", "cols", "sort"].includes(nameToken.value.toLowerCase())) continue;
|
|
17
|
+
if ((operatorToken == null ? void 0 : operatorToken.kind) !== "operator" || operatorToken.value !== ":") continue;
|
|
18
|
+
if ((valueToken == null ? void 0 : valueToken.kind) !== "word" && (valueToken == null ? void 0 : valueToken.kind) !== "quoted") continue;
|
|
19
|
+
const name = nameToken.value.toLowerCase();
|
|
20
|
+
const value = valueToken.value;
|
|
21
|
+
ranges.push([nameToken.start, valueToken.end]);
|
|
22
|
+
if (name === "view") {
|
|
23
|
+
if (value === "list" || value === "card" || value === "table") presentation.view = value;
|
|
24
|
+
else diagnostics.push(diagnostic(`Unknown view "${value}".`, nameToken.start, valueToken.end));
|
|
25
|
+
} else if (name === "cols") {
|
|
26
|
+
const columns = value.split(",").map((column) => column.trim()).filter(Boolean);
|
|
27
|
+
if (columns.length) presentation.columns = columns;
|
|
28
|
+
else
|
|
29
|
+
diagnostics.push(diagnostic("The cols directive needs at least one column.", nameToken.start, valueToken.end));
|
|
30
|
+
} else {
|
|
31
|
+
const sort = value.split(",").map((item) => item.trim()).filter(Boolean).map(
|
|
32
|
+
(item) => ({
|
|
33
|
+
key: item.startsWith("-") ? item.slice(1) : item,
|
|
34
|
+
direction: item.startsWith("-") ? "desc" : "asc"
|
|
35
|
+
})
|
|
36
|
+
).filter((item) => item.key);
|
|
37
|
+
if (sort.length) presentation.sort = sort;
|
|
38
|
+
else diagnostics.push(diagnostic("The sort directive needs at least one key.", nameToken.start, valueToken.end));
|
|
39
|
+
}
|
|
40
|
+
index += 2;
|
|
41
|
+
}
|
|
42
|
+
let result = "";
|
|
43
|
+
let cursor = 0;
|
|
44
|
+
for (const [start, end] of ranges) {
|
|
45
|
+
result += query.slice(cursor, start);
|
|
46
|
+
cursor = end;
|
|
47
|
+
}
|
|
48
|
+
return { query: result + query.slice(cursor), presentation };
|
|
49
|
+
}
|
|
50
|
+
function tokenize(query, diagnostics) {
|
|
51
|
+
var _a;
|
|
52
|
+
const tokens = [];
|
|
53
|
+
let index = 0;
|
|
54
|
+
while (index < query.length) {
|
|
55
|
+
if (/\s/.test(query[index])) {
|
|
56
|
+
index++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const start = index;
|
|
60
|
+
const char = query[index];
|
|
61
|
+
if (char === "(" || char === ")") {
|
|
62
|
+
tokens.push({ kind: char === "(" ? "lparen" : "rparen", value: char, start, end: ++index });
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (char === '"') {
|
|
66
|
+
index++;
|
|
67
|
+
let value = "";
|
|
68
|
+
let closed = false;
|
|
69
|
+
while (index < query.length) {
|
|
70
|
+
const current = query[index++];
|
|
71
|
+
if (current === "\\" && index < query.length) value += query[index++];
|
|
72
|
+
else if (current === '"') {
|
|
73
|
+
closed = true;
|
|
74
|
+
break;
|
|
75
|
+
} else value += current;
|
|
76
|
+
}
|
|
77
|
+
if (!closed) diagnostics.push(diagnostic("Unterminated quoted phrase.", start, index));
|
|
78
|
+
tokens.push({ kind: "quoted", value, start, end: index });
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (query.startsWith("!=", index) || query.startsWith(">=", index) || query.startsWith("<=", index)) {
|
|
82
|
+
tokens.push({ kind: "operator", value: query.slice(index, index + 2), start, end: index += 2 });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if ("=<>~^".includes(char)) {
|
|
86
|
+
tokens.push({ kind: "operator", value: char, start, end: ++index });
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (char === ":") {
|
|
90
|
+
tokens.push({ kind: "operator", value: char, start, end: ++index });
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const valueToken = ((_a = tokens[tokens.length - 1]) == null ? void 0 : _a.kind) === "operator";
|
|
94
|
+
while (index < query.length && !/\s/.test(query[index]) && !"()=<>~^".includes(query[index]) && !(query[index] === "!" && query[index + 1] === "=") && !(query[index] === ":" && !valueToken)) {
|
|
95
|
+
index++;
|
|
96
|
+
}
|
|
97
|
+
tokens.push({ kind: "word", value: query.slice(start, index), start, end: index });
|
|
98
|
+
}
|
|
99
|
+
return tokens;
|
|
100
|
+
}
|
|
101
|
+
function parseScalar(value) {
|
|
102
|
+
if (/^-?\d+$/.test(value)) return Number(value);
|
|
103
|
+
if (value === "true") return true;
|
|
104
|
+
if (value === "false") return false;
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
function quoteExploreValue(value) {
|
|
108
|
+
return /[\s()"=<>~^]/.test(value) ? `"${value.replace(/"/g, '\\"')}"` : value;
|
|
109
|
+
}
|
|
110
|
+
function flatten(kind, children) {
|
|
111
|
+
const flattened = children.flatMap((child) => child.kind === kind ? child.children : [child]);
|
|
112
|
+
return flattened.length === 1 ? flattened[0] : { kind, children: flattened };
|
|
113
|
+
}
|
|
114
|
+
var ExploreParser = class {
|
|
115
|
+
constructor(tokens, diagnostics) {
|
|
116
|
+
this.tokens = tokens;
|
|
117
|
+
this.diagnostics = diagnostics;
|
|
118
|
+
}
|
|
119
|
+
index = 0;
|
|
120
|
+
parse() {
|
|
121
|
+
const node = this.parseOr();
|
|
122
|
+
while (this.current()) {
|
|
123
|
+
const token = this.current();
|
|
124
|
+
this.diagnostics.push(diagnostic(`Unexpected token "${token.value}".`, token.start, token.end));
|
|
125
|
+
this.index++;
|
|
126
|
+
}
|
|
127
|
+
return node;
|
|
128
|
+
}
|
|
129
|
+
current() {
|
|
130
|
+
return this.tokens[this.index];
|
|
131
|
+
}
|
|
132
|
+
parseOr() {
|
|
133
|
+
var _a, _b;
|
|
134
|
+
const nodes = [];
|
|
135
|
+
const first = this.parseAnd();
|
|
136
|
+
if (first) nodes.push(first);
|
|
137
|
+
while (((_a = this.current()) == null ? void 0 : _a.kind) === "word" && ((_b = this.current()) == null ? void 0 : _b.value.toUpperCase()) === "OR") {
|
|
138
|
+
const token = this.current();
|
|
139
|
+
this.index++;
|
|
140
|
+
const next = this.parseAnd();
|
|
141
|
+
if (next) nodes.push(next);
|
|
142
|
+
else this.diagnostics.push(diagnostic("OR needs a right-hand expression.", token.start, token.end));
|
|
143
|
+
}
|
|
144
|
+
return nodes.length ? flatten("or", nodes) : null;
|
|
145
|
+
}
|
|
146
|
+
parseAnd() {
|
|
147
|
+
const nodes = [];
|
|
148
|
+
const first = this.parseUnary();
|
|
149
|
+
if (first) nodes.push(first);
|
|
150
|
+
while (true) {
|
|
151
|
+
const token = this.current();
|
|
152
|
+
if ((token == null ? void 0 : token.kind) === "word" && token.value.toUpperCase() === "AND") {
|
|
153
|
+
this.index++;
|
|
154
|
+
const next = this.parseUnary();
|
|
155
|
+
if (next) nodes.push(next);
|
|
156
|
+
else this.diagnostics.push(diagnostic("AND needs a right-hand expression.", token.start, token.end));
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (token && (token.kind === "lparen" || token.kind === "quoted" || token.kind === "word") && token.value.toUpperCase() !== "OR") {
|
|
160
|
+
const next = this.parseUnary();
|
|
161
|
+
if (next) nodes.push(next);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
return nodes.length ? flatten("and", nodes) : null;
|
|
167
|
+
}
|
|
168
|
+
parseUnary() {
|
|
169
|
+
const token = this.current();
|
|
170
|
+
if ((token == null ? void 0 : token.kind) === "word" && token.value.toUpperCase() === "NOT") {
|
|
171
|
+
this.index++;
|
|
172
|
+
const child = this.parseUnary();
|
|
173
|
+
if (!child) this.diagnostics.push(diagnostic("NOT needs an expression.", token.start, token.end));
|
|
174
|
+
return child ? { kind: "not", child } : null;
|
|
175
|
+
}
|
|
176
|
+
if ((token == null ? void 0 : token.kind) === "operator") {
|
|
177
|
+
this.diagnostics.push(diagnostic(`Unexpected operator "${token.value}".`, token.start, token.end));
|
|
178
|
+
this.index++;
|
|
179
|
+
return this.parseUnary();
|
|
180
|
+
}
|
|
181
|
+
return this.parsePrimary();
|
|
182
|
+
}
|
|
183
|
+
parsePrimary() {
|
|
184
|
+
var _a;
|
|
185
|
+
const token = this.current();
|
|
186
|
+
if (!token) return null;
|
|
187
|
+
if (token.kind === "lparen") {
|
|
188
|
+
this.index++;
|
|
189
|
+
const child = this.parseOr();
|
|
190
|
+
if (((_a = this.current()) == null ? void 0 : _a.kind) === "rparen") this.index++;
|
|
191
|
+
else this.diagnostics.push(diagnostic("Missing closing parenthesis.", token.start, token.end));
|
|
192
|
+
return child;
|
|
193
|
+
}
|
|
194
|
+
if (token.kind === "rparen") {
|
|
195
|
+
this.diagnostics.push(diagnostic("Unexpected closing parenthesis.", token.start, token.end));
|
|
196
|
+
this.index++;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
this.index++;
|
|
200
|
+
if (token.kind === "quoted") return { kind: "text", value: token.value, phrase: true };
|
|
201
|
+
const operator = this.current();
|
|
202
|
+
if ((operator == null ? void 0 : operator.kind) !== "operator") return { kind: "text", value: token.value, phrase: false };
|
|
203
|
+
this.index++;
|
|
204
|
+
const valueToken = this.current();
|
|
205
|
+
if (!valueToken || valueToken.kind === "rparen" || valueToken.kind === "operator") {
|
|
206
|
+
this.diagnostics.push(
|
|
207
|
+
diagnostic(`Predicate "${token.value}${operator.value}" needs a value.`, token.start, operator.end)
|
|
208
|
+
);
|
|
209
|
+
return { kind: "text", value: token.value, phrase: false };
|
|
210
|
+
}
|
|
211
|
+
this.index++;
|
|
212
|
+
const value = valueToken.value;
|
|
213
|
+
if (operator.value === ":" && token.value === "type") {
|
|
214
|
+
if (resultTypes.has(value))
|
|
215
|
+
return { kind: "predicate", predicate: { kind: "type", value } };
|
|
216
|
+
return { kind: "predicate", predicate: { kind: "attribute", key: token.value, operator: "contains", value } };
|
|
217
|
+
}
|
|
218
|
+
if (operator.value === ":" && token.value === "in") {
|
|
219
|
+
if (value === "site" || value === "node") return null;
|
|
220
|
+
return {
|
|
221
|
+
kind: "predicate",
|
|
222
|
+
predicate: value.startsWith("hm://") ? { kind: "scope", scope: "url", value } : { kind: "scope", scope: "space", value }
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (operator.value === ":" && token.value === "path") {
|
|
226
|
+
const prefix = value.endsWith("/*");
|
|
227
|
+
return {
|
|
228
|
+
kind: "predicate",
|
|
229
|
+
predicate: { kind: "scope", scope: "path", value: prefix ? value.slice(0, -2) : value, prefix }
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if ((token.value === "has" || token.value === "missing") && operator.value === ":") {
|
|
233
|
+
return {
|
|
234
|
+
kind: "predicate",
|
|
235
|
+
predicate: { kind: "attribute", key: value, operator: token.value === "has" ? "exists" : "missing" }
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
if (operator.value === ":" || operator.value === "~" || operator.value === "^") {
|
|
239
|
+
return {
|
|
240
|
+
kind: "predicate",
|
|
241
|
+
predicate: {
|
|
242
|
+
kind: "attribute",
|
|
243
|
+
key: token.value,
|
|
244
|
+
operator: operator.value === "^" ? "prefix" : "contains",
|
|
245
|
+
value: valueToken.kind === "quoted" ? value : String(parseScalar(value))
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
if (comparisonOperators.has(operator.value)) {
|
|
250
|
+
return {
|
|
251
|
+
kind: "predicate",
|
|
252
|
+
predicate: {
|
|
253
|
+
kind: "attribute",
|
|
254
|
+
key: token.value,
|
|
255
|
+
operator: "comparison",
|
|
256
|
+
comparison: operator.value,
|
|
257
|
+
value: parseScalar(value)
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return { kind: "text", value: `${token.value}${operator.value}${value}`, phrase: false };
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
function parseExploreQuery(query) {
|
|
265
|
+
const diagnostics = [];
|
|
266
|
+
const source = query;
|
|
267
|
+
const sourceTokens = tokenize(source, diagnostics);
|
|
268
|
+
const { query: withoutPresentation, presentation } = extractPresentation(source, sourceTokens, diagnostics);
|
|
269
|
+
const ast = new ExploreParser(tokenize(withoutPresentation, diagnostics), diagnostics).parse();
|
|
270
|
+
return { ast, presentation, diagnostics };
|
|
271
|
+
}
|
|
272
|
+
function nodePrecedence(node) {
|
|
273
|
+
return node.kind === "or" ? 1 : node.kind === "and" ? 2 : node.kind === "not" ? 3 : 4;
|
|
274
|
+
}
|
|
275
|
+
function serializePredicate(predicate) {
|
|
276
|
+
if (predicate.kind === "type") return `type:${predicate.value}`;
|
|
277
|
+
if (predicate.kind === "scope") {
|
|
278
|
+
if (predicate.scope === "space" || predicate.scope === "url") return `in:${predicate.value}`;
|
|
279
|
+
const pathPredicate = predicate;
|
|
280
|
+
return `path:${pathPredicate.value}${pathPredicate.prefix ? "/*" : ""}`;
|
|
281
|
+
}
|
|
282
|
+
if (predicate.operator === "exists" || predicate.operator === "missing")
|
|
283
|
+
return `${predicate.operator === "exists" ? "has" : "missing"}:${predicate.key}`;
|
|
284
|
+
if (predicate.operator === "contains") return `${predicate.key}:${quoteExploreValue(predicate.value)}`;
|
|
285
|
+
if (predicate.operator === "prefix") return `${predicate.key}^${quoteExploreValue(predicate.value)}`;
|
|
286
|
+
const comparison = predicate;
|
|
287
|
+
return `${comparison.key}${comparison.comparison}${typeof comparison.value === "string" ? quoteExploreValue(comparison.value) : String(comparison.value)}`;
|
|
288
|
+
}
|
|
289
|
+
function serializeNode(node, parentPrecedence = 0) {
|
|
290
|
+
let value;
|
|
291
|
+
if (node.kind === "text") value = node.phrase ? `"${node.value.replace(/"/g, '\\"')}"` : node.value;
|
|
292
|
+
else if (node.kind === "predicate") value = serializePredicate(node.predicate);
|
|
293
|
+
else if (node.kind === "not") value = `NOT ${serializeNode(node.child, nodePrecedence(node))}`;
|
|
294
|
+
else
|
|
295
|
+
value = node.children.map((child) => serializeNode(child, nodePrecedence(node))).join(node.kind === "and" ? " AND " : " OR ");
|
|
296
|
+
return nodePrecedence(node) < parentPrecedence ? `(${value})` : value;
|
|
297
|
+
}
|
|
298
|
+
function serializeExploreQuery(query, presentation) {
|
|
299
|
+
var _a, _b;
|
|
300
|
+
const ast = query && "kind" in query ? query : query == null ? void 0 : query.ast;
|
|
301
|
+
const directives = query && "kind" in query ? presentation : query == null ? void 0 : query.presentation;
|
|
302
|
+
const parts = ast ? [serializeNode(ast)] : [];
|
|
303
|
+
if (directives == null ? void 0 : directives.view) parts.push(`view:${directives.view}`);
|
|
304
|
+
if ((_a = directives == null ? void 0 : directives.columns) == null ? void 0 : _a.length) parts.push(`cols:${directives.columns.join(",")}`);
|
|
305
|
+
if ((_b = directives == null ? void 0 : directives.sort) == null ? void 0 : _b.length)
|
|
306
|
+
parts.push(`sort:${directives.sort.map((rule) => `${rule.direction === "desc" ? "-" : ""}${rule.key}`).join(",")}`);
|
|
307
|
+
return parts.join(" ");
|
|
308
|
+
}
|
|
309
|
+
function attributeValueJson(value) {
|
|
310
|
+
return typeof value === "number" ? { intValue: value } : typeof value === "boolean" ? { boolValue: value } : { stringValue: value };
|
|
311
|
+
}
|
|
312
|
+
function comparisonOperatorJson(operator) {
|
|
313
|
+
switch (operator) {
|
|
314
|
+
case "!=":
|
|
315
|
+
return "NOT_EQUAL";
|
|
316
|
+
case "<":
|
|
317
|
+
return "LESS_THAN";
|
|
318
|
+
case "<=":
|
|
319
|
+
return "LESS_THAN_OR_EQUAL";
|
|
320
|
+
case ">":
|
|
321
|
+
return "GREATER_THAN";
|
|
322
|
+
case ">=":
|
|
323
|
+
return "GREATER_THAN_OR_EQUAL";
|
|
324
|
+
default:
|
|
325
|
+
return "EQUAL";
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function predicateFilter(predicate) {
|
|
329
|
+
if (predicate.kind === "type") return void 0;
|
|
330
|
+
if (predicate.kind === "scope") {
|
|
331
|
+
if (predicate.scope === "space") return { spaceMatch: { space: predicate.value } };
|
|
332
|
+
if (predicate.scope === "url") return { urlMatch: { url: predicate.value, prefix: true } };
|
|
333
|
+
const pathPredicate = predicate;
|
|
334
|
+
return { pathMatch: { path: pathPredicate.value === "/" ? "" : pathPredicate.value, prefix: pathPredicate.prefix } };
|
|
335
|
+
}
|
|
336
|
+
if (predicate.operator === "exists") return { exists: { key: predicate.key } };
|
|
337
|
+
if (predicate.operator === "missing") return { missing: { key: predicate.key } };
|
|
338
|
+
if (predicate.operator === "contains" || predicate.operator === "prefix") {
|
|
339
|
+
return {
|
|
340
|
+
stringMatch: {
|
|
341
|
+
key: predicate.key,
|
|
342
|
+
value: predicate.value,
|
|
343
|
+
caseSensitive: false,
|
|
344
|
+
prefix: predicate.operator === "prefix"
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
const comparison = predicate;
|
|
349
|
+
return {
|
|
350
|
+
comparison: {
|
|
351
|
+
key: comparison.key,
|
|
352
|
+
operator: comparisonOperatorJson(comparison.comparison),
|
|
353
|
+
value: attributeValueJson(comparison.value)
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
function compileNode(node) {
|
|
358
|
+
if (!node || node.kind === "text" || node.kind === "predicate" && node.predicate.kind === "type")
|
|
359
|
+
return { unconstrained: true };
|
|
360
|
+
if (node.kind === "predicate") {
|
|
361
|
+
const filter = predicateFilter(node.predicate);
|
|
362
|
+
return filter ? { filter, unconstrained: false } : { unconstrained: true };
|
|
363
|
+
}
|
|
364
|
+
if (node.kind === "not") {
|
|
365
|
+
const child = compileNode(node.child);
|
|
366
|
+
return child.filter && !child.unconstrained ? { filter: { not: { filter: child.filter } }, unconstrained: false } : { unconstrained: true };
|
|
367
|
+
}
|
|
368
|
+
const children = node.children.map(compileNode);
|
|
369
|
+
if (node.kind === "or" && children.some((child) => child.unconstrained)) return { unconstrained: true };
|
|
370
|
+
const filters = children.flatMap((child) => child.filter ? [child.filter] : []);
|
|
371
|
+
if (!filters.length) return { unconstrained: true };
|
|
372
|
+
if (filters.length === 1) return { filter: filters[0], unconstrained: children.some((child) => child.unconstrained) };
|
|
373
|
+
return {
|
|
374
|
+
filter: node.kind === "and" ? { and: { filters } } : { or: { filters } },
|
|
375
|
+
unconstrained: children.some((child) => child.unconstrained)
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
function contextFilter(context) {
|
|
379
|
+
if (context.type !== "site") return null;
|
|
380
|
+
return { urlMatch: { url: context.url, prefix: true } };
|
|
381
|
+
}
|
|
382
|
+
function compileExploreQuery(parsed, context) {
|
|
383
|
+
const documentPredicates = [];
|
|
384
|
+
const textTerms = [];
|
|
385
|
+
const requestedTypes = [];
|
|
386
|
+
const excludedTypes = [];
|
|
387
|
+
const positiveScopes = [];
|
|
388
|
+
const diagnostics = [];
|
|
389
|
+
const containsText = (node) => {
|
|
390
|
+
if (!node) return false;
|
|
391
|
+
if (node.kind === "text") return true;
|
|
392
|
+
if (node.kind === "not") return containsText(node.child);
|
|
393
|
+
if (node.kind === "and" || node.kind === "or") return node.children.some(containsText);
|
|
394
|
+
return false;
|
|
395
|
+
};
|
|
396
|
+
const containsPredicate = (node) => {
|
|
397
|
+
if (!node) return false;
|
|
398
|
+
if (node.kind === "predicate") return true;
|
|
399
|
+
if (node.kind === "not") return containsPredicate(node.child);
|
|
400
|
+
if (node.kind === "and" || node.kind === "or") return node.children.some(containsPredicate);
|
|
401
|
+
return false;
|
|
402
|
+
};
|
|
403
|
+
const walkWithPolarity = (node, positive = true) => {
|
|
404
|
+
if (!node) return;
|
|
405
|
+
if (node.kind === "text") textTerms.push({ value: node.value, phrase: node.phrase });
|
|
406
|
+
if (node.kind === "predicate") {
|
|
407
|
+
if (node.predicate.kind === "type") {
|
|
408
|
+
if (positive) requestedTypes.push(node.predicate.value);
|
|
409
|
+
else excludedTypes.push(node.predicate.value);
|
|
410
|
+
} else {
|
|
411
|
+
documentPredicates.push(node.predicate);
|
|
412
|
+
if (positive && node.predicate.kind === "scope") positiveScopes.push(node.predicate);
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (node.kind === "not") {
|
|
417
|
+
const child = compileNode(node.child);
|
|
418
|
+
if (positive && child.unconstrained && containsText(node.child) && containsPredicate(node.child)) {
|
|
419
|
+
diagnostics.push(
|
|
420
|
+
diagnostic(
|
|
421
|
+
"This exclusion could not be applied to the text part; results may include excluded documents.",
|
|
422
|
+
0,
|
|
423
|
+
0
|
|
424
|
+
)
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
return walkWithPolarity(node.child, !positive);
|
|
428
|
+
}
|
|
429
|
+
if (node.kind === "and" || node.kind === "or") node.children.forEach((child) => walkWithPolarity(child, positive));
|
|
430
|
+
};
|
|
431
|
+
walkWithPolarity(parsed.ast);
|
|
432
|
+
const compiled = compileNode(parsed.ast);
|
|
433
|
+
const uniqueRequestedTypes = Array.from(new Set(requestedTypes));
|
|
434
|
+
const rootsOnly = uniqueRequestedTypes.length === 1 && uniqueRequestedTypes[0] === "space" ? { pathMatch: { path: "", prefix: false } } : null;
|
|
435
|
+
const filters = [contextFilter(context), rootsOnly, compiled.filter].filter(
|
|
436
|
+
(filter2) => !!filter2
|
|
437
|
+
);
|
|
438
|
+
const filter = filters.length === 0 ? void 0 : filters.length === 1 ? filters[0] : { and: { filters } };
|
|
439
|
+
return {
|
|
440
|
+
filter,
|
|
441
|
+
documentPredicates,
|
|
442
|
+
textTerms,
|
|
443
|
+
requestedTypes: uniqueRequestedTypes,
|
|
444
|
+
excludedTypes: Array.from(new Set(excludedTypes)),
|
|
445
|
+
positiveScopes,
|
|
446
|
+
presentation: parsed.presentation,
|
|
447
|
+
diagnostics
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
export {
|
|
451
|
+
compileExploreQuery,
|
|
452
|
+
parseExploreQuery,
|
|
453
|
+
quoteExploreValue,
|
|
454
|
+
serializeExploreQuery
|
|
455
|
+
};
|
package/dist/hm-resolver.mjs
CHANGED