@rohal12/spindle 0.32.1 → 0.34.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.
@@ -0,0 +1,282 @@
1
+ [
2
+ {
3
+ "name": "set",
4
+ "block": false,
5
+ "subMacros": [],
6
+ "source": "builtin"
7
+ },
8
+ {
9
+ "name": "print",
10
+ "block": false,
11
+ "subMacros": [],
12
+ "interpolate": true,
13
+ "merged": true,
14
+ "source": "builtin"
15
+ },
16
+ {
17
+ "name": "computed",
18
+ "block": false,
19
+ "subMacros": [],
20
+ "merged": true,
21
+ "source": "builtin"
22
+ },
23
+ {
24
+ "name": "meter",
25
+ "block": false,
26
+ "subMacros": [],
27
+ "interpolate": true,
28
+ "merged": true,
29
+ "source": "builtin"
30
+ },
31
+ {
32
+ "name": "if",
33
+ "block": true,
34
+ "subMacros": [],
35
+ "interpolate": true,
36
+ "merged": true,
37
+ "source": "builtin"
38
+ },
39
+ {
40
+ "name": "for",
41
+ "block": true,
42
+ "subMacros": [],
43
+ "interpolate": true,
44
+ "merged": true,
45
+ "source": "builtin"
46
+ },
47
+ {
48
+ "name": "do",
49
+ "block": true,
50
+ "subMacros": [],
51
+ "source": "builtin"
52
+ },
53
+ {
54
+ "name": "button",
55
+ "block": true,
56
+ "subMacros": [],
57
+ "interpolate": true,
58
+ "source": "builtin"
59
+ },
60
+ {
61
+ "name": "link",
62
+ "block": true,
63
+ "subMacros": [],
64
+ "interpolate": true,
65
+ "source": "builtin"
66
+ },
67
+ {
68
+ "name": "switch",
69
+ "block": true,
70
+ "subMacros": [
71
+ "case",
72
+ "default"
73
+ ],
74
+ "merged": true,
75
+ "source": "builtin"
76
+ },
77
+ {
78
+ "name": "timed",
79
+ "block": true,
80
+ "subMacros": [
81
+ "next"
82
+ ],
83
+ "interpolate": true,
84
+ "source": "builtin"
85
+ },
86
+ {
87
+ "name": "repeat",
88
+ "block": true,
89
+ "subMacros": [],
90
+ "interpolate": true,
91
+ "source": "builtin"
92
+ },
93
+ {
94
+ "name": "stop",
95
+ "block": false,
96
+ "subMacros": [],
97
+ "source": "builtin"
98
+ },
99
+ {
100
+ "name": "type",
101
+ "block": true,
102
+ "subMacros": [],
103
+ "interpolate": true,
104
+ "source": "builtin"
105
+ },
106
+ {
107
+ "name": "widget",
108
+ "block": true,
109
+ "subMacros": [],
110
+ "source": "builtin"
111
+ },
112
+ {
113
+ "name": "story-title",
114
+ "block": false,
115
+ "subMacros": [],
116
+ "source": "builtin"
117
+ },
118
+ {
119
+ "name": "back",
120
+ "block": false,
121
+ "subMacros": [],
122
+ "source": "builtin"
123
+ },
124
+ {
125
+ "name": "forward",
126
+ "block": false,
127
+ "subMacros": [],
128
+ "source": "builtin"
129
+ },
130
+ {
131
+ "name": "quicksave",
132
+ "block": false,
133
+ "subMacros": [],
134
+ "source": "builtin"
135
+ },
136
+ {
137
+ "name": "quickload",
138
+ "block": false,
139
+ "subMacros": [],
140
+ "source": "builtin"
141
+ },
142
+ {
143
+ "name": "restart",
144
+ "block": false,
145
+ "subMacros": [],
146
+ "source": "builtin"
147
+ },
148
+ {
149
+ "name": "saves",
150
+ "block": false,
151
+ "subMacros": [],
152
+ "source": "builtin"
153
+ },
154
+ {
155
+ "name": "settings",
156
+ "block": false,
157
+ "subMacros": [],
158
+ "source": "builtin"
159
+ },
160
+ {
161
+ "name": "dialog",
162
+ "block": true,
163
+ "subMacros": [],
164
+ "interpolate": true,
165
+ "source": "builtin"
166
+ },
167
+ {
168
+ "name": "save-manager",
169
+ "block": false,
170
+ "subMacros": [],
171
+ "source": "builtin"
172
+ },
173
+ {
174
+ "name": "settings-controls",
175
+ "block": false,
176
+ "subMacros": [],
177
+ "source": "builtin"
178
+ },
179
+ {
180
+ "name": "include",
181
+ "block": false,
182
+ "subMacros": [],
183
+ "interpolate": true,
184
+ "merged": true,
185
+ "source": "builtin"
186
+ },
187
+ {
188
+ "name": "nobr",
189
+ "block": true,
190
+ "subMacros": [],
191
+ "source": "builtin"
192
+ },
193
+ {
194
+ "name": "goto",
195
+ "block": false,
196
+ "subMacros": [],
197
+ "merged": true,
198
+ "source": "builtin"
199
+ },
200
+ {
201
+ "name": "unset",
202
+ "block": false,
203
+ "subMacros": [],
204
+ "source": "builtin"
205
+ },
206
+ {
207
+ "name": "textbox",
208
+ "block": false,
209
+ "subMacros": [],
210
+ "storeVar": true,
211
+ "source": "builtin"
212
+ },
213
+ {
214
+ "name": "numberbox",
215
+ "block": false,
216
+ "subMacros": [],
217
+ "storeVar": true,
218
+ "source": "builtin"
219
+ },
220
+ {
221
+ "name": "textarea",
222
+ "block": false,
223
+ "subMacros": [],
224
+ "storeVar": true,
225
+ "source": "builtin"
226
+ },
227
+ {
228
+ "name": "checkbox",
229
+ "block": false,
230
+ "subMacros": [],
231
+ "storeVar": true,
232
+ "source": "builtin"
233
+ },
234
+ {
235
+ "name": "radiobutton",
236
+ "block": false,
237
+ "subMacros": [],
238
+ "storeVar": true,
239
+ "source": "builtin"
240
+ },
241
+ {
242
+ "name": "listbox",
243
+ "block": true,
244
+ "subMacros": [
245
+ "option"
246
+ ],
247
+ "storeVar": true,
248
+ "source": "builtin"
249
+ },
250
+ {
251
+ "name": "cycle",
252
+ "block": true,
253
+ "subMacros": [],
254
+ "storeVar": true,
255
+ "source": "builtin"
256
+ },
257
+ {
258
+ "name": "span",
259
+ "block": true,
260
+ "subMacros": [],
261
+ "source": "builtin"
262
+ },
263
+ {
264
+ "name": "passage",
265
+ "block": false,
266
+ "subMacros": [],
267
+ "interpolate": true,
268
+ "source": "builtin"
269
+ },
270
+ {
271
+ "name": "watch",
272
+ "block": false,
273
+ "subMacros": [],
274
+ "source": "builtin"
275
+ },
276
+ {
277
+ "name": "unwatch",
278
+ "block": false,
279
+ "subMacros": [],
280
+ "source": "builtin"
281
+ }
282
+ ]
@@ -0,0 +1,44 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const __dirname = dirname(fileURLToPath(import.meta.url));
6
+
7
+ // Load built-in macro metadata generated at build time
8
+ const registryPath = join(__dirname, 'macro-registry.json');
9
+ let builtins = [];
10
+ try {
11
+ builtins = JSON.parse(readFileSync(registryPath, 'utf-8'));
12
+ } catch {
13
+ // macro-registry.json not yet built — empty builtins
14
+ }
15
+
16
+ const metadata = new Map();
17
+ for (const m of builtins) metadata.set(m.name.toLowerCase(), m);
18
+
19
+ /**
20
+ * Metadata-only defineMacro for tooling.
21
+ * Captures macro metadata without creating Preact components.
22
+ * LSP servers call this to register user-defined macros discovered in story scripts.
23
+ */
24
+ export function defineMacro(config) {
25
+ const name = config.name.toLowerCase();
26
+ metadata.set(name, {
27
+ name: config.name,
28
+ block: config.block ?? (config.subMacros?.length > 0 ? true : false),
29
+ subMacros: config.subMacros ?? [],
30
+ storeVar: config.storeVar,
31
+ interpolate: config.interpolate,
32
+ merged: config.merged,
33
+ description: config.description,
34
+ parameters: config.parameters,
35
+ source: 'user',
36
+ });
37
+ }
38
+
39
+ /**
40
+ * Return metadata for all registered macros (built-in + user-defined).
41
+ */
42
+ export function getMacroRegistry() {
43
+ return Array.from(metadata.values());
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.32.1",
3
+ "version": "0.34.0",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -25,16 +25,22 @@
25
25
  ".": {
26
26
  "types": "./types/index.d.ts",
27
27
  "import": "./dist/pkg/index.js"
28
+ },
29
+ "./tooling": {
30
+ "types": "./types/tooling.d.ts",
31
+ "import": "./dist/pkg/tooling.js"
28
32
  }
29
33
  },
