explorbot 0.4.10 → 0.5.0
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/README.md +4 -1
- package/bin/mdq.ts +18 -0
- package/boat/api-tester/src/ai/chief.ts +72 -0
- package/boat/api-tester/src/api-client.ts +37 -0
- package/boat/prima/src/prima.ts +41 -2
- package/dist/bin/mdq.js +19 -0
- package/dist/boat/api-tester/src/ai/chief.js +69 -0
- package/dist/boat/api-tester/src/api-client.js +26 -0
- package/dist/boat/prima/src/prima.js +42 -2
- package/dist/package.json +3 -2
- package/dist/src/ai/agent.d.ts +3 -1
- package/dist/src/ai/judge-provider.d.ts +17 -0
- package/dist/src/ai/judge-provider.js +56 -0
- package/dist/src/ai/judge-tool.d.ts +2 -0
- package/dist/src/ai/judge-tool.js +33 -0
- package/dist/src/ai/judge.d.ts +28 -0
- package/dist/src/ai/judge.js +71 -0
- package/dist/src/ai/navigator.d.ts +3 -0
- package/dist/src/ai/navigator.js +13 -4
- package/dist/src/ai/pilot.d.ts +4 -0
- package/dist/src/ai/pilot.js +57 -5
- package/dist/src/ai/planner.js +9 -6
- package/dist/src/ai/provider.d.ts +4 -1
- package/dist/src/ai/provider.js +52 -7
- package/dist/src/ai/researcher/deep-analysis.js +2 -2
- package/dist/src/ai/researcher/locators.js +2 -2
- package/dist/src/ai/researcher/pagination.js +1 -1
- package/dist/src/ai/researcher/research-result.js +2 -2
- package/dist/src/ai/researcher.js +1 -1
- package/dist/src/ai/task-agent.d.ts +2 -0
- package/dist/src/ai/task-agent.js +3 -1
- package/dist/src/ai/tester.js +19 -15
- package/dist/src/ai/tools.d.ts +4 -3
- package/dist/src/ai/tools.js +35 -7
- package/dist/src/api/request-result.js +2 -1
- package/dist/src/command-handler.d.ts +1 -0
- package/dist/src/command-handler.js +24 -3
- package/dist/src/commands/base-command.d.ts +5 -0
- package/dist/src/commands/base-command.js +3 -0
- package/dist/src/commands/explore-command.d.ts +2 -1
- package/dist/src/commands/explore-command.js +12 -1
- package/dist/src/commands/freesail-command.js +8 -2
- package/dist/src/commands/init-command.js +1 -1
- package/dist/src/commands/navigate-command.d.ts +2 -1
- package/dist/src/commands/navigate-command.js +6 -0
- package/dist/src/commands/plan-load-command.d.ts +2 -1
- package/dist/src/commands/plan-load-command.js +4 -0
- package/dist/src/commands/plans-command.d.ts +3 -9
- package/dist/src/commands/plans-command.js +11 -21
- package/dist/src/commands/rerun-command.d.ts +2 -1
- package/dist/src/commands/rerun-command.js +5 -1
- package/dist/src/commands/research-command.d.ts +2 -1
- package/dist/src/commands/research-command.js +6 -0
- package/dist/src/commands/test-command.d.ts +2 -1
- package/dist/src/commands/test-command.js +4 -1
- package/dist/src/components/Autocomplete.js +26 -12
- package/dist/src/components/InputReadline.js +10 -1
- package/dist/src/config.d.ts +6 -0
- package/dist/src/experience-tracker.js +4 -3
- package/dist/src/explorbot.d.ts +3 -0
- package/dist/src/explorbot.js +8 -0
- package/dist/src/knowledge-tracker.js +1 -1
- package/dist/src/state-manager.d.ts +2 -0
- package/dist/src/state-manager.js +16 -0
- package/dist/src/test-plan.d.ts +11 -0
- package/dist/src/test-plan.js +54 -2
- package/dist/src/utils/aria-ref.js +1 -1
- package/dist/src/utils/logger.js +8 -2
- package/dist/src/utils/markdown-query.d.ts +1 -48
- package/dist/src/utils/markdown-query.js +1 -444
- package/dist/src/utils/mdq/cli.d.ts +6 -0
- package/dist/src/utils/mdq/cli.js +122 -0
- package/dist/src/utils/mdq/edit.d.ts +24 -0
- package/dist/src/utils/mdq/edit.js +147 -0
- package/dist/src/utils/mdq/query.d.ts +118 -0
- package/dist/src/utils/mdq/query.js +451 -0
- package/dist/src/utils/strings.d.ts +1 -0
- package/dist/src/utils/strings.js +7 -0
- package/dist/src/utils/test-files.d.ts +1 -0
- package/dist/src/utils/test-files.js +5 -2
- package/docs/api-testing/planning.md +1 -1
- package/docs/superpowers/plans/2026-09-15-mdq-package.md +130 -94
- package/docs/superpowers/specs/2026-09-18-judge-decision-model-design.md +79 -0
- package/package.json +3 -2
- package/src/ai/agent.ts +3 -1
- package/src/ai/judge-provider.ts +62 -0
- package/src/ai/judge-tool.ts +35 -0
- package/src/ai/judge.ts +75 -0
- package/src/ai/navigator.ts +15 -4
- package/src/ai/pilot.ts +58 -5
- package/src/ai/planner.ts +9 -6
- package/src/ai/provider.ts +51 -7
- package/src/ai/researcher/deep-analysis.ts +2 -2
- package/src/ai/researcher/locators.ts +2 -2
- package/src/ai/researcher/pagination.ts +1 -1
- package/src/ai/researcher/research-result.ts +2 -2
- package/src/ai/researcher.ts +1 -1
- package/src/ai/task-agent.ts +4 -1
- package/src/ai/tester.ts +19 -16
- package/src/ai/tools.ts +42 -7
- package/src/api/request-result.ts +2 -1
- package/src/command-handler.ts +28 -3
- package/src/commands/base-command.ts +9 -0
- package/src/commands/explore-command.ts +15 -2
- package/src/commands/freesail-command.ts +9 -2
- package/src/commands/init-command.ts +1 -1
- package/src/commands/navigate-command.ts +8 -1
- package/src/commands/plan-load-command.ts +6 -1
- package/src/commands/plans-command.ts +13 -29
- package/src/commands/rerun-command.ts +7 -2
- package/src/commands/research-command.ts +8 -1
- package/src/commands/test-command.ts +6 -2
- package/src/components/Autocomplete.tsx +39 -10
- package/src/components/InputReadline.tsx +10 -1
- package/src/config.ts +1 -0
- package/src/experience-tracker.ts +4 -3
- package/src/explorbot.ts +8 -0
- package/src/knowledge-tracker.ts +1 -1
- package/src/state-manager.ts +16 -0
- package/src/test-plan.ts +67 -2
- package/src/utils/aria-ref.ts +1 -1
- package/src/utils/logger.ts +6 -1
- package/src/utils/markdown-query.ts +1 -519
- package/src/utils/mdq/cli.ts +118 -0
- package/src/utils/mdq/edit.ts +158 -0
- package/src/utils/mdq/query.ts +556 -0
- package/src/utils/strings.ts +7 -0
- package/src/utils/test-files.ts +5 -2
|
@@ -1,519 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type SelectorType = 'section' | 'section1' | 'section2' | 'section3' | 'section4' | 'section5' | 'section6' | 'table' | 'heading' | 'paragraph' | 'list' | 'item' | 'code' | 'blockquote' | 'hr' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
|
|
4
|
-
|
|
5
|
-
export interface TextMatcher {
|
|
6
|
-
mode: 'exact' | 'contains' | 'regex';
|
|
7
|
-
value: string;
|
|
8
|
-
negated: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface QuerySegment {
|
|
12
|
-
selector: SelectorType;
|
|
13
|
-
textMatch?: TextMatcher;
|
|
14
|
-
index: number | null;
|
|
15
|
-
slice: { from?: number; to?: number } | null;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface MatchedRange {
|
|
19
|
-
token: Token;
|
|
20
|
-
start: number;
|
|
21
|
-
length: number;
|
|
22
|
-
innerTokens?: MatchedRange[];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function parseQuery(input: string): QuerySegment[] {
|
|
26
|
-
const segments: QuerySegment[] = [];
|
|
27
|
-
let pos = 0;
|
|
28
|
-
|
|
29
|
-
function peek(): string {
|
|
30
|
-
return pos < input.length ? input[pos] : '';
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function advance(): string {
|
|
34
|
-
return input[pos++] || '';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function skipWhitespace() {
|
|
38
|
-
while (pos < input.length && /\s/.test(input[pos])) pos++;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function readIdentifier(): string {
|
|
42
|
-
const start = pos;
|
|
43
|
-
while (pos < input.length && /[a-zA-Z_\d]/.test(input[pos])) pos++;
|
|
44
|
-
return input.slice(start, pos);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function readQuotedString(): string {
|
|
48
|
-
const quote = advance();
|
|
49
|
-
let result = '';
|
|
50
|
-
while (pos < input.length && input[pos] !== quote) {
|
|
51
|
-
if (input[pos] === '\\') {
|
|
52
|
-
pos++;
|
|
53
|
-
result += input[pos] || '';
|
|
54
|
-
} else {
|
|
55
|
-
result += input[pos];
|
|
56
|
-
}
|
|
57
|
-
pos++;
|
|
58
|
-
}
|
|
59
|
-
if (pos < input.length) pos++;
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function readUntilAny(chars: string): string {
|
|
64
|
-
const start = pos;
|
|
65
|
-
while (pos < input.length && !chars.includes(input[pos])) pos++;
|
|
66
|
-
return input.slice(start, pos);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function parseTextMatcher(): TextMatcher {
|
|
70
|
-
let negated = false;
|
|
71
|
-
if (peek() === '!') {
|
|
72
|
-
negated = true;
|
|
73
|
-
advance();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (peek() === '~') {
|
|
77
|
-
advance();
|
|
78
|
-
const value = readQuotedString();
|
|
79
|
-
return { mode: 'contains', value, negated };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (peek() === '/') {
|
|
83
|
-
advance();
|
|
84
|
-
let value = '';
|
|
85
|
-
while (pos < input.length && input[pos] !== '/') {
|
|
86
|
-
value += input[pos];
|
|
87
|
-
pos++;
|
|
88
|
-
}
|
|
89
|
-
if (pos < input.length) pos++;
|
|
90
|
-
while (pos < input.length && /[gimsuy]/.test(input[pos])) pos++;
|
|
91
|
-
return { mode: 'regex', value, negated };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const value = readQuotedString();
|
|
95
|
-
return { mode: 'exact', value, negated };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
while (pos < input.length) {
|
|
99
|
-
skipWhitespace();
|
|
100
|
-
if (pos >= input.length) break;
|
|
101
|
-
|
|
102
|
-
const selector = readIdentifier();
|
|
103
|
-
if (!selector) {
|
|
104
|
-
pos++;
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const segment: QuerySegment = {
|
|
109
|
-
selector: selector as SelectorType,
|
|
110
|
-
index: null,
|
|
111
|
-
slice: null,
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
if (peek() === '(') {
|
|
115
|
-
advance();
|
|
116
|
-
segment.textMatch = parseTextMatcher();
|
|
117
|
-
if (peek() === ')') advance();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
while (peek() === '[') {
|
|
121
|
-
advance();
|
|
122
|
-
const content = readUntilAny(']');
|
|
123
|
-
if (/^-?\d*(:-?\d*)?$/.test(content) && content !== '') {
|
|
124
|
-
if (content.includes(':')) {
|
|
125
|
-
const colonIdx = content.indexOf(':');
|
|
126
|
-
const fromStr = content.slice(0, colonIdx);
|
|
127
|
-
const toStr = content.slice(colonIdx + 1);
|
|
128
|
-
segment.slice = {
|
|
129
|
-
from: fromStr ? Number.parseInt(fromStr, 10) : undefined,
|
|
130
|
-
to: toStr ? Number.parseInt(toStr, 10) : undefined,
|
|
131
|
-
};
|
|
132
|
-
} else {
|
|
133
|
-
segment.index = Number.parseInt(content, 10);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (peek() === ']') advance();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
segments.push(segment);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return segments;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function matchText(text: string, matcher: TextMatcher): boolean {
|
|
146
|
-
let result: boolean;
|
|
147
|
-
|
|
148
|
-
switch (matcher.mode) {
|
|
149
|
-
case 'exact':
|
|
150
|
-
result = text === matcher.value;
|
|
151
|
-
break;
|
|
152
|
-
case 'contains':
|
|
153
|
-
result = text.includes(matcher.value);
|
|
154
|
-
break;
|
|
155
|
-
case 'regex':
|
|
156
|
-
result = new RegExp(matcher.value, 'i').test(text);
|
|
157
|
-
break;
|
|
158
|
-
default:
|
|
159
|
-
result = false;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return matcher.negated ? !result : result;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function entryKey(line: string): string | null {
|
|
166
|
-
const separator = line.indexOf(':');
|
|
167
|
-
if (separator < 1) return null;
|
|
168
|
-
return line.slice(0, separator).trim().toLowerCase();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function getTokenText(token: Token): string {
|
|
172
|
-
const t = token as any;
|
|
173
|
-
switch (token.type) {
|
|
174
|
-
case 'heading':
|
|
175
|
-
case 'paragraph':
|
|
176
|
-
case 'code':
|
|
177
|
-
case 'blockquote':
|
|
178
|
-
case 'list_item':
|
|
179
|
-
return t.text || '';
|
|
180
|
-
case 'table':
|
|
181
|
-
return (t.header || []).map((h: any) => h.text).join(', ');
|
|
182
|
-
default:
|
|
183
|
-
return '';
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function getHeadingDepth(selector: string): number | null {
|
|
188
|
-
const match = selector.match(/^h([1-6])$/);
|
|
189
|
-
return match ? Number.parseInt(match[1], 10) : null;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function isSectionSelector(selector: string): boolean {
|
|
193
|
-
return /^section\d?$/.test(selector);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function getSectionDepth(selector: string): number | null {
|
|
197
|
-
const match = selector.match(/^section([1-6])$/);
|
|
198
|
-
return match ? Number.parseInt(match[1], 10) : null;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function selectorToTokenType(selector: string): string | null {
|
|
202
|
-
if (/^h[1-6]$/.test(selector)) return 'heading';
|
|
203
|
-
const map: Record<string, string> = {
|
|
204
|
-
heading: 'heading',
|
|
205
|
-
paragraph: 'paragraph',
|
|
206
|
-
table: 'table',
|
|
207
|
-
code: 'code',
|
|
208
|
-
list: 'list',
|
|
209
|
-
blockquote: 'blockquote',
|
|
210
|
-
hr: 'hr',
|
|
211
|
-
item: 'list_item',
|
|
212
|
-
};
|
|
213
|
-
return map[selector] || null;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function buildTokenIndex(source: string): MatchedRange[] {
|
|
217
|
-
const tokens = marked.lexer(source);
|
|
218
|
-
const ranges: MatchedRange[] = [];
|
|
219
|
-
let offset = 0;
|
|
220
|
-
|
|
221
|
-
for (const token of tokens) {
|
|
222
|
-
const raw = (token as any).raw || '';
|
|
223
|
-
ranges.push({ token, start: offset, length: raw.length });
|
|
224
|
-
offset += raw.length;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return ranges;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function computeSections(candidates: MatchedRange[], segment: QuerySegment): MatchedRange[] {
|
|
231
|
-
const sectionDepth = getSectionDepth(segment.selector as string);
|
|
232
|
-
const sections: MatchedRange[] = [];
|
|
233
|
-
|
|
234
|
-
for (let i = 0; i < candidates.length; i++) {
|
|
235
|
-
const range = candidates[i];
|
|
236
|
-
if (range.token.type !== 'heading') continue;
|
|
237
|
-
|
|
238
|
-
const heading = range.token as Tokens.Heading;
|
|
239
|
-
if (sectionDepth !== null && heading.depth !== sectionDepth) continue;
|
|
240
|
-
if (segment.textMatch && !matchText(heading.text, segment.textMatch)) continue;
|
|
241
|
-
|
|
242
|
-
const depth = heading.depth;
|
|
243
|
-
const innerTokens: MatchedRange[] = [];
|
|
244
|
-
let endOffset = range.start + range.length;
|
|
245
|
-
|
|
246
|
-
for (let j = i + 1; j < candidates.length; j++) {
|
|
247
|
-
const nextRange = candidates[j];
|
|
248
|
-
if (nextRange.token.type === 'heading' && (nextRange.token as Tokens.Heading).depth <= depth) break;
|
|
249
|
-
innerTokens.push(nextRange);
|
|
250
|
-
endOffset = nextRange.start + nextRange.length;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
sections.push({
|
|
254
|
-
token: range.token,
|
|
255
|
-
start: range.start,
|
|
256
|
-
length: endOffset - range.start,
|
|
257
|
-
innerTokens,
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return sections;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function extractListItems(candidates: MatchedRange[]): MatchedRange[] {
|
|
265
|
-
const items: MatchedRange[] = [];
|
|
266
|
-
|
|
267
|
-
for (const range of candidates) {
|
|
268
|
-
if (range.token.type !== 'list') continue;
|
|
269
|
-
|
|
270
|
-
const list = range.token as Tokens.List;
|
|
271
|
-
const listRaw = (range.token as any).raw as string;
|
|
272
|
-
let searchFrom = 0;
|
|
273
|
-
|
|
274
|
-
for (const item of list.items) {
|
|
275
|
-
const itemRaw = (item as any).raw as string;
|
|
276
|
-
const idx = listRaw.indexOf(itemRaw, searchFrom);
|
|
277
|
-
if (idx === -1) continue;
|
|
278
|
-
|
|
279
|
-
items.push({
|
|
280
|
-
token: item as unknown as Token,
|
|
281
|
-
start: range.start + idx,
|
|
282
|
-
length: itemRaw.length,
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
searchFrom = idx + itemRaw.length;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return items;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function applyIndexSlice(matches: MatchedRange[], segment: QuerySegment): MatchedRange[] {
|
|
293
|
-
if (segment.index !== null) {
|
|
294
|
-
const idx = segment.index < 0 ? matches.length + segment.index : segment.index;
|
|
295
|
-
return idx >= 0 && idx < matches.length ? [matches[idx]] : [];
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (segment.slice) {
|
|
299
|
-
const { from, to } = segment.slice;
|
|
300
|
-
return matches.slice(from, to);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return matches;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
function expandSectionRanges(matches: MatchedRange[]): MatchedRange[] {
|
|
307
|
-
let hasSection = false;
|
|
308
|
-
for (const m of matches) {
|
|
309
|
-
if (m.innerTokens) {
|
|
310
|
-
hasSection = true;
|
|
311
|
-
break;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
if (!hasSection) return matches;
|
|
315
|
-
|
|
316
|
-
const expanded: MatchedRange[] = [];
|
|
317
|
-
for (const m of matches) {
|
|
318
|
-
if (m.innerTokens) {
|
|
319
|
-
expanded.push({ token: m.token, start: m.start, length: ((m.token as any).raw || '').length });
|
|
320
|
-
expanded.push(...m.innerTokens);
|
|
321
|
-
} else {
|
|
322
|
-
expanded.push(m);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return expanded;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
function executeSegments(candidates: MatchedRange[], segments: QuerySegment[]): MatchedRange[] {
|
|
329
|
-
if (segments.length === 0) return candidates;
|
|
330
|
-
|
|
331
|
-
const segment = segments[0];
|
|
332
|
-
const remaining = segments.slice(1);
|
|
333
|
-
|
|
334
|
-
if (isSectionSelector(segment.selector as string)) {
|
|
335
|
-
const sections = computeSections(candidates, segment);
|
|
336
|
-
const indexed = applyIndexSlice(sections, segment);
|
|
337
|
-
|
|
338
|
-
if (remaining.length === 0) return indexed;
|
|
339
|
-
|
|
340
|
-
const results: MatchedRange[] = [];
|
|
341
|
-
for (const section of indexed) {
|
|
342
|
-
results.push(...executeSegments(section.innerTokens || [], remaining));
|
|
343
|
-
}
|
|
344
|
-
return results;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (segment.selector === 'item') {
|
|
348
|
-
let items = extractListItems(candidates);
|
|
349
|
-
if (segment.textMatch) {
|
|
350
|
-
items = items.filter((r) => matchText(getTokenText(r.token), segment.textMatch!));
|
|
351
|
-
}
|
|
352
|
-
return executeSegments(applyIndexSlice(items, segment), remaining);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
const tokenType = selectorToTokenType(segment.selector as string);
|
|
356
|
-
if (!tokenType) return [];
|
|
357
|
-
|
|
358
|
-
let matches = candidates.filter((r) => r.token.type === tokenType);
|
|
359
|
-
|
|
360
|
-
const depth = getHeadingDepth(segment.selector as string);
|
|
361
|
-
if (depth !== null) {
|
|
362
|
-
matches = matches.filter((r) => (r.token as any).depth === depth);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
if (segment.textMatch) {
|
|
366
|
-
matches = matches.filter((r) => matchText(getTokenText(r.token), segment.textMatch!));
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
return executeSegments(applyIndexSlice(matches, segment), remaining);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
export class MarkdownQuery {
|
|
373
|
-
private source: string;
|
|
374
|
-
private matches: MatchedRange[];
|
|
375
|
-
|
|
376
|
-
constructor(source: string, matches?: MatchedRange[]) {
|
|
377
|
-
this.source = source;
|
|
378
|
-
this.matches = matches || buildTokenIndex(source);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
query(selector: string): MarkdownQuery {
|
|
382
|
-
const segments = parseQuery(selector);
|
|
383
|
-
const candidates = expandSectionRanges(this.matches);
|
|
384
|
-
const results = executeSegments(candidates, segments);
|
|
385
|
-
return new MarkdownQuery(this.source, results);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
text(): string {
|
|
389
|
-
return this.matches.map((r) => this.source.slice(r.start, r.start + r.length)).join('');
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
get(): string {
|
|
393
|
-
return this.text();
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
toJson(): Record<string, string>[] {
|
|
397
|
-
const results: Record<string, string>[] = [];
|
|
398
|
-
|
|
399
|
-
for (const range of this.matches) {
|
|
400
|
-
if (range.token.type !== 'table') continue;
|
|
401
|
-
|
|
402
|
-
const table = range.token as Tokens.Table;
|
|
403
|
-
const headers = table.header.map((h) => h.text);
|
|
404
|
-
for (const row of table.rows) {
|
|
405
|
-
const obj: Record<string, string> = {};
|
|
406
|
-
for (let i = 0; i < headers.length; i++) {
|
|
407
|
-
obj[headers[i]] = row[i]?.text ?? '';
|
|
408
|
-
}
|
|
409
|
-
results.push(obj);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return results;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
keyValue(): Record<string, string> {
|
|
417
|
-
const entries: Record<string, string> = {};
|
|
418
|
-
|
|
419
|
-
for (const range of this.matches) {
|
|
420
|
-
for (const line of getTokenText(range.token).split('\n')) {
|
|
421
|
-
const key = entryKey(line);
|
|
422
|
-
if (!key) continue;
|
|
423
|
-
const value = line.slice(line.indexOf(':') + 1).trim();
|
|
424
|
-
if (value) entries[key] = value;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
return entries;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
setKeyValue(key: string, value: string | null): string {
|
|
432
|
-
return this.replaceEach((match) => {
|
|
433
|
-
const token = match.matches[0].token;
|
|
434
|
-
const lines = getTokenText(token)
|
|
435
|
-
.split('\n')
|
|
436
|
-
.map((line) => line.trim())
|
|
437
|
-
.filter(Boolean);
|
|
438
|
-
|
|
439
|
-
const index = lines.findIndex((line) => entryKey(line) === key.toLowerCase());
|
|
440
|
-
if (index < 0 && value) lines.push(`${key}: ${value}`);
|
|
441
|
-
if (index >= 0 && value) lines[index] = `${key}: ${value}`;
|
|
442
|
-
if (index >= 0 && !value) lines.splice(index, 1);
|
|
443
|
-
|
|
444
|
-
if (token.type !== 'blockquote') return lines.join('\n');
|
|
445
|
-
return lines.map((line) => `> ${line}`).join('\n');
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
replace(content: string): string {
|
|
450
|
-
return this.replaceEach(() => content);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
replaceEach(replacer: (match: MarkdownQuery, index: number) => string): string {
|
|
454
|
-
const sorted = [...this.matches].sort((a, b) => a.start - b.start);
|
|
455
|
-
|
|
456
|
-
const kept: MatchedRange[] = [];
|
|
457
|
-
let lastEnd = -1;
|
|
458
|
-
for (const range of sorted) {
|
|
459
|
-
if (range.start < lastEnd) continue;
|
|
460
|
-
kept.push(range);
|
|
461
|
-
lastEnd = range.start + range.length;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const replacements = kept.map((range, index) => replacer(new MarkdownQuery(this.source, [range]), index));
|
|
465
|
-
let result = this.source;
|
|
466
|
-
for (let i = kept.length - 1; i >= 0; i--) {
|
|
467
|
-
const range = kept[i];
|
|
468
|
-
result = result.slice(0, range.start) + replacements[i] + result.slice(range.start + range.length);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
return result;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
count(): number {
|
|
475
|
-
return this.matches.length;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
first(): MarkdownQuery {
|
|
479
|
-
return new MarkdownQuery(this.source, this.matches.slice(0, 1));
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
last(): MarkdownQuery {
|
|
483
|
-
return new MarkdownQuery(this.source, this.matches.slice(-1));
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
before(): MarkdownQuery {
|
|
487
|
-
if (this.matches.length === 0) return new MarkdownQuery(this.source, []);
|
|
488
|
-
const cutoff = this.matches[0].start;
|
|
489
|
-
const allTokens = buildTokenIndex(this.source);
|
|
490
|
-
const beforeTokens = allTokens.filter((r) => r.start + r.length <= cutoff);
|
|
491
|
-
return new MarkdownQuery(this.source, beforeTokens);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
after(): MarkdownQuery {
|
|
495
|
-
if (this.matches.length === 0) return new MarkdownQuery(this.source, []);
|
|
496
|
-
const lastMatch = this.matches[this.matches.length - 1];
|
|
497
|
-
const cutoff = lastMatch.start + lastMatch.length;
|
|
498
|
-
const allTokens = buildTokenIndex(this.source);
|
|
499
|
-
const afterTokens = allTokens.filter((r) => r.start >= cutoff);
|
|
500
|
-
return new MarkdownQuery(this.source, afterTokens);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
each(): MarkdownQuery[] {
|
|
504
|
-
return this.matches.map((m) => new MarkdownQuery(this.source, [m]));
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
meta(): Array<{ type: string; depth: number | null; text: string }> {
|
|
508
|
-
return this.matches.map((range) => {
|
|
509
|
-
const token = range.token as any;
|
|
510
|
-
let depth: number | null = null;
|
|
511
|
-
if (token.type === 'heading') depth = token.depth;
|
|
512
|
-
return { type: token.type, depth, text: getTokenText(range.token) };
|
|
513
|
-
});
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
export function mdq(source: string): MarkdownQuery {
|
|
518
|
-
return new MarkdownQuery(source);
|
|
519
|
-
}
|
|
1
|
+
export * from './mdq/query.ts';
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { type MarkdownQuery, MdqError, mdq } from './query.ts';
|
|
4
|
+
|
|
5
|
+
const EDIT_FLAGS = ['remove', 'replace', 'insertBefore', 'insertAfter', 'prepend', 'append', 'addRow', 'addItem', 'set'] as const;
|
|
6
|
+
|
|
7
|
+
export async function runMdq(argv: string[], readStdin: StdinReader): Promise<CliResult> {
|
|
8
|
+
const program = new Command();
|
|
9
|
+
program
|
|
10
|
+
.name('mdq')
|
|
11
|
+
.description('query and edit markdown')
|
|
12
|
+
.argument('[selector]', 'markdown selector')
|
|
13
|
+
.argument('[file]', 'file to read; stdin when omitted')
|
|
14
|
+
.option('-j, --json', 'output rows as JSON')
|
|
15
|
+
.option('-c, --count', 'print the number of matches')
|
|
16
|
+
.option('-t, --text', 'print unwrapped text')
|
|
17
|
+
.option('--frontmatter', 'print frontmatter as JSON')
|
|
18
|
+
.option('-i, --in-place', 'write the result back to the file')
|
|
19
|
+
.option('--remove', 'delete matched blocks')
|
|
20
|
+
.option('--replace <markdown>', 'replace matched blocks')
|
|
21
|
+
.option('--insert-before <markdown>', 'insert before each match')
|
|
22
|
+
.option('--insert-after <markdown>', 'insert after each match')
|
|
23
|
+
.option('--prepend <markdown>', 'insert at the start of each match')
|
|
24
|
+
.option('--append <markdown>', 'insert at the end of each match')
|
|
25
|
+
.option('--add-row <json>', 'append a table row')
|
|
26
|
+
.option('--add-item <text>', 'append a list item')
|
|
27
|
+
.option('--set <key=value>', 'set an entry; omit the value to delete it')
|
|
28
|
+
.exitOverride()
|
|
29
|
+
.configureOutput({ writeOut: () => {}, writeErr: () => {} });
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
program.parse(argv, { from: 'user' });
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return { output: String((error as Error).message), code: 2 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const options = program.opts();
|
|
38
|
+
let [selector, file] = program.args;
|
|
39
|
+
if (options.frontmatter && selector && !file) {
|
|
40
|
+
file = selector;
|
|
41
|
+
selector = '';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let source = '';
|
|
45
|
+
if (!file) source = await readStdin();
|
|
46
|
+
if (file) {
|
|
47
|
+
try {
|
|
48
|
+
source = readFileSync(file, 'utf8');
|
|
49
|
+
} catch {
|
|
50
|
+
return { output: `Cannot read ${file}`, code: 2 };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
return await apply(mdq(source), selector, options, file);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error instanceof MdqError) return { output: error.message, code: 2 };
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function apply(doc: MarkdownQuery, selector: string, options: Record<string, any>, file?: string): Promise<CliResult> {
|
|
63
|
+
if (options.frontmatter) return { output: `${JSON.stringify(doc.frontmatter(), null, 2)}\n`, code: 0 };
|
|
64
|
+
if (!selector) return { output: 'A selector is required', code: 2 };
|
|
65
|
+
|
|
66
|
+
const chosen = EDIT_FLAGS.filter((flag) => options[flag] !== undefined);
|
|
67
|
+
if (chosen.length > 1) return { output: 'Only one edit at a time', code: 2 };
|
|
68
|
+
|
|
69
|
+
const selection = doc.query(selector);
|
|
70
|
+
if (chosen.length === 0) return read(selection, options);
|
|
71
|
+
|
|
72
|
+
if (!selection.exists()) return finish(String(doc), 1, options, file);
|
|
73
|
+
return finish(String(edit(selection, chosen[0], options)), 0, options, file);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function read(selection: MarkdownQuery, options: Record<string, any>): CliResult {
|
|
77
|
+
let code = 1;
|
|
78
|
+
if (selection.exists()) code = 0;
|
|
79
|
+
if (options.count) return { output: `${selection.count()}\n`, code: 0 };
|
|
80
|
+
if (options.json) return { output: `${JSON.stringify(selection.rows(), null, 2)}\n`, code };
|
|
81
|
+
if (options.text)
|
|
82
|
+
return {
|
|
83
|
+
output: selection
|
|
84
|
+
.nodes()
|
|
85
|
+
.map((node) => node.text)
|
|
86
|
+
.join('\n\n'),
|
|
87
|
+
code,
|
|
88
|
+
};
|
|
89
|
+
return { output: selection.text(), code };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function edit(selection: MarkdownQuery, flag: string, options: Record<string, any>): MarkdownQuery {
|
|
93
|
+
if (flag === 'remove') return selection.remove();
|
|
94
|
+
if (flag === 'replace') return selection.replace(options.replace);
|
|
95
|
+
if (flag === 'insertBefore') return selection.insertBefore(options.insertBefore);
|
|
96
|
+
if (flag === 'insertAfter') return selection.insertAfter(options.insertAfter);
|
|
97
|
+
if (flag === 'prepend') return selection.prepend(options.prepend);
|
|
98
|
+
if (flag === 'append') return selection.append(options.append);
|
|
99
|
+
if (flag === 'addItem') return selection.addItem(options.addItem);
|
|
100
|
+
if (flag === 'addRow') return selection.addRow(JSON.parse(options.addRow));
|
|
101
|
+
const separator = options.set.indexOf('=');
|
|
102
|
+
if (separator < 0) return selection.setEntry(options.set, null);
|
|
103
|
+
return selection.setEntry(options.set.slice(0, separator), options.set.slice(separator + 1) || null);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function finish(output: string, code: number, options: Record<string, any>, file?: string): CliResult {
|
|
107
|
+
if (!options.inPlace) return { output, code };
|
|
108
|
+
if (!file) return { output: '--in-place needs a file', code: 2 };
|
|
109
|
+
writeFileSync(file, output);
|
|
110
|
+
return { output: '', code };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type StdinReader = () => Promise<string>;
|
|
114
|
+
|
|
115
|
+
export interface CliResult {
|
|
116
|
+
output: string;
|
|
117
|
+
code: number;
|
|
118
|
+
}
|