@flipbook-md/core 0.0.0-stage → 0.13.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 ADDED
@@ -0,0 +1,17 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Flipbook MD contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
package/README.md CHANGED
@@ -1,3 +1,229 @@
1
- # Temporary Holding Version
1
+ # @flipbook-md/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The shared library behind [Flipbook MD](https://github.com/uminocelo/flipbook-md). It loads Markdown decks, applies templates, groups, themes and blueprints, serves the slide player, and exports PNG carousels and MP4 videos.
4
+
5
+ The `flipbook` CLI and Flipbook Studio both build on this package. If you only want to present or export a deck, install the CLI instead:
6
+
7
+ ```sh
8
+ npm install -g flipbook-md
9
+ ```
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install @flipbook-md/core
15
+ ```
16
+
17
+ Requires Node.js 20 or newer. Export needs Chromium (`npx playwright install chromium`), and video export also needs `ffmpeg` on your `PATH`.
18
+
19
+ ## Use
20
+
21
+ ```js
22
+ import { discoverPresentation, startServer, exportCarousel } from "@flipbook-md/core";
23
+
24
+ const deck = await discoverPresentation("./my-talk");
25
+ const { url } = await startServer(deck);
26
+ console.log(`Open ${url}`);
27
+
28
+ await exportCarousel(deck, { preset: "instagram", outputDir: "dist/carousel" });
29
+ ```
30
+
31
+ ### Create and write files
32
+
33
+ ```js
34
+ import { createDeck, readFileWithHash, atomicWrite, FileConflictError } from "@flipbook-md/core";
35
+
36
+ // A folder deck with one placeholder slide per required step of the kit's blueprint.
37
+ const { path } = await createDeck("./decks", { name: "my-guide", title: "My guide", kind: "folder", group: "marvel_content_guide" });
38
+
39
+ // Hash-checked, atomic writes: pass the hash you read, or null for a new file.
40
+ const { text, hash } = await readFileWithHash(`${path}/slides/01_cover.slide.md`);
41
+ await atomicWrite(`${path}/slides/01_cover.slide.md`, text.replace("Guide", "Field guide"), { baseHash: hash, roots: ["./decks"] });
42
+ ```
43
+
44
+ `atomicWrite` refuses files outside `roots`, refuses core's built-in `library/` (`BuiltInReadOnlyError`), and throws `FileConflictError` when the file changed since you read it. Each typed error has a `code` such as `FILE_CONFLICT` or `DECK_EXISTS`.
45
+
46
+ ### Edit slides
47
+
48
+ ```js
49
+ import { readSlide, updateSlide, renderSlideDocument, discoverPresentation } from "@flipbook-md/core";
50
+
51
+ // Folder decks: the manifest path is the slide ID. Single-file decks: "#0", "#1", …
52
+ const { draft, locations, hash } = await readSlide("./decks/my-guide", "slides/01_cover.slide.md");
53
+
54
+ // Only changed fields are written, where they were found. Unchanged files stay byte-for-byte identical.
55
+ await updateSlide("./decks/my-guide", "slides/01_cover.slide.md", { ...draft, template: "summary" }, hash);
56
+
57
+ // A script-free preview document (declarative shadow DOM), optionally for an unsaved draft.
58
+ const deck = await discoverPresentation("./decks/my-guide");
59
+ const html = await renderSlideDocument(deck, 0, { draft, assetBase: "/files/" });
60
+ ```
61
+
62
+ ### Read and write kits
63
+
64
+ ```js
65
+ import { readKit, writeKit, renderSampleDocument } from "@flipbook-md/core";
66
+
67
+ // library: ~/Documents/flipbook/{themes,templates,blueprints}/<name>; deck: presentation.css,
68
+ // templates.yml, blueprint.yml in deckDir; builtin: core's own library (read-only).
69
+ const ref = { kind: "theme", name: "dusk", scope: "library" };
70
+ const { hash } = await writeKit(ref, { source: ":root { --accent: #f05; }" }, null); // null = create
71
+ const kit = await readKit(ref); // { source, parsed, hash, builtIn, path, invalid }
72
+ await writeKit(ref, { source: kit.source.replace("#f05", "#0f8") }, kit.hash);
73
+
74
+ // Cover, content, quote, media and callout samples in one script-free document.
75
+ const html = await renderSampleDocument({ themeCss: kit.source, canvas: "midnight" });
76
+ ```
77
+
78
+ YAML kits are validated by the same normalizers the loader uses before anything is written.
79
+
80
+ Form edits send a merge patch instead of the whole file. Comments, key order and quoting survive, and `null` deletes a key:
81
+
82
+ ```js
83
+ const talk = { kind: "blueprint", name: "talk", scope: "library" };
84
+ await writeKit(talk, { patch: { types: { point: { max_chars: 250 } } } }, (await readKit(talk)).hash);
85
+ // A deck's templates.yml is patched one template at a time (created if missing):
86
+ await writeKit({ kind: "template", name: "quote", scope: "deck", deckDir }, { patch: { align: "start" } }, hash);
87
+ ```
88
+
89
+ ### Copy and rename media kits
90
+
91
+ ```js
92
+ import { copyGroup, findDecksUsingGroup, readKit, renameGroup } from "@flipbook-md/core";
93
+
94
+ await copyGroup({ name: "marvel_content_guide", scope: "builtin" }, "my_kit"); // ~/Documents/flipbook/templates/my_kit.yml
95
+ const { hash } = await readKit({ kind: "group", name: "my_kit", scope: "library" });
96
+ await renameGroup({ name: "my_kit", scope: "library" }, "brand", hash); // my_kit_* templates become brand_*
97
+ console.log(await findDecksUsingGroup("my_kit", ["talks/q3", "intro.slide.md"])); // decks still on the old name
98
+ ```
99
+
100
+ ### Check a draft blueprint
101
+
102
+ ```js
103
+ import { evaluateDraftBlueprint, loadPresentation } from "@flipbook-md/core";
104
+
105
+ const { presentation } = await loadPresentation("talk", { blueprint: "evaluate" });
106
+ const result = evaluateDraftBlueprint(presentation, "pages:\n - type: cover\ntypes:\n cover: {}\n");
107
+ if (result.errors) console.log("The blueprint itself is invalid:", result.errors);
108
+ else console.log(result.violations); // the slides that would fail
109
+ ```
110
+
111
+ ### Export with progress and cancel
112
+
113
+ ```js
114
+ import { checkExportDependencies, exportCarousel } from "@flipbook-md/core";
115
+
116
+ const deps = await checkExportDependencies("png"); // "video" also checks ffmpeg
117
+ if (!deps.ok) console.log(`Run: ${deps.installCommand}`);
118
+
119
+ const controller = new AbortController();
120
+ const result = await exportCarousel(deck, {
121
+ preset: "instagram",
122
+ outputDir: "dist/carousel",
123
+ onProgress: ({ done, total }) => console.log(`${done}/${total}`),
124
+ signal: controller.signal // controller.abort() stops Chromium (and ffmpeg for video)
125
+ });
126
+ if (result.cancelled) console.log("Cancelled; kept", result.files);
127
+ else console.log(result.files); // ["01-intro.png", …, "README.txt"]
128
+ ```
129
+
130
+ `startServer` also takes a function instead of a presentation (called per request, so saved edits show after a reload) and a `startSlide` option; the player honours `?slide=N` too.
131
+
132
+ ### Check against the blueprint without throwing
133
+
134
+ ```js
135
+ import { loadPresentation } from "@flipbook-md/core";
136
+
137
+ // Violations come back as data (the CLI still throws and prints `message`).
138
+ // An unsaved draft can be applied in memory first.
139
+ const { presentation, violations } = await loadPresentation("./decks/my-guide", {
140
+ draft: { slideId: "slides/02_point.slide.md", draft }
141
+ });
142
+ // violations: [{ source: "slides/02_point.slide.md", code: "MAX_CHARS", message: "slides/02_point.slide.md: visible text is 412 characters; maximum is 250." }]
143
+ ```
144
+
145
+ Codes: `ORDER`, `MISSING_TYPE`, `TEMPLATE`, `MAX_CHARS`, `MAX_ITEMS`, `REQUIRED`, `NOT_ALLOWED`. A deck's own `blueprint.yml` wins over its group's. A slide out of order doesn't hide content violations: every slide of a known type is still checked against its rules.
146
+
147
+ ### Add, reorder and delete slides
148
+
149
+ ```js
150
+ import { insertSlide, removeSlide, reorderSlides, readFileWithHash } from "@flipbook-md/core";
151
+
152
+ // Structure writes are guarded by the manifest's hash (or the single file's).
153
+ const { hash } = await readFileWithHash("./decks/my-guide/presentation.yml");
154
+
155
+ // Writes slides/NN_<type|slide>.slide.md with the next free NN and adds it after the given slide.
156
+ const added = await insertSlide("./decks/my-guide", "slides/01_cover.slide.md", draft, hash);
157
+
158
+ // Only the manifest's slides: order changes; files are never renamed.
159
+ const order = ["slides/01_cover.slide.md", "slides/03_point.slide.md", added.slideId];
160
+ const { hashes } = await reorderSlides("./decks/my-guide", order, added.hashes["presentation.yml"]);
161
+
162
+ // Removes the manifest entry, then the file. A deck's last slide can't be removed (LastSlideError).
163
+ await removeSlide("./decks/my-guide", added.slideId, hashes["presentation.yml"]);
164
+ ```
165
+
166
+ In a single-file deck, slides are `---` chunks: inserting adds a chunk, reordering moves chunks and keeps each one's bytes, and slide IDs (`#0`, `#1`, …) follow the new positions.
167
+
168
+ ### Add and remove deck images
169
+
170
+ ```js
171
+ import { writeDeckAsset, listDeckAssets, assetReferences, removeDeckAsset, loadPresentation } from "@flipbook-md/core";
172
+
173
+ // PNG, JPEG, GIF, WebP or SVG, up to 8 MB, and the bytes must match the extension (AssetError otherwise).
174
+ // Saved in the deck's assets/ (next to the file for a single-file deck) under a kebab-case name;
175
+ // a taken name gets -2, -3, … — an existing file is never overwritten.
176
+ const { path } = await writeDeckAsset("./decks/my-guide", "Cover Photo.png", bytes); // "assets/cover-photo.png"
177
+
178
+ const images = await listDeckAssets("./decks/my-guide"); // [{ path, bytes, mime, width, height }]
179
+
180
+ // Which slides (and the deck's own presentation.css) use which image.
181
+ const { presentation } = await loadPresentation("./decks/my-guide", { blueprint: "evaluate" });
182
+ const uses = await assetReferences(presentation); // [{ source, index, path }]
183
+
184
+ await removeDeckAsset("./decks/my-guide", path);
185
+ ```
186
+
187
+ AssetError codes: `ASSET_TYPE`, `ASSET_TOO_LARGE`, `ASSET_CONTENT_MISMATCH`, `ASSET_NOT_FOUND`.
188
+
189
+ ### Import a document
190
+
191
+ ```js
192
+ import { sourceToSlides, importDeck } from "@flipbook-md/core";
193
+
194
+ // Pure and deterministic: a slide starts at each # or ## heading and each --- line. The first #
195
+ // (else the frontmatter title, else options.title) is the cover; a short intro (≤ 300 characters)
196
+ // sits on it. A section over 8 list items or 700 visible characters splits into "Title (2)", …
197
+ // Code fences are never split; #### and deeper become bold lines; raw HTML is removed.
198
+ const { title, slides, sections, chars, dropped } = sourceToSlides(markdown, { title: "notes" });
199
+
200
+ // Writes a folder deck (never a single file): images first, under the writeDeckAsset rules,
201
+ // then slides/NN-<slug>.slide.md and presentation.yml. Nothing is left behind on failure.
202
+ // `fileName` is the href the Markdown uses; images that fail the asset check are dropped and listed.
203
+ const result = await importDeck("./decks", {
204
+ name: "notes",
205
+ title: "Meeting notes",
206
+ group: null, // or a kit name: the deck joins it, and its blueprint reports violations
207
+ markdown,
208
+ images: [{ fileName: "image1.png", bytes }]
209
+ });
210
+ // { path, slides, assets: ["assets/image1.png"], dropped: [{ kind, reason, count, detail? }] }
211
+ ```
212
+
213
+ Only the package entry is public. Deep imports such as `@flipbook-md/core/src/…` are blocked. Type declarations ship in `index.d.ts`.
214
+
215
+ The deck file format (slides, `presentation.yml`, `templates.yml`, `blueprint.yml`, groups and themes) is documented in the [Flipbook MD README](https://github.com/uminocelo/flipbook-md#write-a-presentation).
216
+
217
+ ## Develop
218
+
219
+ ```sh
220
+ npm install
221
+ npm test # node:test suite
222
+ npm run typecheck # checks index.d.ts against types-check/check.ts
223
+ ```
224
+
225
+ Tests read the library in `~/Documents/flipbook/` first. Set `FLIPBOOK_HOME` to an empty folder to test against the built-in library only.
226
+
227
+ ## Release
228
+
229
+ Bump `version` in `package.json` and merge to `main`. CI publishes the new version to npm with provenance and tags `v<version>`. A `-next.N` version publishes under the `next` dist-tag.