@rohal12/spindle 0.64.32 → 0.64.33
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +42 -13
- package/dist/pkg/story-variables.js +79 -29
- package/package.json +1 -1
- package/src/code-check.ts +1 -0
- package/src/format/format-passage.ts +9 -0
- package/src/hooks/use-modal-focus.ts +23 -10
- package/src/js-bindings.ts +41 -23
- package/src/markup/ast.ts +6 -1
- package/src/markup/pair.ts +10 -2
- package/src/markup/parse.ts +40 -1
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +5 -6
- package/src/markup/validate.ts +3 -0
- package/src/tooling.ts +1 -0
- package/types/tooling.d.ts +2 -0
package/src/markup/parse.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface MarkupHooks extends CodeEnd {
|
|
|
24
24
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
25
25
|
isBlock(name: string): boolean;
|
|
26
26
|
branchParents?(name: string): readonly string[];
|
|
27
|
+
isStandalone?(name: string): boolean;
|
|
27
28
|
/** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
|
|
28
29
|
isRaw(name: string): boolean;
|
|
29
30
|
/** Whether an attribute's value is code (`onclick`), not markup. */
|
|
@@ -93,6 +94,7 @@ function run(
|
|
|
93
94
|
startRule: 'Tokens' | 'SelectorsPrefix',
|
|
94
95
|
options: ParseMarkupOptions,
|
|
95
96
|
completion = false,
|
|
97
|
+
findDelimiter = indexDelimiters(source),
|
|
96
98
|
): unknown {
|
|
97
99
|
const hooks = { ...defaultHooks, ...options.hooks };
|
|
98
100
|
if (!options.hooks?.closeBrace) {
|
|
@@ -106,6 +108,7 @@ function run(
|
|
|
106
108
|
text: options.text === true,
|
|
107
109
|
hooks,
|
|
108
110
|
completion,
|
|
111
|
+
findDelimiter,
|
|
109
112
|
});
|
|
110
113
|
} catch (err) {
|
|
111
114
|
const { location, code } = err as {
|
|
@@ -128,6 +131,27 @@ function run(
|
|
|
128
131
|
}
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
/** Literal delimiter searches shared by suffix and prefix recovery attempts. */
|
|
135
|
+
function indexDelimiters(source: string) {
|
|
136
|
+
const positions = new Map<string, number[]>();
|
|
137
|
+
return (delimiter: string, from: number, end = source.length): number => {
|
|
138
|
+
let offsets = positions.get(delimiter);
|
|
139
|
+
if (!offsets) {
|
|
140
|
+
offsets = [];
|
|
141
|
+
for (
|
|
142
|
+
let at = source.indexOf(delimiter);
|
|
143
|
+
at >= 0;
|
|
144
|
+
at = source.indexOf(delimiter, at + 1)
|
|
145
|
+
) {
|
|
146
|
+
offsets.push(at);
|
|
147
|
+
}
|
|
148
|
+
positions.set(delimiter, offsets);
|
|
149
|
+
}
|
|
150
|
+
const at = offsets[upperBound(offsets, from - 1)];
|
|
151
|
+
return at !== undefined && at + delimiter.length <= end ? at : -1;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
131
155
|
/** What tolerant parsing makes of markup (see analyzeMarkup). */
|
|
132
156
|
export interface MarkupAnalysis {
|
|
133
157
|
tokens: Token[];
|
|
@@ -156,6 +180,7 @@ export function analyzeMarkup(
|
|
|
156
180
|
isBlock: hooks.isBlock,
|
|
157
181
|
isRaw: hooks.isRaw,
|
|
158
182
|
branchParents: hooks.branchParents,
|
|
183
|
+
isStandalone: hooks.isStandalone,
|
|
159
184
|
source,
|
|
160
185
|
});
|
|
161
186
|
if (errors.length === 0 && paired.errors.length === 0) {
|
|
@@ -289,6 +314,7 @@ export function scanMarkupTolerant(
|
|
|
289
314
|
// Share the index across recovery attempts, whose inputs are suffixes or
|
|
290
315
|
// truncated prefixes. Re-indexing each attempt would itself be quadratic.
|
|
291
316
|
const codeEnd = indexCodeEnds(source);
|
|
317
|
+
const findDelimiter = indexDelimiters(source);
|
|
292
318
|
const customCloseBrace = options.hooks?.closeBrace;
|
|
293
319
|
const lastBrace = source.lastIndexOf('}');
|
|
294
320
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
@@ -309,7 +335,20 @@ export function scanMarkupTolerant(
|
|
|
309
335
|
};
|
|
310
336
|
try {
|
|
311
337
|
return (
|
|
312
|
-
run(
|
|
338
|
+
run(
|
|
339
|
+
text,
|
|
340
|
+
'Tokens',
|
|
341
|
+
{ ...options, hooks },
|
|
342
|
+
completion,
|
|
343
|
+
(delimiter, from) => {
|
|
344
|
+
const at = findDelimiter(
|
|
345
|
+
delimiter,
|
|
346
|
+
base + from,
|
|
347
|
+
base + text.length,
|
|
348
|
+
);
|
|
349
|
+
return at < 0 ? -1 : at - base;
|
|
350
|
+
},
|
|
351
|
+
) as Token[]
|
|
313
352
|
).map((token) => shiftOffsets(token, base));
|
|
314
353
|
} catch (error) {
|
|
315
354
|
if (!(error instanceof MarkupError)) throw error;
|
|
@@ -20,6 +20,8 @@ export interface ParseOptions {
|
|
|
20
20
|
text: boolean;
|
|
21
21
|
/** What the grammar leaves to code (see parse.ts MarkupHooks). */
|
|
22
22
|
hooks: unknown;
|
|
23
|
+
/** Indexed literal search, relative to this parse attempt. */
|
|
24
|
+
findDelimiter: (delimiter: string, from: number) => number;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -133,9 +133,8 @@ const textMode = options.text === true;
|
|
|
133
133
|
// Only cursor-context parsing accepts EOF in place of an unfinished closer.
|
|
134
134
|
const completionMode = options.completion === true;
|
|
135
135
|
const hooks = options.hooks;
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
const lastCommentClose = input.lastIndexOf('-->');
|
|
136
|
+
// Indexed by parse.ts across recovery suffixes and truncated prefixes.
|
|
137
|
+
const findDelimiter = options.findDelimiter;
|
|
139
138
|
/** End of the stretch a `.|n|` repetition consumes (see skipTo). */
|
|
140
139
|
let skipEnd = -1;
|
|
141
140
|
/** Whether the attribute whose value is being read holds code. */
|
|
@@ -252,7 +251,7 @@ TextRun
|
|
|
252
251
|
* rendering drops it. (An unclosed `<!--` is ordinary text.)
|
|
253
252
|
*/
|
|
254
253
|
Comment
|
|
255
|
-
= value:$("<!--" (">" / "->" / &{ return completionMode || offset()
|
|
254
|
+
= value:$("<!--" (">" / "->" / &{ return completionMode || findDelimiter('-->', offset()) >= 0; } (!"-->" .)* ("-->" / OpenEnd))) {
|
|
256
255
|
const node = lexeme('text', location(), { value });
|
|
257
256
|
node.comment = true;
|
|
258
257
|
return node;
|
|
@@ -303,8 +302,8 @@ LinkLexeme
|
|
|
303
302
|
*/
|
|
304
303
|
LinkText
|
|
305
304
|
= start:Here &{
|
|
306
|
-
const close =
|
|
307
|
-
const nested =
|
|
305
|
+
const close = findDelimiter(']]', start);
|
|
306
|
+
const nested = findDelimiter('[[', start);
|
|
308
307
|
linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
|
|
309
308
|
return true;
|
|
310
309
|
}
|
package/src/markup/validate.ts
CHANGED
|
@@ -220,6 +220,9 @@ export function validateMarkup(
|
|
|
220
220
|
ReturnType<typeof sourcePositions>
|
|
221
221
|
>();
|
|
222
222
|
const hooks = {
|
|
223
|
+
...(options.parametersOf?.pairing?.isStandalone && {
|
|
224
|
+
isStandalone: options.parametersOf.pairing.isStandalone,
|
|
225
|
+
}),
|
|
223
226
|
...(options.isBlockMacro && { isBlock: options.isBlockMacro }),
|
|
224
227
|
...(options.branchParents && { branchParents: options.branchParents }),
|
|
225
228
|
isRaw: (name: string) =>
|
package/src/tooling.ts
CHANGED
|
@@ -249,6 +249,7 @@ function tokenizationOptions(
|
|
|
249
249
|
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
250
250
|
isBlock: blockLookup(macros),
|
|
251
251
|
branchParents: branchParentLookup(macros),
|
|
252
|
+
isStandalone: parametersOf.pairing!.isStandalone,
|
|
252
253
|
...options.hooks,
|
|
253
254
|
},
|
|
254
255
|
};
|
package/types/tooling.d.ts
CHANGED
|
@@ -1071,6 +1071,8 @@ export interface PairMarkupOptions {
|
|
|
1071
1071
|
isRaw?(name: string): boolean;
|
|
1072
1072
|
/** Parents declaring a branching sub-macro (default: registered metadata). */
|
|
1073
1073
|
branchParents?(name: string): readonly string[];
|
|
1074
|
+
/** Whether a companion name also has a standalone macro/widget implementation. */
|
|
1075
|
+
isStandalone?(name: string): boolean;
|
|
1074
1076
|
/**
|
|
1075
1077
|
* The markup the tokens are of: lets a message say at which line and column
|
|
1076
1078
|
* an element was opened (else it says at which offset).
|