@elvishscout/mdstory 0.1.2 → 0.1.4

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 CHANGED
@@ -1,5 +1,438 @@
1
1
  # MdStory
2
2
 
3
- An interactive story format based on Markdown and Handlebars with JavaScript support.
3
+ An interactive fiction scripting format based on Markdown and Handlebars.
4
4
 
5
- Online demo available on <https://mdstory.elvish.cc>.
5
+ Online demo: <https://mdstory.elvish.cc>
6
+
7
+ ## Features
8
+
9
+ - Seamless integration of Markdown, Handlebars and JavaScript.
10
+ - Ease-to-use API, for both Web and command line applications.
11
+
12
+ ## File Format
13
+
14
+ ### Metadata
15
+
16
+ The story file header may include YAML-formatted [Metadata](#type-metadata). Metadata defines the basic information and global configuration of the story.
17
+
18
+ ### Chapters
19
+
20
+ Story files are divided into chapters by level-one headings. The `id` attribute of the heading serves as the unique identifier (`id`) for the chapter. If omitted, the heading content is used as the chapter `id` instead.
21
+
22
+ Handlebars template language is supported in the chapter content. During rendering, properties in `globals` and `assets` are added to the context for direct access by the template.
23
+
24
+ ### Helpers
25
+
26
+ MdStory includes built-in Handlebars helpers for creating interactive components or various functionalities:
27
+
28
+ #### `{{input type name=default}}`
29
+
30
+ Creates an input of type `type` and assigns the input value (by default `default`) to a global variable named `name`. Supported input types include: `string`, `number`, `boolean`, and `object` (represented as a JSON string).
31
+
32
+ In HTML rendering mode, it generates a corresponding `<input>` element.
33
+
34
+ #### `{{set name=value}}`
35
+
36
+ Assigns the value `value` to a global variable named `name`.
37
+
38
+ #### `{{#nav target}}`
39
+
40
+ Creates an entry to navigate to another chapter identified by `target`.
41
+
42
+ In HTML rendering mode, it generates a corresponding `<button type="submit">` element.
43
+
44
+ #### `{{linebreak [n]}}`
45
+
46
+ Inserts `n` blank lines, where `n` defaults to `1`.
47
+
48
+ #### `{{asset name}}`
49
+
50
+ Retrieves the URL of a resource file named `name`. Basically it works the same as `{{name}}` except that it supports resource names with special characters.
51
+
52
+ #### `{{mime name}}`
53
+
54
+ Retrieves the MIME type of a resource file named `name`.
55
+
56
+ ### JavaScript Integration
57
+
58
+ JavaScript may be included into the story source using the `<script>` tag. Scripts before any level-one headings are considered global scripts, while those within chapters are considered chapter scripts. Only one `<script>` tag is allowed at the beginning of the story and in each chapter.
59
+
60
+ Scripts are evaluated during runtime using the `Function()` constructor, therefore you may use top-level `return` statements to return objects of type [StoryHooks](#type-storyhooks) or [ChapterHooks](#type-chapterhooks).
61
+
62
+ ### Stylesheets
63
+
64
+ CSS stylesheets may be included using the `<style>` tag, which are extracted and gathered into the `stylesheet` property of [StoryBody](#type-storybody) during parsing.
65
+
66
+ ## Examples
67
+
68
+ Here are some [examples](./examples/) of story files.
69
+
70
+ ## API
71
+
72
+ ### `type Value`
73
+
74
+ ```typescript
75
+ type JsonPrimitive = number | string | boolean | null;
76
+ type JsonArray = JsonValue[];
77
+ type JsonObject = { [key: string]: JsonValue };
78
+ type JsonValue = JsonPrimitive | JsonArray | JsonObject;
79
+ type Value = JsonValue;
80
+ ```
81
+
82
+ The type of variables in MdStory, basically all types allowed in JSON, including primitive values (`string`, `number`, `boolean`, `null`) and nested `array`s and `object`s. Functions and `undefined` are not supported.
83
+
84
+ ### `type ValueType`
85
+
86
+ ```typescript
87
+ type ValueType = "string" | "number" | "boolean" | "object";
88
+ ```
89
+
90
+ The type indicator for input fields.
91
+
92
+ ### `type Scope`
93
+
94
+ ```typescript
95
+ type Scope = JsonObject;
96
+ ```
97
+
98
+ An object of variable values by their names, used to store global variables or input fields.
99
+
100
+ ### `type Asset`
101
+
102
+ ```typescript
103
+ type Asset = { url: string; mime?: string };
104
+ ```
105
+
106
+ A referenceable resource file.
107
+
108
+ #### Properties
109
+
110
+ - `url`: The URL of the resource.
111
+ - `mime (optional)`: The MIME type of the resource.
112
+
113
+ ### `type Metadata`
114
+
115
+ ```typescript
116
+ type Metadata = {
117
+ title?: string;
118
+ author?: string;
119
+ email?: string;
120
+ assets?: Record<string, Asset>;
121
+ };
122
+ ```
123
+
124
+ The Metadata of the story.
125
+
126
+ #### Properties
127
+
128
+ - `title (optional)`: The story title.
129
+ - `author (optional)`: The author's name.
130
+ - `email (optional)`: The author's email address.
131
+ - `globals (optional)`: A [Scope](#type-scope) containing initial values of global variables.
132
+ - `assets (optional)`: An object of all [Asset](#type-asset) objects used in the story by their names.
133
+
134
+ ### `type ChapterBody`
135
+
136
+ ```typescript
137
+ type ChapterBody = {
138
+ title: string;
139
+ template: string;
140
+ script: string;
141
+ };
142
+ ```
143
+
144
+ The structured representation of chapter content.
145
+
146
+ #### Properties
147
+
148
+ - `title`: The chapter title.
149
+ - `template`: The Handlebars template for rendering.
150
+ - `script`: The JavaScript script for the chapter.
151
+
152
+ ### `type StoryBody`
153
+
154
+ ```typescript
155
+ type StoryBody = {
156
+ metadata: Metadata;
157
+ chapters: Record<string, ChapterBody>;
158
+ entry: string | null;
159
+ script: string;
160
+ stylesheet: string;
161
+ };
162
+ ```
163
+
164
+ The structured representation of story content.
165
+
166
+ #### Properties
167
+
168
+ - `metadata`: The story [Metadata](#type-metadata).
169
+ - `chapters`: An object of [ChapterBody](#type-chapterbody) objects by their `id`s.
170
+ - `entry`: The `id` of the entry chapter of the story, which may be `null`.
171
+ - `script`: The global JavaScript script of the story.
172
+ - `stylesheet`: The global stylesheet of the story.
173
+
174
+ ### `type StoryHooks`
175
+
176
+ ```typescript
177
+ type StoryHooks = {
178
+ onStart?: (globals: Scope) => Scope | void;
179
+ };
180
+ ```
181
+
182
+ Defines the global lifecycle hooks of the story.
183
+
184
+ #### Methods
185
+
186
+ - `onStart (optional)`: Called when the story starts.
187
+ - Parameters:
188
+ - `globals`: A [Scope](#type-scope) of initial global variables.
189
+ - Return value:
190
+ - Updated [Scope](#type-scope) of global variables or `void`.
191
+
192
+ ### `type ChapterHooks`
193
+
194
+ ```typescript
195
+ type ChapterHooks = {
196
+ onEnter?: (globals: Scope) => Scope | void;
197
+ onLeave?: (globals: Scope, fields: Scope) => Scope | void;
198
+ onNavigate?: (target: string | null, globals: Scope, fields: Scope) => string | null;
199
+ };
200
+ ```
201
+
202
+ Defines the lifecycle hooks of a chapter.
203
+
204
+ #### Methods
205
+
206
+ - `onEnter (optional)`: Called when entering a chapter.
207
+ - Parameters:
208
+ - `globals`: A [Scope](#type-scope) of current global variables.
209
+ - Return value:
210
+ - Updated [Scope](#type-scope) of global variables or `void`. Such update only applies to the current lifecycle of the chapter.
211
+ - `onLeave (optional)`: Called when leaving a chapter.
212
+ - Parameters:
213
+ - `globals`: A [Scope](#type-scope) of current global variables.
214
+ - `fields`: A [Scope](#type-scope) of input fields from current chapter.
215
+ - Return value:
216
+ - Updated [Scope](#type-scope) of global variables or `void`.
217
+ - `onNavigate (optional)`: Called during chapter navigation.
218
+ - Parameters:
219
+ - `target` : `id` of the target chapter.
220
+ - `globals`: A [Scope](#type-scope) of current global variables.
221
+ - `fields`: A [Scope](#type-scope) of input fields from current chapter.
222
+ - Return value:
223
+ - Updated `target` or `void`.
224
+
225
+ ### `type Renderer`
226
+
227
+ ```typescript
228
+ type Renderer = {
229
+ input?: (options: { name: string; type: ValueType; value: Value }) => string;
230
+ nav?: (options: { target: string; children: string }) => string;
231
+ };
232
+ ```
233
+
234
+ Defines the renderer interface for generating outputs in different formats.
235
+
236
+ #### Methods
237
+
238
+ - `input (optional)`: Generates the rendering result of an input field.
239
+ - Parameters:
240
+ - `name`: The name of the variable.
241
+ - `type`: The [ValueType](#type-valuetype) of the variable.
242
+ - `value`: The default [Value](#type-value) of the variable.
243
+ - Return value:
244
+ - The rendered input field.
245
+
246
+ - `nav (optional)`: Generates the rendering result of chapter navigation.
247
+ - Parameters:
248
+ - `target`: The `id` of the target chapter.
249
+ - `children`: The text content of the navigation button.
250
+ - Return value:
251
+ - The rendered navigation button.
252
+
253
+ ### `type RenderOptions`
254
+
255
+ ```typescript
256
+ type RenderOptions = {
257
+ format: "markdown" | "html" | Renderer;
258
+ html?: boolean;
259
+ };
260
+ ```
261
+
262
+ Defines rendering options.
263
+
264
+ #### Properties
265
+
266
+ - `format`: The rendering format, which may be `"markdown"`, `"html"`, or a custom [Renderer](#type-renderer).
267
+ - `html (optional)`: Whether to parse Markdown into HTML, defaults to `false`.
268
+
269
+ ### `type RenderResult`
270
+
271
+ ```typescript
272
+ type RenderResult = {
273
+ text: string;
274
+ inputs: { name: string; type: ValueType; value: Value }[];
275
+ navs: { text: string; target: string | null }[];
276
+ };
277
+ ```
278
+
279
+ The rendering result, containing the rendered text and extracted fields.
280
+
281
+ #### Properties
282
+
283
+ - `text`: The rendered text content.
284
+ - `inputs`: An array of input fields, each containing:
285
+ - `name`: The name of the variable.
286
+ - `type`: The [ValueType](#type-valuetype) of the variable.
287
+ - `value`: The default [Value](#type-value) of the variable.
288
+ - `navs`: An array of navigation fields, each containing:
289
+ - `text`: The text content of the navigation button.
290
+ - `target`: The `id` of the target chapter.
291
+
292
+ ### `type ChapterOptions`
293
+
294
+ ```typescript
295
+ type ChapterOptions = {
296
+ id: string;
297
+ title: string;
298
+ template: string;
299
+ hooks: ChapterHooks;
300
+ };
301
+ ```
302
+
303
+ Defines the initialization options of a chapter.
304
+
305
+ #### Properties
306
+
307
+ - `id`: The unique identifier of the chapter.
308
+ - `title`: The title of the chapter.
309
+ - `template`: The Handlebars template for rendering.
310
+ - `hooks`: An object of type [ChapterHooks](#type-chapterhooks).
311
+
312
+ ### `class Chapter`
313
+
314
+ ```typescript
315
+ class Chapter {
316
+ id: string;
317
+ title: string;
318
+ template: string;
319
+ hooks: ChapterHooks;
320
+
321
+ constructor(options: ChapterOptions);
322
+ render(scope: Scope, assets: Record<string, Asset>, options: RenderOptions): string;
323
+ }
324
+ ```
325
+
326
+ Defines a chapter.
327
+
328
+ #### Properties
329
+
330
+ - `id`: The unique identifier of the chapter.
331
+ - `title`: The title of the chapter.
332
+ - `template`: The Handlebars template for rendering.
333
+ - `hooks`: Chapter hooks of type [ChapterHooks](#type-chapterhooks).
334
+
335
+ #### Methods
336
+
337
+ - `constructor`: Initializes the chapter instance.
338
+ - Parameters:
339
+ - `options`: An object of type [ChapterOptions](#type-chapteroptions).
340
+ - `render`: Renders the chapter content.
341
+ - Parameters:
342
+ - `scope`: An object of type [Scope](#type-scope) for template rendering.
343
+ - `assets`: An object of [Asset](#type-asset) objects by their names.
344
+ - `options`: An object of type [RenderOptions](#type-renderoptions).
345
+ - Return value:
346
+ - An object of type [RenderResult](#type-renderresult).
347
+
348
+ ### `type StoryPrompt`
349
+
350
+ ```typescript
351
+ type StoryPrompt = (props: { chapter: Chapter } & RenderResult) => Promise<{ target: string | null; updates: Scope } | FormData>;
352
+ ```
353
+
354
+ Defines the prompt function of the story, used to handle user input.
355
+
356
+ #### Parameters
357
+
358
+ - `props`: An object containing the current chapter and rendering result.
359
+ - `chapter`: Current [Chapter](#class-chapter).
360
+ - `text`: The rendered chapter content.
361
+ - `inputs`, `navs`: Fields from [RenderResult](#type-renderresult).
362
+
363
+ #### Return value
364
+
365
+ - A `Promise` resolving to one of the following forms:
366
+ - `{ target, updates }`: The `id` of the target chapter and updated [Scope](#type-scope) of global variables.
367
+ - `FormData`: Contains the form data of user input, typically from a Web app.
368
+
369
+ ### `class StoryBase`
370
+
371
+ ```typescript
372
+ class StoryBase {
373
+ metadata: Metadata;
374
+ globals: Scope;
375
+ chapters: Record<string, Chapter>;
376
+ entry: Chapter | null;
377
+ hooks: StoryHooks;
378
+ stylesheet: string;
379
+ assets: Record<string, Asset>;
380
+
381
+ constructor(storyBody: StoryBody);
382
+ play(prompt: StoryPrompt, options: RenderOptions): Promise<void>;
383
+ }
384
+ ```
385
+
386
+ Defines the base class of the story, containing the core logic of the story.
387
+
388
+ #### Properties
389
+
390
+ - `metadata`: Story [Metadata](#type-metadata).
391
+ - `globals`: A [Scope](#type-scope) of global variables.
392
+ - `chapters`: An object of [Chapter](#class-chapter) objects by their `id`s.
393
+ - `entry`: The entry [Chapter](#class-chapter) of the story.
394
+ - `hooks`: An object of type [StoryHooks](#type-storyhooks).
395
+ - `stylesheet`: The global stylesheet of the story.
396
+ - `assets`: An object of [Asset](#type-asset) objects by their names.
397
+
398
+ #### Methods
399
+
400
+ - `constructor`: Initializes the story instance.
401
+ - `play`: Starts playing the story.
402
+ - Parameters:
403
+ - `prompt`: A function of type [StoryPrompt](#type-storyprompt).
404
+ - `options`: An object of type [RenderOptions](#type-renderoptions).
405
+ - Return value:
406
+ - A `Promise` resolving to `void`.
407
+
408
+ ### `function parseStorySource`
409
+
410
+ ```typescript
411
+ function parseStorySource(source: string): StoryBody;
412
+ ```
413
+
414
+ Parses the story source in Markdown format.
415
+
416
+ #### Parameters
417
+
418
+ - `source`: The story source in Markdown format.
419
+
420
+ #### Return value
421
+
422
+ - An object of type [StoryBody](#type-storybody).
423
+
424
+ ### `class Story`
425
+
426
+ ```typescript
427
+ class Story extends StoryBase {
428
+ constructor(source: string);
429
+ }
430
+ ```
431
+
432
+ Inherits from [StoryBase](#class-storybase), creating a story instance from the story source in Markdown format.
433
+
434
+ #### Methods
435
+
436
+ - `constructor`: Initializes the story instance.
437
+ - Parameters:
438
+ - `source`: The story source in Markdown format.
@@ -1,6 +1,7 @@
1
1
  import Handlebars from "handlebars";
2
2
  import MarkdownIt from "markdown-it";
3
3
  import pluginAttrs from "markdown-it-attrs";
4
+ const escapeHtml = (text) => text.replace(/[<>&'"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
4
5
  const valueType = (value) => {
5
6
  if (typeof value === "string") {
6
7
  return "string";
@@ -13,7 +14,6 @@ const valueType = (value) => {
13
14
  }
14
15
  return "object";
15
16
  };
16
- const escapeHtml = (text) => text.replace(/[<>&'"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
17
17
  const createElementHtml = (tag, attrs, children) => {
18
18
  // prettier-ignore
19
19
  const voidTags = [
@@ -21,61 +21,75 @@ const createElementHtml = (tag, attrs, children) => {
21
21
  "link", "meta", "param", "source", "track", "wbr",
22
22
  ];
23
23
  const attrText = Object.entries(attrs)
24
- .filter(([, value]) => value !== undefined)
25
24
  .map(([name, value]) => {
26
25
  if (typeof value === "boolean") {
27
26
  value = value ? "" : undefined;
28
27
  }
29
- const escapedValue = escapeHtml(value ?? "");
30
- return `${name}="${escapedValue}"`;
28
+ if (value === undefined) {
29
+ return null;
30
+ }
31
+ if (value) {
32
+ const escapedValue = escapeHtml(value ?? "");
33
+ return `${name}="${escapedValue}"`;
34
+ }
35
+ else {
36
+ return name;
37
+ }
31
38
  })
39
+ .filter((attr) => attr !== null)
32
40
  .join(" ");
33
41
  if (voidTags.includes(tag)) {
34
42
  return `<${tag} ${attrText}>`;
35
43
  }
36
44
  return `<${tag} ${attrText}>${children ?? ""}</${tag}>`;
37
45
  };
38
- const createInputHtml = ({ tagMap = {}, name, type, value, required, readonly, }) => {
39
- const tag = tagMap["input"] ?? "input";
46
+ const createInputHtml = ({ name, type, value }) => {
40
47
  const inputType = type === "boolean" ? "checkbox" : "text";
41
48
  const inputAttrs = {
42
49
  name,
43
50
  type: inputType,
44
51
  value: inputType !== "checkbox" ? (type === "object" ? JSON.stringify(value) : String(value)) : undefined,
45
52
  checked: inputType === "checkbox" && value ? "" : undefined,
46
- required,
47
- readonly,
48
53
  "aria-label": name,
49
54
  };
50
- return createElementHtml(tag, inputAttrs);
55
+ return createElementHtml("input", inputAttrs);
51
56
  };
52
- const createSubmitButtonHtml = ({ tagMap = {}, target, children, }) => {
53
- const tag = tagMap["button"] ?? "button";
57
+ const createSubmitButtonHtml = ({ target, children }) => {
54
58
  const buttonAttrs = {
55
59
  name: "@target",
56
60
  type: "submit",
57
61
  value: target,
58
62
  };
59
- return createElementHtml(tag, buttonAttrs, children);
63
+ return createElementHtml("button", buttonAttrs, children);
60
64
  };
61
- const useHelper = ({ inputs, sets, navs }, assets, options) => {
65
+ const markdownRenderer = {
66
+ input({ name, type }) {
67
+ if (type === "boolean") {
68
+ return `[? _${name}_]`;
69
+ }
70
+ else {
71
+ return `[> _${name}_]`;
72
+ }
73
+ },
74
+ nav({ children }) {
75
+ return `[@ __${children}__]`;
76
+ },
77
+ };
78
+ const htmlRenderer = {
79
+ input({ type, name, value }) {
80
+ return createInputHtml({ name, type, value });
81
+ },
82
+ nav({ target, children }) {
83
+ return createSubmitButtonHtml({ target: target ?? "", children });
84
+ },
85
+ };
86
+ const useHelper = ({ inputs, sets, navs }, assets, renderer) => {
62
87
  return {
63
88
  input(type, opt) {
64
89
  for (const name in opt.hash) {
65
- let result;
66
90
  const value = opt.hash[name];
67
91
  inputs.push({ name, type, value });
68
- if (options.format === "html") {
69
- result = createInputHtml({ name, type, value, ...options });
70
- }
71
- else {
72
- if (type === "boolean") {
73
- result = `[? _${name}_]`;
74
- }
75
- else {
76
- result = `[> _${name}_]`;
77
- }
78
- }
92
+ const result = renderer.input ? renderer.input({ name, type, value }) : "";
79
93
  return new Handlebars.SafeString(result);
80
94
  }
81
95
  return "";
@@ -89,15 +103,9 @@ const useHelper = ({ inputs, sets, navs }, assets, options) => {
89
103
  return "";
90
104
  },
91
105
  nav(target, opt) {
92
- let result;
93
106
  const text = opt.fn(this).trim();
94
107
  navs.push({ text, target });
95
- if (options.format === "html") {
96
- result = createSubmitButtonHtml({ target: target ?? "", children: text, ...options });
97
- }
98
- else {
99
- result = `[@ __${text}__]`;
100
- }
108
+ const result = renderer.nav ? renderer.nav({ target, children: text }) : "";
101
109
  return new Handlebars.SafeString(result);
102
110
  },
103
111
  asset(name) {
@@ -107,7 +115,7 @@ const useHelper = ({ inputs, sets, navs }, assets, options) => {
107
115
  return new Handlebars.SafeString(assets[name]?.mime ?? "");
108
116
  },
109
117
  linebreak(n) {
110
- return new Handlebars.SafeString("\n".repeat(n ?? 1));
118
+ return new Handlebars.SafeString("<br>".repeat(n ?? 1));
111
119
  },
112
120
  };
113
121
  };
@@ -118,19 +126,28 @@ export class Chapter {
118
126
  this.template = template;
119
127
  this.hooks = hooks;
120
128
  }
121
- render(scope, assets = {}, options) {
122
- const md = new MarkdownIt({ html: true }).use(pluginAttrs);
129
+ render(scope, assets = {}, { format, html }) {
130
+ let renderer;
131
+ if (format === "markdown") {
132
+ renderer = markdownRenderer;
133
+ }
134
+ else if (format === "html") {
135
+ renderer = htmlRenderer;
136
+ }
137
+ else {
138
+ renderer = format;
139
+ }
123
140
  const fields = {
124
141
  inputs: [],
125
142
  sets: [],
126
143
  navs: [],
127
144
  };
128
- const helpers = useHelper(fields, assets, options);
145
+ const helpers = useHelper(fields, assets, renderer);
129
146
  let text;
130
- if (options.format === "html") {
147
+ if (html) {
148
+ const md = new MarkdownIt({ html: true }).use(pluginAttrs);
131
149
  text = Handlebars.compile(this.template)(scope, { helpers });
132
150
  text = md.render(text);
133
- text = createElementHtml("input", { type: "submit", disabled: true, hidden: true }) + text;
134
151
  }
135
152
  else {
136
153
  text = Handlebars.compile(this.template, { noEscape: true })(scope, { helpers });
@@ -1,9 +1,10 @@
1
1
  import { z } from "zod";
2
2
  export const ValueSchema = z.any().transform((v) => v);
3
3
  export const ScopeSchema = z.record(ValueSchema);
4
+ const AssetObjectSchema = z.object({ url: z.string(), mime: z.string().optional() });
4
5
  export const AssetSchema = z.union([
5
- z.string().transform((url) => ({ url, mime: undefined })),
6
- z.object({ url: z.string(), mime: z.string().optional() }),
6
+ z.string().transform((url) => AssetObjectSchema.parse({ url, mime: undefined })),
7
+ AssetObjectSchema,
7
8
  ]);
8
9
  export const AssetsSchema = z.record(AssetSchema);
9
10
  export const MetadataSchema = z.object({
@@ -4,9 +4,9 @@ import pluginFrontMatter from "markdown-it-front-matter";
4
4
  import pluginAttrs from "markdown-it-attrs";
5
5
  import { MetadataSchema } from "./definitions.js";
6
6
  import { DuplicateIdError, EmptyChapterIdError, InvalidMetadataError } from "./error.js";
7
- export const parseStoryContent = (content) => {
7
+ export const parseStorySource = (source) => {
8
8
  const md = new MarkdownIt({ html: true }).use(pluginAttrs).use(pluginFrontMatter, () => { });
9
- const tokens = md.parse(content, {});
9
+ const tokens = md.parse(source, {});
10
10
  let metadata = MetadataSchema.parse({});
11
11
  let storyScript = "";
12
12
  let stylesheet = "";
@@ -61,7 +61,7 @@ export const parseStoryContent = (content) => {
61
61
  }
62
62
  }
63
63
  });
64
- const lines = content.split("\n").map((line, i) => {
64
+ const lines = source.split("\n").map((line, i) => {
65
65
  if (ignoredRanges.find(([from, to]) => i >= from && i < to)) {
66
66
  return null;
67
67
  }
@@ -13,9 +13,9 @@ const parstInput = (type, text) => {
13
13
  }
14
14
  return text;
15
15
  };
16
- const parseFormData = (formData, { inputs, sets }) => {
16
+ const parseFormData = (formData, { inputs }) => {
17
17
  const target = formData.get("@target") || null;
18
- const updates = Object.fromEntries([...inputs, ...sets].map(({ name, type }) => {
18
+ const updates = Object.fromEntries(inputs.map(({ name, type }) => {
19
19
  const value = formData.get(name);
20
20
  try {
21
21
  return [name, parstInput(type, value)];
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from "./base/index.js";
2
- import { StoryBase, parseStoryContent } from "./base/index.js";
2
+ import { StoryBase, parseStorySource } from "./base/index.js";
3
3
  export class Story extends StoryBase {
4
- constructor(content) {
5
- super(parseStoryContent(content));
4
+ constructor(source) {
5
+ super(parseStorySource(source));
6
6
  }
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@elvishscout/mdstory",
3
- "version": "0.1.2",
4
- "description": "An interactive story format based on Markdown and Handlebars with JavaScript support.",
3
+ "repository": "https://github.com/ElvishScout/mdstory.git",
4
+ "version": "0.1.4",
5
+ "description": "An interactive fiction scripting format based on Markdown and Handlebars.",
5
6
  "main": "./dist/index.js",
6
7
  "type": "module",
7
8
  "files": [
@@ -37,4 +38,4 @@
37
38
  ],
38
39
  "author": "Jiakai Jiang",
39
40
  "license": "ISC"
40
- }
41
+ }
@@ -1,13 +1,22 @@
1
1
  import { ValueType, Value, ChapterHooks, Scope, Asset } from "./definitions.js";
2
- type MarkdownOptions = {};
3
- type HtmlOptions = {
4
- tagMap?: Record<string, string>;
2
+ export type Renderer = {
3
+ input?: ({ name, type, value }: {
4
+ name: string;
5
+ type: ValueType;
6
+ value: string;
7
+ }) => string;
8
+ nav?: ({ target, children }: {
9
+ target: string | null;
10
+ children: string;
11
+ }) => string;
12
+ };
13
+ export type RenderOptions = {
14
+ format: "markdown" | "html" | Renderer;
15
+ html?: boolean;
5
16
  };
6
- export type RenderOptions = ({
7
- format: "markdown";
8
- } & MarkdownOptions) | ({
9
- format: "html";
10
- } & HtmlOptions);
17
+ export type RenderResult = {
18
+ text: string;
19
+ } & Omit<Fields, "sets">;
11
20
  type Fields = {
12
21
  inputs: {
13
22
  name: string;
@@ -24,9 +33,6 @@ type Fields = {
24
33
  target: string | null;
25
34
  }[];
26
35
  };
27
- export type RenderResult = {
28
- text: string;
29
- } & Fields;
30
36
  export type ChapterOptions = {
31
37
  id: string;
32
38
  title: string;
@@ -39,6 +45,6 @@ export declare class Chapter {
39
45
  template: string;
40
46
  hooks: ChapterHooks;
41
47
  constructor({ id, title, template, hooks }: ChapterOptions);
42
- render(scope: Scope, assets: Record<string, Asset> | undefined, options: RenderOptions): RenderResult;
48
+ render(scope: Scope, assets: Record<string, Asset> | undefined, { format, html }: RenderOptions): RenderResult;
43
49
  }
44
50
  export {};
@@ -9,7 +9,7 @@ export declare const ValueSchema: z.ZodEffects<z.ZodAny, string | number | boole
9
9
  export declare const ScopeSchema: z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>;
10
10
  export declare const AssetSchema: z.ZodUnion<[z.ZodEffects<z.ZodString, {
11
11
  url: string;
12
- mime: undefined;
12
+ mime?: string | undefined;
13
13
  }, string>, z.ZodObject<{
14
14
  url: z.ZodString;
15
15
  mime: z.ZodOptional<z.ZodString>;
@@ -22,7 +22,7 @@ export declare const AssetSchema: z.ZodUnion<[z.ZodEffects<z.ZodString, {
22
22
  }>]>;
23
23
  export declare const AssetsSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodEffects<z.ZodString, {
24
24
  url: string;
25
- mime: undefined;
25
+ mime?: string | undefined;
26
26
  }, string>, z.ZodObject<{
27
27
  url: z.ZodString;
28
28
  mime: z.ZodOptional<z.ZodString>;
@@ -40,7 +40,7 @@ export declare const MetadataSchema: z.ZodObject<{
40
40
  globals: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>>;
41
41
  assets: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodEffects<z.ZodString, {
42
42
  url: string;
43
- mime: undefined;
43
+ mime?: string | undefined;
44
44
  }, string>, z.ZodObject<{
45
45
  url: z.ZodString;
46
46
  mime: z.ZodOptional<z.ZodString>;
@@ -57,9 +57,6 @@ export declare const MetadataSchema: z.ZodObject<{
57
57
  email?: string | undefined;
58
58
  globals?: Record<string, string | number | boolean | JsonArray | JsonObject | null> | undefined;
59
59
  assets?: Record<string, {
60
- url: string;
61
- mime: undefined;
62
- } | {
63
60
  url: string;
64
61
  mime?: string | undefined;
65
62
  }> | undefined;
@@ -1,2 +1,2 @@
1
1
  import { StoryBody } from "./definitions.js";
2
- export declare const parseStoryContent: (content: string) => StoryBody;
2
+ export declare const parseStorySource: (source: string) => StoryBody;
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./base/index.js";
2
2
  import { StoryBase } from "./base/index.js";
3
3
  export declare class Story extends StoryBase {
4
- constructor(content: string);
4
+ constructor(source: string);
5
5
  }