-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (193 loc) · 6.39 KB
/
Copy pathindex.js
File metadata and controls
220 lines (193 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Markdown-driven dialog system (mdif)
//
// Format (game.md):
// ## SectionName <- dialog id (lowercased, spaces→-)
// > _Speaker_ Text <- a line of dialog (speaker underlined in the quote)
// - [Label](#id) <- menu choice that jumps to another dialog
// - [Label](END) <- menu choice that closes the dialog
//
// Usage:
// import { parseDialog, DialogSystem } from './mdif.js'
// const dialog = new DialogSystem(await fs.readFile('game.md', 'utf8'))
// dialog.onChoice = (choice) => {
// if (choice.url === 'END') { /* close dialog */ }
// else dialog.open(choice.url.replace('#', ''))
// }
// dialog.open('test')
// // each frame, call dialog.current to get current line, dialog.advance() on confirm
import md from 'markdown-ast'
import fm from 'front-matter'
// --- helpers ----------------------------------------------------------------
/** Extract plain text from an AST node array */
function blockText(block = []) {
return block
.map((n) => {
if (n.type === 'text') return n.text
if (n.block) return blockText(n.block)
return ''
})
.join('')
}
/** Derive a dialog id from a heading string (lowercase, spaces→hyphens) */
function headingId(text) {
return text.trim().toLowerCase().replace(/\s+/g, '-')
}
// --- parser -----------------------------------------------------------------
/**
* Parse a markdown string into:
* info – frontmatter attributes
* dialogs – Map<id, { lines: [{speaker, text}], choices: [{label, url}] }>
*/
export function parseDialog(markdown) {
const { attributes, body } = fm(markdown)
const ast = md(body)
/** @type {Map<string, {lines: Array<{speaker:string,text:string}>, choices: Array<{label:string,url:string}>}>} */
const dialogs = new Map()
let currentId = null
for (const node of ast) {
// A h2 title starts a new dialog section
if (node.type === 'title' && node.rank === 2) {
currentId = headingId(blockText(node.block))
dialogs.set(currentId, { lines: [], choices: [] })
continue
}
if (currentId === null) continue
const section = dialogs.get(currentId)
// Blockquote → a line of dialog
// Expected structure: > _Speaker_ Rest of text
if (node.type === 'quote') {
const block = node.block ?? []
let speaker = ''
let text = ''
if (block[0]?.type === 'italic') {
speaker = blockText(block[0].block)
// Everything after the italic node
text = block
.slice(1)
.map((n) => (n.type === 'text' ? n.text : blockText(n.block ?? [])))
.join('')
.trimStart()
} else {
text = blockText(block)
}
section.lines.push({ speaker, text })
continue
}
// List item → a menu choice
if (node.type === 'list') {
const block = node.block ?? []
// Each list item's block should be a single link node
const link = block.find((n) => n.type === 'link')
if (link) {
section.choices.push({
label: blockText(link.block),
url: link.url
})
}
continue
}
}
return { info: attributes, dialogs }
}
// --- DialogSystem -----------------------------------------------------------
/**
* Engine-agnostic dialog state machine.
*
* Events / callbacks:
* onLine(line) – called whenever the current line changes
* line = { speaker, text } | null
* onChoices(choices) – called when the last line has been shown and choices
* are available. choices = [{label, url}]
* If no choices exist the dialog auto-closes (onClose).
* onChoice(choice) – called when the user picks a menu item.
* choice = { label, url }
* url === 'END' means close; '#id' means jump to id.
* You are expected to call dialog.open(id) or leave it.
* onClose() – called when the dialog ends (no more lines/choices).
*
* API:
* dialog.open(id) – start/restart a named dialog section
* dialog.advance() – move to next line (call on confirm key-press)
* dialog.choose(i) – pick menu option by index
* dialog.current – { speaker, text } of the current line, or null
* dialog.choices – current choices array (populated after last line)
* dialog.isOpen – true while a dialog is active
*/
export class DialogSystem {
constructor(markdown) {
const { info, dialogs } = parseDialog(markdown)
this.info = info
this.dialogs = dialogs
this._section = null
this._lineIndex = -1
this.current = null
this.choices = []
this.isOpen = false
// Callbacks – override these
this.onLine = null
this.onChoices = null
this.onChoice = null
this.onClose = null
}
/** Start a dialog section by id */
open(id) {
const section = this.dialogs.get(id.toLowerCase())
if (!section) {
console.warn(`[mdif] Unknown dialog id: "${id}"`)
return
}
this._section = section
this._lineIndex = -1
this.choices = []
this.isOpen = true
this.advance()
}
/** Advance to the next line. Call this on each user confirmation. */
advance() {
if (!this._section) return
const { lines, choices } = this._section
this._lineIndex++
if (this._lineIndex < lines.length) {
this.current = lines[this._lineIndex]
this.choices = []
if (this.onLine) this.onLine(this.current)
} else {
// Past the last line
this.current = null
if (choices.length > 0) {
this.choices = choices
if (this.onChoices) this.onChoices(this.choices)
} else {
this.close()
}
}
}
/** Pick a menu choice by index */
choose(index) {
const choice = this.choices[index]
if (!choice) return
this.choices = []
if (this.onChoice) {
this.onChoice(choice)
} else {
// Default handling when no callback is provided
if (choice.url === 'END') {
this.close()
} else {
const id = choice.url.replace(/^#/, '')
this.open(id)
}
}
}
close() {
this.isOpen = false
this._section = null
this.current = null
this.choices = []
if (this.onClose) this.onClose()
}
}
/** Convenience: parse + return a ready-to-use DialogSystem */
export default function createDialog(markdown) {
return new DialogSystem(markdown)
}