@elvishscout/mdstory 0.1.3 → 0.2.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/LICENSE +21 -21
- package/README.md +323 -295
- package/README.zh-CN.md +323 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/cli/commands/build.js +33 -0
- package/dist/cli/commands/play.js +9 -0
- package/dist/cli/index.js +44 -0
- package/dist/cli/markdown.js +27 -0
- package/dist/cli/prompt.js +44 -0
- package/dist/core/chapter.js +31 -0
- package/dist/core/definitions.js +2 -0
- package/dist/{base → core}/index.js +2 -1
- package/dist/core/parser.js +228 -0
- package/dist/{base/chapter.js → core/render.js} +45 -63
- package/dist/core/scene.js +28 -0
- package/dist/core/schema.js +43 -0
- package/dist/core/story.js +247 -0
- package/dist/core/utils.js +66 -0
- package/dist/index.js +1 -7
- package/dist/tools/count-words.js +22 -0
- package/html-template/dist/index.html +73 -0
- package/package.json +30 -10
- package/types/cli/commands/build.d.ts +6 -0
- package/types/cli/commands/play.d.ts +4 -0
- package/types/cli/index.d.ts +2 -0
- package/types/cli/markdown.d.ts +2 -0
- package/types/cli/prompt.d.ts +3 -0
- package/types/core/chapter.d.ts +19 -0
- package/types/core/definitions.d.ts +83 -0
- package/types/{base → core}/index.d.ts +2 -1
- package/types/core/parser.d.ts +39 -0
- package/types/core/render.d.ts +46 -0
- package/types/core/scene.d.ts +20 -0
- package/types/core/schema.d.ts +92 -0
- package/types/core/story.d.ts +54 -0
- package/types/core/utils.d.ts +7 -0
- package/types/index.d.ts +1 -5
- package/types/tools/count-words.d.ts +1 -0
- package/dist/base/definitions.js +0 -29
- package/dist/base/error.js +0 -30
- package/dist/base/parser.js +0 -86
- package/dist/base/story.js +0 -82
- package/types/base/chapter.d.ts +0 -50
- package/types/base/definitions.d.ts +0 -113
- package/types/base/error.d.ts +0 -20
- package/types/base/parser.d.ts +0 -2
- package/types/base/story.d.ts +0 -19
package/dist/base/story.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { ChapterHooksSchema, StoryHooksSchema, } from "./definitions.js";
|
|
2
|
-
import { Chapter } from "./chapter.js";
|
|
3
|
-
import { ChapterNotFoundError, InvalidInputError } from "./error.js";
|
|
4
|
-
const parstInput = (type, text) => {
|
|
5
|
-
if (type === "boolean") {
|
|
6
|
-
return text === "on";
|
|
7
|
-
}
|
|
8
|
-
if (type === "number") {
|
|
9
|
-
return text ? Number(text) : null;
|
|
10
|
-
}
|
|
11
|
-
if (type === "object") {
|
|
12
|
-
return text ? JSON.parse(text) : null;
|
|
13
|
-
}
|
|
14
|
-
return text;
|
|
15
|
-
};
|
|
16
|
-
const parseFormData = (formData, { inputs, sets }) => {
|
|
17
|
-
const target = formData.get("@target") || null;
|
|
18
|
-
const updates = Object.fromEntries([...inputs, ...sets].map(({ name, type }) => {
|
|
19
|
-
const value = formData.get(name);
|
|
20
|
-
try {
|
|
21
|
-
return [name, parstInput(type, value)];
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
throw new InvalidInputError(name, value);
|
|
25
|
-
}
|
|
26
|
-
}));
|
|
27
|
-
return { target, updates };
|
|
28
|
-
};
|
|
29
|
-
export class StoryBase {
|
|
30
|
-
constructor({ metadata, chapters, entry, script, stylesheet }) {
|
|
31
|
-
const realChapters = Object.fromEntries(Object.entries(chapters).map(([id, { title, template, script }]) => {
|
|
32
|
-
const hooks = (script && ChapterHooksSchema.parse(new Function(script)())) || {};
|
|
33
|
-
return [id, new Chapter({ id, title, template, hooks })];
|
|
34
|
-
}));
|
|
35
|
-
this.metadata = metadata;
|
|
36
|
-
this.globals = metadata.globals ?? {};
|
|
37
|
-
this.chapters = realChapters;
|
|
38
|
-
this.entry = (entry && realChapters[entry]) || null;
|
|
39
|
-
this.hooks = (script && StoryHooksSchema.parse(new Function(script)())) || {};
|
|
40
|
-
this.stylesheet = stylesheet;
|
|
41
|
-
this.assets = metadata.assets ?? {};
|
|
42
|
-
}
|
|
43
|
-
async play(prompt, options) {
|
|
44
|
-
if (this.hooks.onStart) {
|
|
45
|
-
this.hooks.onStart(this.globals);
|
|
46
|
-
}
|
|
47
|
-
const assetsUrl = Object.fromEntries(Object.entries(this.assets).map(([name, { url }]) => [name, url]));
|
|
48
|
-
let chapter = this.entry;
|
|
49
|
-
while (chapter) {
|
|
50
|
-
let scope = this.globals;
|
|
51
|
-
scope = Object.assign(scope, assetsUrl);
|
|
52
|
-
if (chapter.hooks.onEnter) {
|
|
53
|
-
const modified = chapter.hooks.onEnter(this.globals);
|
|
54
|
-
if (modified !== undefined) {
|
|
55
|
-
scope = Object.assign(scope, modified);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const renderResult = chapter.render(scope, this.assets, options);
|
|
59
|
-
const promptResult = await prompt({ chapter, ...renderResult });
|
|
60
|
-
const { target, updates } = promptResult instanceof FormData ? parseFormData(promptResult, renderResult) : promptResult;
|
|
61
|
-
let modifiedTarget = target;
|
|
62
|
-
let modifiedUpdates = updates;
|
|
63
|
-
if (chapter.hooks.onLeave) {
|
|
64
|
-
const modified = chapter.hooks.onLeave(updates, this.globals);
|
|
65
|
-
if (modified !== undefined) {
|
|
66
|
-
modifiedUpdates = Object.assign(updates, modified);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (chapter.hooks.onNavigate) {
|
|
70
|
-
const modified = chapter.hooks.onNavigate(target, updates, this.globals);
|
|
71
|
-
if (modified !== undefined) {
|
|
72
|
-
modifiedTarget = modified;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
this.globals = Object.assign(this.globals, modifiedUpdates);
|
|
76
|
-
if (modifiedTarget !== null && !(modifiedTarget in this.chapters)) {
|
|
77
|
-
throw new ChapterNotFoundError(modifiedTarget);
|
|
78
|
-
}
|
|
79
|
-
chapter = modifiedTarget !== null ? this.chapters[modifiedTarget] : null;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
package/types/base/chapter.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { ValueType, Value, ChapterHooks, Scope, Asset } from "./definitions.js";
|
|
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;
|
|
16
|
-
};
|
|
17
|
-
export type RenderResult = {
|
|
18
|
-
text: string;
|
|
19
|
-
} & Fields;
|
|
20
|
-
type Fields = {
|
|
21
|
-
inputs: {
|
|
22
|
-
name: string;
|
|
23
|
-
type: ValueType;
|
|
24
|
-
value: Value;
|
|
25
|
-
}[];
|
|
26
|
-
sets: {
|
|
27
|
-
name: string;
|
|
28
|
-
type: ValueType;
|
|
29
|
-
value: Value;
|
|
30
|
-
}[];
|
|
31
|
-
navs: {
|
|
32
|
-
text: string;
|
|
33
|
-
target: string | null;
|
|
34
|
-
}[];
|
|
35
|
-
};
|
|
36
|
-
export type ChapterOptions = {
|
|
37
|
-
id: string;
|
|
38
|
-
title: string;
|
|
39
|
-
template: string;
|
|
40
|
-
hooks: ChapterHooks;
|
|
41
|
-
};
|
|
42
|
-
export declare class Chapter {
|
|
43
|
-
id: string;
|
|
44
|
-
title: string;
|
|
45
|
-
template: string;
|
|
46
|
-
hooks: ChapterHooks;
|
|
47
|
-
constructor({ id, title, template, hooks }: ChapterOptions);
|
|
48
|
-
render(scope: Scope, assets: Record<string, Asset> | undefined, { format, html }: RenderOptions): RenderResult;
|
|
49
|
-
}
|
|
50
|
-
export {};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
type JsonPrimitive = number | string | boolean | null;
|
|
3
|
-
type JsonArray = JsonValue[];
|
|
4
|
-
type JsonObject = {
|
|
5
|
-
[key: string]: JsonValue;
|
|
6
|
-
};
|
|
7
|
-
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
|
|
8
|
-
export declare const ValueSchema: z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>;
|
|
9
|
-
export declare const ScopeSchema: z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>;
|
|
10
|
-
export declare const AssetSchema: z.ZodUnion<[z.ZodEffects<z.ZodString, {
|
|
11
|
-
url: string;
|
|
12
|
-
mime?: string | undefined;
|
|
13
|
-
}, string>, z.ZodObject<{
|
|
14
|
-
url: z.ZodString;
|
|
15
|
-
mime: z.ZodOptional<z.ZodString>;
|
|
16
|
-
}, "strip", z.ZodTypeAny, {
|
|
17
|
-
url: string;
|
|
18
|
-
mime?: string | undefined;
|
|
19
|
-
}, {
|
|
20
|
-
url: string;
|
|
21
|
-
mime?: string | undefined;
|
|
22
|
-
}>]>;
|
|
23
|
-
export declare const AssetsSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodEffects<z.ZodString, {
|
|
24
|
-
url: string;
|
|
25
|
-
mime?: string | undefined;
|
|
26
|
-
}, string>, z.ZodObject<{
|
|
27
|
-
url: z.ZodString;
|
|
28
|
-
mime: z.ZodOptional<z.ZodString>;
|
|
29
|
-
}, "strip", z.ZodTypeAny, {
|
|
30
|
-
url: string;
|
|
31
|
-
mime?: string | undefined;
|
|
32
|
-
}, {
|
|
33
|
-
url: string;
|
|
34
|
-
mime?: string | undefined;
|
|
35
|
-
}>]>>;
|
|
36
|
-
export declare const MetadataSchema: z.ZodObject<{
|
|
37
|
-
title: z.ZodOptional<z.ZodString>;
|
|
38
|
-
author: z.ZodOptional<z.ZodString>;
|
|
39
|
-
email: z.ZodOptional<z.ZodString>;
|
|
40
|
-
globals: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>>;
|
|
41
|
-
assets: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodEffects<z.ZodString, {
|
|
42
|
-
url: string;
|
|
43
|
-
mime?: string | undefined;
|
|
44
|
-
}, string>, z.ZodObject<{
|
|
45
|
-
url: z.ZodString;
|
|
46
|
-
mime: z.ZodOptional<z.ZodString>;
|
|
47
|
-
}, "strip", z.ZodTypeAny, {
|
|
48
|
-
url: string;
|
|
49
|
-
mime?: string | undefined;
|
|
50
|
-
}, {
|
|
51
|
-
url: string;
|
|
52
|
-
mime?: string | undefined;
|
|
53
|
-
}>]>>>;
|
|
54
|
-
}, "strip", z.ZodTypeAny, {
|
|
55
|
-
title?: string | undefined;
|
|
56
|
-
author?: string | undefined;
|
|
57
|
-
email?: string | undefined;
|
|
58
|
-
globals?: Record<string, string | number | boolean | JsonArray | JsonObject | null> | undefined;
|
|
59
|
-
assets?: Record<string, {
|
|
60
|
-
url: string;
|
|
61
|
-
mime?: string | undefined;
|
|
62
|
-
}> | undefined;
|
|
63
|
-
}, {
|
|
64
|
-
title?: string | undefined;
|
|
65
|
-
author?: string | undefined;
|
|
66
|
-
email?: string | undefined;
|
|
67
|
-
globals?: Record<string, any> | undefined;
|
|
68
|
-
assets?: Record<string, string | {
|
|
69
|
-
url: string;
|
|
70
|
-
mime?: string | undefined;
|
|
71
|
-
}> | undefined;
|
|
72
|
-
}>;
|
|
73
|
-
export declare const StoryHooksSchema: z.ZodObject<{
|
|
74
|
-
onStart: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>], z.ZodUnknown>, z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>, z.ZodVoid]>>>;
|
|
75
|
-
}, "strip", z.ZodTypeAny, {
|
|
76
|
-
onStart?: ((args_0: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
|
|
77
|
-
}, {
|
|
78
|
-
onStart?: ((args_0: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => void | Record<string, any>) | undefined;
|
|
79
|
-
}>;
|
|
80
|
-
export declare const ChapterHooksSchema: z.ZodObject<{
|
|
81
|
-
onEnter: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>], z.ZodUnknown>, z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>, z.ZodVoid]>>>;
|
|
82
|
-
onLeave: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>, z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>], z.ZodUnknown>, z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>, z.ZodVoid]>>>;
|
|
83
|
-
onNavigate: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodUnion<[z.ZodString, z.ZodNull]>, z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>, z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>], z.ZodUnknown>, z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNull]>, z.ZodVoid]>>>;
|
|
84
|
-
}, "strip", z.ZodTypeAny, {
|
|
85
|
-
onEnter?: ((args_0: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
|
|
86
|
-
onLeave?: ((args_0: Record<string, any>, args_1: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
|
|
87
|
-
onNavigate?: ((args_0: string | null, args_1: Record<string, any>, args_2: Record<string, any>, ...args: unknown[]) => string | void | null) | undefined;
|
|
88
|
-
}, {
|
|
89
|
-
onEnter?: ((args_0: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => void | Record<string, any>) | undefined;
|
|
90
|
-
onLeave?: ((args_0: Record<string, string | number | boolean | JsonArray | JsonObject | null>, args_1: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => void | Record<string, any>) | undefined;
|
|
91
|
-
onNavigate?: ((args_0: string | null, args_1: Record<string, string | number | boolean | JsonArray | JsonObject | null>, args_2: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => string | void | null) | undefined;
|
|
92
|
-
}>;
|
|
93
|
-
export type Value = z.infer<typeof ValueSchema>;
|
|
94
|
-
export type Scope = z.infer<typeof ScopeSchema>;
|
|
95
|
-
export type Asset = z.infer<typeof AssetSchema>;
|
|
96
|
-
export type Metadata = z.infer<typeof MetadataSchema>;
|
|
97
|
-
export type StoryHooks = z.infer<typeof StoryHooksSchema>;
|
|
98
|
-
export type ChapterHooks = z.infer<typeof ChapterHooksSchema>;
|
|
99
|
-
export type ValueType = "string" | "number" | "boolean" | "object";
|
|
100
|
-
export type ChapterBody = {
|
|
101
|
-
title: string;
|
|
102
|
-
template: string;
|
|
103
|
-
script: string;
|
|
104
|
-
};
|
|
105
|
-
export type StoryBody = {
|
|
106
|
-
metadata: Metadata;
|
|
107
|
-
chapters: Record<string, ChapterBody>;
|
|
108
|
-
entry: string | null;
|
|
109
|
-
script: string;
|
|
110
|
-
stylesheet: string;
|
|
111
|
-
};
|
|
112
|
-
export type StoryAssets = Record<string, string>;
|
|
113
|
-
export {};
|
package/types/base/error.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export declare class InvalidMetadataError extends Error {
|
|
2
|
-
content: string;
|
|
3
|
-
constructor(content: string, message?: string);
|
|
4
|
-
}
|
|
5
|
-
export declare class DuplicateIdError extends Error {
|
|
6
|
-
id: string;
|
|
7
|
-
constructor(id: string, message?: string);
|
|
8
|
-
}
|
|
9
|
-
export declare class EmptyChapterIdError extends Error {
|
|
10
|
-
constructor(message?: string);
|
|
11
|
-
}
|
|
12
|
-
export declare class ChapterNotFoundError extends Error {
|
|
13
|
-
target: string | null;
|
|
14
|
-
constructor(target: string | null, message?: string);
|
|
15
|
-
}
|
|
16
|
-
export declare class InvalidInputError extends Error {
|
|
17
|
-
name: string;
|
|
18
|
-
input: string | null;
|
|
19
|
-
constructor(name: string, input: string | null, message?: string);
|
|
20
|
-
}
|
package/types/base/parser.d.ts
DELETED
package/types/base/story.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { StoryBody, StoryHooks, Scope, Metadata, Asset } from "./definitions.js";
|
|
2
|
-
import { Chapter, RenderOptions, RenderResult } from "./chapter.js";
|
|
3
|
-
export type StoryPrompt = (props: {
|
|
4
|
-
chapter: Chapter;
|
|
5
|
-
} & RenderResult) => Promise<{
|
|
6
|
-
target: string | null;
|
|
7
|
-
updates: Scope;
|
|
8
|
-
} | FormData>;
|
|
9
|
-
export declare class StoryBase {
|
|
10
|
-
metadata: Metadata;
|
|
11
|
-
globals: Scope;
|
|
12
|
-
chapters: Record<string, Chapter>;
|
|
13
|
-
entry: Chapter | null;
|
|
14
|
-
hooks: StoryHooks;
|
|
15
|
-
stylesheet: string;
|
|
16
|
-
assets: Record<string, Asset>;
|
|
17
|
-
constructor({ metadata, chapters, entry, script, stylesheet }: StoryBody);
|
|
18
|
-
play(prompt: StoryPrompt, options: RenderOptions): Promise<void>;
|
|
19
|
-
}
|