@elvishscout/mdstory 0.1.0 → 0.1.1

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.
@@ -1,4 +1,4 @@
1
- import handlebars from "handlebars";
1
+ import Handlebars from "handlebars";
2
2
  import MarkdownIt from "markdown-it";
3
3
  import pluginAttrs from "markdown-it-attrs";
4
4
  const valueType = (value) => {
@@ -67,18 +67,22 @@ const createInputMarkdown = ({ name, type }) => {
67
67
  const useHelper = ({ inputs, sets, navs }, options) => {
68
68
  return {
69
69
  input(type, opt) {
70
+ let result = "";
70
71
  for (const name in opt.hash) {
71
72
  const value = opt.hash[name];
72
73
  inputs.push({ name, type, value });
73
74
  if (options.format === "html") {
74
- return createInputHtml({ name, type, value, ...options });
75
+ result = createInputHtml({ name, type, value, ...options });
75
76
  }
76
- return createInputMarkdown({ name, type });
77
+ else {
78
+ result = createInputMarkdown({ name, type });
79
+ }
80
+ break;
77
81
  }
78
- return "";
82
+ return new Handlebars.SafeString(result);
79
83
  },
80
84
  set(opt) {
81
- return Object.entries(opt.hash)
85
+ const result = Object.entries(opt.hash)
82
86
  .map(([name, value]) => {
83
87
  const type = valueType(value);
84
88
  sets.push({ name, type, value });
@@ -88,17 +92,16 @@ const useHelper = ({ inputs, sets, navs }, options) => {
88
92
  return "";
89
93
  })
90
94
  .join("");
95
+ return new Handlebars.SafeString(result);
91
96
  },
92
97
  nav(target, opt) {
93
- const text = opt.fn(target).trim();
98
+ let result = "";
99
+ const text = opt.fn(this).trim();
94
100
  navs.push({ text, target });
95
101
  if (options.format === "html") {
96
- return createSubmitButtonHtml({ target: target ?? "", children: text, ...options });
102
+ result = createSubmitButtonHtml({ target: target ?? "", children: text, ...options });
97
103
  }
98
- return "";
99
- },
100
- br(num) {
101
- return new Array(num ?? 1).fill("<br>").join();
104
+ return new Handlebars.SafeString(result);
102
105
  },
103
106
  };
104
107
  };
@@ -116,7 +119,7 @@ export class Chapter {
116
119
  sets: [],
117
120
  navs: [],
118
121
  };
119
- const handle = handlebars.create();
122
+ const handle = Handlebars.create();
120
123
  handle.registerHelper(useHelper(fields, options));
121
124
  let text = handle.compile(this.template)(scope);
122
125
  if (options.format === "html") {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elvishscout/mdstory",
3
- "version": "0.1.0",
4
- "description": "An interactive story format based on Markdown and Handlebars with JavaScript support",
3
+ "version": "0.1.1",
4
+ "description": "An interactive story format based on Markdown and Handlebars with JavaScript support.",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
7
  "files": [
@@ -10,7 +10,7 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsc",
13
- "test": "node ./test/index.test.js"
13
+ "test": "tsx ./test/index.test.ts"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/js-yaml": "^4.0.9",
@@ -19,6 +19,7 @@
19
19
  "@types/node": "^22.14.1",
20
20
  "inquirer": "^12.6.0",
21
21
  "markdown-it-terminal": "^0.4.0",
22
+ "tsx": "^4.19.3",
22
23
  "typescript": "~5.7.2"
23
24
  },
24
25
  "dependencies": {
@@ -36,4 +37,4 @@
36
37
  ],
37
38
  "author": "Jiakai Jiang",
38
39
  "license": "ISC"
39
- }
40
+ }
@@ -1,44 +0,0 @@
1
- import { ValueType, Value, ChapterHooks, Scope } from "./definitions.js";
2
- type MarkdownOptions = {};
3
- type HtmlOptions = {
4
- tagMap?: Record<string, string>;
5
- };
6
- export type RenderOptions = ({
7
- format: "markdown";
8
- } & MarkdownOptions) | ({
9
- format: "html";
10
- } & HtmlOptions);
11
- type Fields = {
12
- inputs: {
13
- name: string;
14
- type: ValueType;
15
- value: Value;
16
- }[];
17
- sets: {
18
- name: string;
19
- type: ValueType;
20
- value: Value;
21
- }[];
22
- navs: {
23
- text: string;
24
- target: string | null;
25
- }[];
26
- };
27
- export type RenderResult = {
28
- text: string;
29
- } & Fields;
30
- export type ChapterOptions = {
31
- id: string;
32
- title: string;
33
- template: string;
34
- hooks: ChapterHooks;
35
- };
36
- export declare class Chapter {
37
- id: string;
38
- title: string;
39
- template: string;
40
- hooks: ChapterHooks;
41
- constructor({ id, title, template, hooks }: ChapterOptions);
42
- render(scope: Scope, options: RenderOptions): RenderResult;
43
- }
44
- export {};
@@ -1,46 +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 MetadataSchema: z.ZodObject<{
11
- title: z.ZodDefault<z.ZodString>;
12
- globals: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodAny, string | number | boolean | JsonArray | JsonObject | null, any>>>;
13
- }, "strip", z.ZodTypeAny, {
14
- title: string;
15
- globals: Record<string, string | number | boolean | JsonArray | JsonObject | null>;
16
- }, {
17
- title?: string | undefined;
18
- globals?: Record<string, any> | undefined;
19
- }>;
20
- export declare const StoryHooksSchema: z.ZodObject<{
21
- 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]>>>;
22
- }, "strip", z.ZodTypeAny, {
23
- onStart?: ((args_0: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
24
- }, {
25
- onStart?: ((args_0: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => void | Record<string, any>) | undefined;
26
- }>;
27
- export declare const ChapterHooksSchema: z.ZodObject<{
28
- 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]>>>;
29
- 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]>>>;
30
- 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]>>>;
31
- }, "strip", z.ZodTypeAny, {
32
- onEnter?: ((args_0: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
33
- onLeave?: ((args_0: Record<string, any>, args_1: Record<string, any>, ...args: unknown[]) => void | Record<string, string | number | boolean | JsonArray | JsonObject | null>) | undefined;
34
- onNavigate?: ((args_0: string | null, args_1: Record<string, any>, args_2: Record<string, any>, ...args: unknown[]) => string | void | null) | undefined;
35
- }, {
36
- onEnter?: ((args_0: Record<string, string | number | boolean | JsonArray | JsonObject | null>, ...args: unknown[]) => void | Record<string, any>) | undefined;
37
- 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;
38
- 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;
39
- }>;
40
- export type Value = z.infer<typeof ValueSchema>;
41
- export type Scope = z.infer<typeof ScopeSchema>;
42
- export type Metadata = z.infer<typeof MetadataSchema>;
43
- export type StoryHooks = z.infer<typeof StoryHooksSchema>;
44
- export type ChapterHooks = z.infer<typeof ChapterHooksSchema>;
45
- export type ValueType = "string" | "number" | "boolean" | "object";
46
- export {};
@@ -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
- }
@@ -1,4 +0,0 @@
1
- export * from "./definitions.js";
2
- export * from "./story.js";
3
- export * from "./chapter.js";
4
- export * from "./error.js";
@@ -1,32 +0,0 @@
1
- import { StoryHooks, Scope, Metadata, ChapterHooks } 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
- type ChapterBody = {
10
- title: string;
11
- template: string;
12
- hooks: ChapterHooks;
13
- };
14
- type StoryBody = {
15
- metadata: Metadata;
16
- chapters: Record<string, ChapterBody>;
17
- entry: string | null;
18
- hooks: StoryHooks;
19
- stylesheet: string;
20
- };
21
- export declare const parseStoryContent: (content: string) => StoryBody;
22
- export declare class StoryBase {
23
- metadata: Metadata;
24
- globals: Scope;
25
- chapters: Record<string, Chapter>;
26
- entry: Chapter | null;
27
- hooks: StoryHooks;
28
- stylesheet: string;
29
- constructor({ metadata, chapters, entry, hooks, stylesheet }: StoryBody);
30
- play(prompt: StoryPrompt, options: RenderOptions): Promise<void>;
31
- }
32
- export {};
@@ -1,5 +0,0 @@
1
- export * from "./base/index.js";
2
- import { StoryBase } from "./base/index.js";
3
- export declare class Story extends StoryBase {
4
- constructor(content: string);
5
- }