@flipbook-md/core 0.0.0-stage → 0.14.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,252 @@
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
+ It is one of three Flipbook MD projects:
6
+
7
+ | Project | What it is |
8
+ | --- | --- |
9
+ | [`flipbook-md`](https://github.com/uminocelo/flipbook-md) | The `flipbook` command: present, check and export decks. |
10
+ | `@flipbook-md/core` (this package) | The library both apps build on: loading, rendering, blueprints, export and document import. |
11
+ | Flipbook Studio | A self-hosted editor for the same deck files, with optional AI from a model running on your machine. **Coming soon.** |
12
+
13
+ If you only want to present or export a deck, install the CLI instead:
14
+
15
+ ```sh
16
+ npm install -g flipbook-md
17
+ ```
18
+
19
+ ## Install
20
+
21
+ ```sh
22
+ npm install @flipbook-md/core
23
+ ```
24
+
25
+ Requires Node.js 20 or newer. Export needs Chromium (`npx playwright install chromium`), and video export also needs `ffmpeg` on your `PATH`.
26
+
27
+ ## Use
28
+
29
+ ```js
30
+ import { discoverPresentation, startServer, exportCarousel } from "@flipbook-md/core";
31
+
32
+ const deck = await discoverPresentation("./my-talk");
33
+ const { url } = await startServer(deck);
34
+ console.log(`Open ${url}`);
35
+
36
+ await exportCarousel(deck, { preset: "instagram", outputDir: "dist/carousel" });
37
+ ```
38
+
39
+ ### Create and write files
40
+
41
+ ```js
42
+ import { createDeck, readFileWithHash, atomicWrite, FileConflictError } from "@flipbook-md/core";
43
+
44
+ // A folder deck with one placeholder slide per required step of the kit's blueprint.
45
+ const { path } = await createDeck("./decks", { name: "my-guide", title: "My guide", kind: "folder", group: "marvel_content_guide" });
46
+
47
+ // Hash-checked, atomic writes: pass the hash you read, or null for a new file.
48
+ const { text, hash } = await readFileWithHash(`${path}/slides/01_cover.slide.md`);
49
+ await atomicWrite(`${path}/slides/01_cover.slide.md`, text.replace("Guide", "Field guide"), { baseHash: hash, roots: ["./decks"] });
50
+ ```
51
+
52
+ `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`.
53
+
54
+ ### Edit slides
55
+
56
+ ```js
57
+ import { readSlide, updateSlide, renderSlideDocument, discoverPresentation } from "@flipbook-md/core";
58
+
59
+ // Folder decks: the manifest path is the slide ID. Single-file decks: "#0", "#1", …
60
+ const { draft, locations, hash } = await readSlide("./decks/my-guide", "slides/01_cover.slide.md");
61
+
62
+ // Only changed fields are written, where they were found. Unchanged files stay byte-for-byte identical.
63
+ await updateSlide("./decks/my-guide", "slides/01_cover.slide.md", { ...draft, template: "summary" }, hash);
64
+
65
+ // A script-free preview document (declarative shadow DOM), optionally for an unsaved draft.
66
+ const deck = await discoverPresentation("./decks/my-guide");
67
+ const html = await renderSlideDocument(deck, 0, { draft, assetBase: "/files/" });
68
+ ```
69
+
70
+ ### Read and write kits
71
+
72
+ ```js
73
+ import { readKit, writeKit, renderSampleDocument } from "@flipbook-md/core";
74
+
75
+ // library: ~/Documents/flipbook/{themes,templates,blueprints}/<name>; deck: presentation.css,
76
+ // templates.yml, blueprint.yml in deckDir; builtin: core's own library (read-only).
77
+ const ref = { kind: "theme", name: "dusk", scope: "library" };
78
+ const { hash } = await writeKit(ref, { source: ":root { --accent: #f05; }" }, null); // null = create
79
+ const kit = await readKit(ref); // { source, parsed, hash, builtIn, path, invalid }
80
+ await writeKit(ref, { source: kit.source.replace("#f05", "#0f8") }, kit.hash);
81
+
82
+ // Cover, content, quote, media and callout samples in one script-free document.
83
+ const html = await renderSampleDocument({ themeCss: kit.source, canvas: "midnight" });
84
+ ```
85
+
86
+ YAML kits are validated by the same normalizers the loader uses before anything is written.
87
+
88
+ Form edits send a merge patch instead of the whole file. Comments, key order and quoting survive, and `null` deletes a key:
89
+
90
+ ```js
91
+ const talk = { kind: "blueprint", name: "talk", scope: "library" };
92
+ await writeKit(talk, { patch: { types: { point: { max_chars: 250 } } } }, (await readKit(talk)).hash);
93
+ // A deck's templates.yml is patched one template at a time (created if missing):
94
+ await writeKit({ kind: "template", name: "quote", scope: "deck", deckDir }, { patch: { align: "start" } }, hash);
95
+ ```
96
+
97
+ ### Duplicate decks and kits
98
+
99
+ ```js
100
+ import { copyKit, duplicateDeck } from "@flipbook-md/core";
101
+
102
+ // A sibling copy: every file except dist/, hidden entries and symlinks; only the title changes.
103
+ const { path, skipped } = await duplicateDeck("talks/q3", { name: "q3-dry-run", title: "Q3 (dry run)" });
104
+ console.log(path, skipped); // talks/q3-dry-run [] (skipped lists any symlinks left out)
105
+
106
+ // Themes, templates and blueprints copy into the library; groups go through copyGroup.
107
+ await copyKit({ kind: "theme", name: "marvel-jarvis" }, "my-theme"); // ~/Documents/flipbook/themes/my-theme.css
108
+ ```
109
+
110
+ A single-file deck is copied to `<name>.slide.md` in the same folder and shares its `assets/`.
111
+
112
+ ### Copy and rename media kits
113
+
114
+ ```js
115
+ import { copyGroup, findDecksUsingGroup, readKit, renameGroup } from "@flipbook-md/core";
116
+
117
+ await copyGroup({ name: "marvel_content_guide", scope: "builtin" }, "my_kit"); // ~/Documents/flipbook/templates/my_kit.yml
118
+ const { hash } = await readKit({ kind: "group", name: "my_kit", scope: "library" });
119
+ await renameGroup({ name: "my_kit", scope: "library" }, "brand", hash); // my_kit_* templates become brand_*
120
+ console.log(await findDecksUsingGroup("my_kit", ["talks/q3", "intro.slide.md"])); // decks still on the old name
121
+ ```
122
+
123
+ ### Check a draft blueprint
124
+
125
+ ```js
126
+ import { evaluateDraftBlueprint, loadPresentation } from "@flipbook-md/core";
127
+
128
+ const { presentation } = await loadPresentation("talk", { blueprint: "evaluate" });
129
+ const result = evaluateDraftBlueprint(presentation, "pages:\n - type: cover\ntypes:\n cover: {}\n");
130
+ if (result.errors) console.log("The blueprint itself is invalid:", result.errors);
131
+ else console.log(result.violations); // the slides that would fail
132
+ ```
133
+
134
+ ### Export with progress and cancel
135
+
136
+ ```js
137
+ import { checkExportDependencies, exportCarousel } from "@flipbook-md/core";
138
+
139
+ const deps = await checkExportDependencies("png"); // "video" also checks ffmpeg
140
+ if (!deps.ok) console.log(`Run: ${deps.installCommand}`);
141
+
142
+ const controller = new AbortController();
143
+ const result = await exportCarousel(deck, {
144
+ preset: "instagram",
145
+ outputDir: "dist/carousel",
146
+ onProgress: ({ done, total }) => console.log(`${done}/${total}`),
147
+ signal: controller.signal // controller.abort() stops Chromium (and ffmpeg for video)
148
+ });
149
+ if (result.cancelled) console.log("Cancelled; kept", result.files);
150
+ else console.log(result.files); // ["01-intro.png", …, "README.txt"]
151
+ ```
152
+
153
+ `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.
154
+
155
+ ### Check against the blueprint without throwing
156
+
157
+ ```js
158
+ import { loadPresentation } from "@flipbook-md/core";
159
+
160
+ // Violations come back as data (the CLI still throws and prints `message`).
161
+ // An unsaved draft can be applied in memory first.
162
+ const { presentation, violations } = await loadPresentation("./decks/my-guide", {
163
+ draft: { slideId: "slides/02_point.slide.md", draft }
164
+ });
165
+ // violations: [{ source: "slides/02_point.slide.md", code: "MAX_CHARS", message: "slides/02_point.slide.md: visible text is 412 characters; maximum is 250." }]
166
+ ```
167
+
168
+ 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.
169
+
170
+ ### Add, reorder and delete slides
171
+
172
+ ```js
173
+ import { insertSlide, removeSlide, reorderSlides, readFileWithHash } from "@flipbook-md/core";
174
+
175
+ // Structure writes are guarded by the manifest's hash (or the single file's).
176
+ const { hash } = await readFileWithHash("./decks/my-guide/presentation.yml");
177
+
178
+ // Writes slides/NN_<type|slide>.slide.md with the next free NN and adds it after the given slide.
179
+ const added = await insertSlide("./decks/my-guide", "slides/01_cover.slide.md", draft, hash);
180
+
181
+ // Only the manifest's slides: order changes; files are never renamed.
182
+ const order = ["slides/01_cover.slide.md", "slides/03_point.slide.md", added.slideId];
183
+ const { hashes } = await reorderSlides("./decks/my-guide", order, added.hashes["presentation.yml"]);
184
+
185
+ // Removes the manifest entry, then the file. A deck's last slide can't be removed (LastSlideError).
186
+ await removeSlide("./decks/my-guide", added.slideId, hashes["presentation.yml"]);
187
+ ```
188
+
189
+ 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.
190
+
191
+ ### Add and remove deck images
192
+
193
+ ```js
194
+ import { writeDeckAsset, listDeckAssets, assetReferences, removeDeckAsset, loadPresentation } from "@flipbook-md/core";
195
+
196
+ // PNG, JPEG, GIF, WebP or SVG, up to 8 MB, and the bytes must match the extension (AssetError otherwise).
197
+ // Saved in the deck's assets/ (next to the file for a single-file deck) under a kebab-case name;
198
+ // a taken name gets -2, -3, … — an existing file is never overwritten.
199
+ const { path } = await writeDeckAsset("./decks/my-guide", "Cover Photo.png", bytes); // "assets/cover-photo.png"
200
+
201
+ const images = await listDeckAssets("./decks/my-guide"); // [{ path, bytes, mime, width, height }]
202
+
203
+ // Which slides (and the deck's own presentation.css) use which image.
204
+ const { presentation } = await loadPresentation("./decks/my-guide", { blueprint: "evaluate" });
205
+ const uses = await assetReferences(presentation); // [{ source, index, path }]
206
+
207
+ await removeDeckAsset("./decks/my-guide", path);
208
+ ```
209
+
210
+ AssetError codes: `ASSET_TYPE`, `ASSET_TOO_LARGE`, `ASSET_CONTENT_MISMATCH`, `ASSET_NOT_FOUND`.
211
+
212
+ ### Import a document
213
+
214
+ ```js
215
+ import { sourceToSlides, importDeck } from "@flipbook-md/core";
216
+
217
+ // Pure and deterministic: a slide starts at each # or ## heading and each --- line. The first #
218
+ // (else the frontmatter title, else options.title) is the cover; a short intro (≤ 300 characters)
219
+ // sits on it. A section over 8 list items or 700 visible characters splits into "Title (2)", …
220
+ // Code fences are never split; #### and deeper become bold lines; raw HTML is removed.
221
+ const { title, slides, sections, chars, dropped } = sourceToSlides(markdown, { title: "notes" });
222
+
223
+ // Writes a folder deck (never a single file): images first, under the writeDeckAsset rules,
224
+ // then slides/NN-<slug>.slide.md and presentation.yml. Nothing is left behind on failure.
225
+ // `fileName` is the href the Markdown uses; images that fail the asset check are dropped and listed.
226
+ const result = await importDeck("./decks", {
227
+ name: "notes",
228
+ title: "Meeting notes",
229
+ group: null, // or a kit name: the deck joins it, and its blueprint reports violations
230
+ markdown,
231
+ images: [{ fileName: "image1.png", bytes }]
232
+ });
233
+ // { path, slides, assets: ["assets/image1.png"], dropped: [{ kind, reason, count, detail? }] }
234
+ ```
235
+
236
+ Only the package entry is public. Deep imports such as `@flipbook-md/core/src/…` are blocked. Type declarations ship in `index.d.ts`.
237
+
238
+ 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).
239
+
240
+ ## Develop
241
+
242
+ ```sh
243
+ npm install
244
+ npm test # node:test suite
245
+ npm run typecheck # checks index.d.ts against types-check/check.ts
246
+ ```
247
+
248
+ Tests read the library in `~/Documents/flipbook/` first. Set `FLIPBOOK_HOME` to an empty folder to test against the built-in library only.
249
+
250
+ ## Release
251
+
252
+ 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.