30
34
  "files": [
31
35
  "dist/pkg/index.js",
32
36
  "dist/pkg/format.js",
37
+ "dist/pkg/tooling.js",
38
+ "dist/pkg/macro-registry.json",
33
39
  "types",
34
40
  "src"
35
41
  ],
36
42
  "scripts": {
37
- "build": "vite build && bun run scripts/build-format.ts",
43
+ "build": "vite build && bun run scripts/build-format.ts && bun run scripts/dump-macro-registry.ts",
38
44
  "compile": "bun run scripts/compile-story.ts",
39
45
  "preview": "bun run build && bun run compile",
40
46
  "test": "vitest run",
@@ -10,6 +10,7 @@ import {
10
10
 
11
11
  defineMacro({
12
12
  name: 'button',
13
+ block: true,
13
14
  interpolate: true,
14
15
  render({ rawArgs, children = [] }, ctx) {
15
16
  const label = ctx.resolve?.(rawArgs.replace(/^["']|["']$/g, '')) ?? rawArgs;
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'cycle',
5
+ block: true,
5
6
  storeVar: true,
6
7
  render({ children = [] }, ctx) {
7
8
  const options = ctx.extractOptions(children);
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'do',
5
+ block: true,
5
6
  render({ children = [] }, ctx) {
6
7
  const code = ctx.collectText(children);
7
8
 
@@ -91,6 +91,7 @@ function ForIteration({
91
91
 
92
92
  defineMacro({
93
93
  name: 'for',
94
+ block: true,
94
95
  interpolate: true,
95
96
  merged: true,
96
97
  render({ rawArgs, children = [] }, ctx) {
@@ -4,6 +4,7 @@ import type { Branch } from '../../markup/ast';
4
4
 
5
5
  defineMacro({
6
6
  name: 'if',
7
+ block: true,
7
8
  interpolate: true,
8
9
  merged: true,
9
10
  render({ branches = [] }, ctx) {
@@ -56,6 +56,7 @@ function renderChildrenDetached(
56
56
 
57
57
  defineMacro({
58
58
  name: 'link',
59
+ block: true,
59
60
  interpolate: true,
60
61
  render({ rawArgs, children = [] }, ctx) {
61
62
  const { display, passage } = parseArgs(rawArgs);
@@ -3,6 +3,7 @@ import { NobrContext } from '../../markup/render';
3
3
 
4
4
  defineMacro({
5
5
  name: 'nobr',
6
+ block: true,
6
7
  render({ children = [] }, ctx) {
7
8
  return ctx.wrap(
8
9
  ctx.h(
@@ -8,6 +8,7 @@ export const RepeatContext = createContext<{ stop: () => void }>({
8
8
 
9
9
  defineMacro({
10
10
  name: 'repeat',
11
+ block: true,
11
12
  interpolate: true,
12
13
  render({ rawArgs, children = [] }, ctx) {
13
14
  const { useState, useEffect, useCallback } = ctx.hooks;
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'span',
5
+ block: true,
5
6
  render({ children = [] }, ctx) {
6
7
  return ctx.wrap(ctx.renderNodes(children));
7
8
  },
@@ -3,6 +3,7 @@ import { defineMacro } from '../../define-macro';
3
3
 
4
4
  defineMacro({
5
5
  name: 'type',
6
+ block: true,
6
7
  interpolate: true,
7
8
  render({ rawArgs, children = [] }, ctx) {
8
9
  const { useState, useEffect, useRef } = ctx.hooks;
@@ -10,6 +10,7 @@ function parseWidgetDef(rawArgs: string): { name: string; params: string[] } {
10
10
 
11
11
  defineMacro({
12
12
  name: 'widget',
13
+ block: true,
13
14
  render({ rawArgs, children = [] }, ctx) {
14
15
  const { name, params } = parseWidgetDef(rawArgs);
15
16
 
@@ -24,13 +24,53 @@ interface WidgetInvocationProps {
24
24
  }
25
25
 
26
26
  /**
27
- * Split rawArgs by commas, respecting parentheses, brackets, braces, and strings.
27
+ * Try to split a raw string into adjacent quoted string literals separated by
28
+ * whitespace. Returns the individual quoted strings if the entire input is
29
+ * consumed, or `null` if any non-quote/non-whitespace content is found.
28
30
  */
29
- function splitArgs(raw: string): string[] {
31
+ function trySplitAdjacentQuotes(raw: string): string[] | null {
32
+ const args: string[] = [];
33
+ let i = 0;
34
+
35
+ while (i < raw.length) {
36
+ // skip whitespace between quoted strings
37
+ while (i < raw.length && /\s/.test(raw[i]!)) i++;
38
+ if (i >= raw.length) break;
39
+
40
+ const quote = raw[i]!;
41
+ if (quote !== '"' && quote !== "'") return null;
42
+
43
+ // consume the quoted string
44
+ let str = quote;
45
+ i++;
46
+ while (i < raw.length) {
47
+ const ch = raw[i]!;
48
+ str += ch;
49
+ i++;
50
+ if (ch === quote && raw[i - 2] !== '\\') break;
51
+ }
52
+
53
+ // must be properly closed
54
+ if (str.length < 2 || str[str.length - 1] !== quote) return null;
55
+
56
+ args.push(str);
57
+ }
58
+
59
+ // only use this path when there are 2+ adjacent quoted strings
60
+ return args.length > 1 ? args : null;
61
+ }
62
+
63
+ /**
64
+ * Split rawArgs by commas, respecting parentheses, brackets, braces, and
65
+ * strings. When no top-level commas are present, also supports adjacent quoted
66
+ * string literals separated by whitespace (e.g. `"Label" "target"`).
67
+ */
68
+ export function splitArgs(raw: string): string[] {
30
69
  const args: string[] = [];
31
70
  let current = '';
32
71
  let depth = 0;
33
72
  let inString: string | null = null;
73
+ let hasComma = false;
34
74
 
35
75
  for (let i = 0; i < raw.length; i++) {
36
76
  const ch = raw[i]!;
@@ -60,6 +100,7 @@ function splitArgs(raw: string): string[] {
60
100
  }
61
101
 
62
102
  if (ch === ',' && depth === 0) {
103
+ hasComma = true;
63
104
  args.push(current.trim());
64
105
  current = '';
65
106
  continue;
@@ -70,6 +111,14 @@ function splitArgs(raw: string): string[] {
70
111
 
71
112
  const last = current.trim();
72
113
  if (last) args.push(last);
114
+
115
+ // If no commas were found and we got a single expression, try splitting
116
+ // it as adjacent quoted string literals (e.g. "Label" "target").
117
+ if (!hasComma && args.length === 1) {
118
+ const quoted = trySplitAdjacentQuotes(args[0]!);
119
+ if (quoted) return quoted;
120
+ }
121
+
73
122
  return args;
74
123
  }
75
124
 
@@ -27,8 +27,12 @@ import type { UseActionOptions } from './hooks/use-action';
27
27
  import { collectText } from './utils/extract-text';
28
28
  import { currentSourceLocation } from './utils/source-location';
29
29
  import { parseVarArgs, extractOptions } from './components/macros/option-utils';
30
- import { registerMacro, registerSubMacro } from './registry';
31
- import type { MacroProps } from './registry';
30
+ import {
31
+ registerMacro,
32
+ registerSubMacro,
33
+ registerMacroMetadata,
34
+ } from './registry';
35
+ import type { MacroProps, ParameterDef } from './registry';
32
36
  import { registerBlockMacro } from './markup/ast';
33
37
 
34
38
  export function macroClass(type: string, className?: string): string {
@@ -81,6 +85,8 @@ export interface MacroDefinition {
81
85
  interpolate?: boolean;
82
86
  merged?: boolean;
83
87
  storeVar?: boolean;
88
+ description?: string;
89
+ parameters?: ParameterDef[];
84
90
  render: (props: MacroProps, ctx: MacroContext) => VNode | null;
85
91
  }
86
92
 
@@ -121,7 +127,10 @@ function setByPath(
121
127
  target[segments[segments.length - 1]!] = value;
122
128
  }
123
129
 
124
- export function defineMacro(config: MacroDefinition): void {
130
+ export function defineMacro(
131
+ config: MacroDefinition,
132
+ source: 'builtin' | 'user' = 'builtin',
133
+ ): void {
125
134
  function Wrapper(props: MacroProps) {
126
135
  // className/id resolved first (interpolate may transform them)
127
136
  let className = props.className;
@@ -196,13 +205,27 @@ export function defineMacro(config: MacroDefinition): void {
196
205
  }
197
206
 
198
207
  registerMacro(config.name, Wrapper);
208
+
209
+ // Store metadata for tooling API
210
+ const isBlock =
211
+ config.block === true ||
212
+ (config.block !== false && (config.subMacros?.length ?? 0) > 0);
213
+ registerMacroMetadata(config.name, {
214
+ name: config.name,
215
+ block: isBlock,
216
+ subMacros: config.subMacros ?? [],
217
+ storeVar: config.storeVar,
218
+ interpolate: config.interpolate,
219
+ merged: config.merged,
220
+ description: config.description,
221
+ parameters: config.parameters,
222
+ source,
223
+ });
224
+
199
225
  if (config.subMacros) {
200
226
  for (const sub of config.subMacros) registerSubMacro(sub);
201
227
  }
202
- if (
203
- config.block === true ||
204
- (config.block !== false && config.subMacros && config.subMacros.length > 0)
205
- ) {
228
+ if (isBlock) {
206
229
  registerBlockMacro(config.name);
207
230
  }
208
231
  }
package/src/registry.ts CHANGED
@@ -31,3 +31,38 @@ export function registerSubMacro(name: string): void {
31
31
  export function isSubMacro(name: string): boolean {
32
32
  return subMacros.has(name.toLowerCase());
33
33
  }
34
+
35
+ export interface ParameterDef {
36
+ name: string;
37
+ required?: boolean;
38
+ description?: string;
39
+ }
40
+
41
+ export interface MacroMetadata {
42
+ name: string;
43
+ block: boolean;
44
+ subMacros: string[];
45
+ storeVar?: boolean;
46
+ interpolate?: boolean;
47
+ merged?: boolean;
48
+ source: 'builtin' | 'user';
49
+ description?: string;
50
+ parameters?: ParameterDef[];
51
+ }
52
+
53
+ const metadataRegistry = new Map<string, MacroMetadata>();
54
+
55
+ export function registerMacroMetadata(
56
+ name: string,
57
+ metadata: MacroMetadata,
58
+ ): void {
59
+ metadataRegistry.set(name.toLowerCase(), metadata);
60
+ }
61
+
62
+ export function getMacroRegistry(): MacroMetadata[] {
63
+ return Array.from(metadataRegistry.values());
64
+ }
65
+
66
+ export function clearMetadataRegistry(): void {
67
+ metadataRegistry.clear();
68
+ }
package/src/story-api.ts CHANGED
@@ -6,6 +6,8 @@ import { setTitleGenerator } from './saves/save-manager';
6
6
  import { registerClass } from './class-registry';
7
7
  import { defineMacro } from './define-macro';
8
8
  import type { MacroDefinition } from './define-macro';
9
+ import { getMacroRegistry as _getMacroRegistry } from './registry';
10
+ import type { MacroMetadata } from './registry';
9
11
  import {
10
12
  getActions,
11
13
  getAction,
@@ -33,6 +35,7 @@ import type { WatchOptions } from './triggers';
33
35
  import type { TransitionConfig } from './transition';
34
36
 
35
37
  export type { StoryAction };
38
+ export type { MacroMetadata };
36
39
 
37
40
  type NavigateCallback = (to: string, from: string) => void;
38
41
  type ActionsChangedCallback = () => void;
@@ -69,6 +72,7 @@ export interface StoryAPI {
69
72
  readonly settings: typeof settings;
70
73
  registerClass(name: string, ctor: new (...args: any[]) => any): void;
71
74
  defineMacro(config: MacroDefinition): void;
75
+ getMacroRegistry(): MacroMetadata[];
72
76
  readonly saves: {
73
77
  setTitleGenerator(fn: (payload: SavePayload) => string): void;
74
78
  };
@@ -229,7 +233,11 @@ function createStoryAPI(): StoryAPI {
229
233
  },
230
234
 
231
235
  defineMacro(config: MacroDefinition): void {
232
- defineMacro(config);
236
+ defineMacro(config, 'user');
237
+ },
238
+
239
+ getMacroRegistry(): MacroMetadata[] {
240
+ return _getMacroRegistry();
233
241
  },
234
242
 
235
243
  saves: {