chalk-talk 0.6.0 → 0.7.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/scripts/build.mjs CHANGED
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  // Checks chalk-talk recipes and turns them into one HTML file that plays the lesson — or, given several, a course: one
3
- // file with every lesson and a list to go from one to another.
3
+ // file with every lesson and a list to go from one to another. A recipe can be JSON (.json), text (.chalk), a Mermaid
4
+ // flowchart (.mmd, with `%% # chapter` and `%% > narration`), or Markdown (.md): each ```chalk-talk or ```mermaid block
5
+ // in it is a lesson.
4
6
  //
5
- // node build.mjs <recipe.json> [out.html] [--no-review]
6
- // node build.mjs <one.json> <two.json> … --out course.html [--title "The course"] [--no-review]
7
+ // node build.mjs <recipe.json|.chalk|.mmd|.md> [out.html] [--no-review]
8
+ // node build.mjs <one> <two> … --out course.html [--title "The course"] [--no-review]
7
9
  //
8
10
  // Only a path ending in .html is ever written. Exit codes: 0 drawn and checked (or no browser to check with); 1 a recipe
9
11
  // breaks the contract (nothing written); 2 bad arguments; 3 drawn, but the reviewer found something to fix (off the
@@ -13,7 +15,7 @@ import { readFileSync, statSync, writeFileSync } from 'node:fs'
13
15
  import { basename, dirname, join, resolve } from 'node:path'
14
16
  import { fileURLToPath } from 'node:url'
15
17
  import { runPage } from './browser.mjs'
16
- import { chaptersFor, validateScene } from './contract.mjs'
18
+ import { ChalkTextError, chaptersFor, parseChalk, PROP_ALIASES, PROP_NAMES, validateScene } from './contract.mjs'
17
19
 
18
20
  const here = dirname(fileURLToPath(import.meta.url))
19
21
  const argv = process.argv.slice(2)
@@ -24,26 +26,45 @@ const option = (name) => {
24
26
  const valued = new Set(['--out', '--title'])
25
27
  const args = argv.filter((a, i) => !a.startsWith('--') && !valued.has(argv[i - 1]))
26
28
  const noReview = argv.includes('--no-review')
27
- const sources = args.filter((a) => a.endsWith('.json'))
29
+ const KINDS = ['.json', '.chalk', '.mmd', '.md']
30
+ const sources = args.filter((a) => KINDS.some((k) => a.endsWith(k)))
28
31
  const outArg = option('--out') ?? args.find((a) => a.endsWith('.html'))
29
32
  const usage = 'usage: node build.mjs <recipe.json> [out.html] [--no-review]\n node build.mjs <one.json> <two.json> … --out course.html [--title "…"] [--no-review]'
30
- if (!sources.length || args.some((a) => !a.endsWith('.json') && !a.endsWith('.html')) || (outArg && !outArg.endsWith('.html')) || (sources.length > 1 && !outArg)) {
33
+ if (!sources.length || args.some((a) => !KINDS.some((k) => a.endsWith(k)) && !a.endsWith('.html')) || (outArg && !outArg.endsWith('.html'))) {
31
34
  console.error(usage)
32
35
  process.exit(2)
33
36
  }
34
37
 
38
+ const PROPS = [...PROP_NAMES, ...Object.keys(PROP_ALIASES)]
39
+ // The Mermaid converter is large: loaded only for a Mermaid block.
40
+ const mermaid = async (text) => (await import('./mermaid.mjs')).fromMermaid(text)
41
+
42
+ /** The recipes a file holds: one for .json, .chalk and .mmd; one per ```chalk-talk / ```mermaid block in a .md. */
43
+ async function recipesOf(file) {
44
+ const text = readFileSync(resolve(file), 'utf8')
45
+ if (file.endsWith('.json')) return [JSON.parse(text)]
46
+ if (file.endsWith('.chalk')) return [parseChalk(text, PROPS)]
47
+ if (file.endsWith('.mmd')) return [await mermaid(text)]
48
+ const blocks = [...text.matchAll(/^```(chalk-talk|mermaid)[^\n]*\n([\s\S]*?)^```/gm)]
49
+ if (!blocks.length) throw new Error('no ```chalk-talk or ```mermaid block in this file.')
50
+ const out = []
51
+ for (const [, kind, body] of blocks) out.push(kind === 'mermaid' ? await mermaid(body) : parseChalk(body, PROPS))
52
+ return out
53
+ }
54
+
35
55
  const scenes = []
36
56
  for (const file of sources) {
37
57
  try {
38
- scenes.push(validateScene(JSON.parse(readFileSync(resolve(file), 'utf8'))))
58
+ for (const recipe of await recipesOf(file)) scenes.push(validateScene(recipe))
39
59
  } catch (e) {
40
- console.error(`✗ contract: ${sources.length > 1 ? `${basename(file)}: ` : ''}${e.message}`)
60
+ const where = sources.length > 1 || file.endsWith('.md') ? `${basename(file)}: ` : ''
61
+ console.error(`✗ ${e instanceof ChalkTextError ? 'text' : 'contract'}: ${where}${e.message}`)
41
62
  process.exit(1)
42
63
  }
43
64
  }
44
65
 
45
66
  const course = scenes.length > 1
46
- const slug = String(scenes[0].id || basename(sources[0], '.json')).replace(/[^a-z0-9-]+/gi, '-').slice(0, 60) || 'drawing'
67
+ const slug = String(scenes[0].id || basename(sources[0]).replace(/\.[^.]+$/, '')).replace(/[^a-z0-9-]+/gi, '-').slice(0, 60) || 'drawing'
47
68
  const out = resolve(outArg ?? join(dirname(resolve(sources[0])), `${slug}.html`))
48
69
  const content = course ? { course: option('--title') ?? scenes[0].title, lessons: scenes } : scenes[0]
49
70
  // JSON inside a <script> tag: "<" escaped so no text in a recipe can end the tag.