bookcover-core 0.22.3 → 0.23.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.
@@ -6,6 +6,27 @@ export interface CustomFontStyle {
6
6
  family: string;
7
7
  style: FontStyle;
8
8
  }
9
+ /** Options for build_schema beyond the form itself */
10
+ export interface BuildSchemaOptions {
11
+ /**
12
+ * Typst markup to put in place of marker text in the blurb, keyed by marker — plain text
13
+ * exactly as the user sees it in the editor (e.g. 'AUTO-COPYRIGHT'). Every occurrence is
14
+ * replaced, including one inside other text; a marker the user styled only part of is split
15
+ * across text nodes and is NOT replaced, nor is one inside inline code. The form is never
16
+ * changed. Values are trusted, inserted verbatim, and must follow the rules below.
17
+ *
18
+ * - Multi-line markup must be wrapped in one `#[…];` block (the `;` ends the expression, so
19
+ * text after the marker can't continue it), or the marker put alone in its own paragraph:
20
+ * a bare newline would break a list item, heading, or bold/italic span around the marker.
21
+ * - Set your own direction (`#set text(dir: rtl)`) if the text needs it: the blurb's
22
+ * direction per paragraph comes from the marker's text, which reads left-to-right.
23
+ * - Keep text as escaped content, not string literals: each CJK run in the blurb is wrapped
24
+ * in a #text(font:) span after this, which would break a string literal around it.
25
+ * - Use no images or named fonts. The compile has no files beyond the cover's own, and
26
+ * loads only the fonts the blurb's text (substitutions included) resolves to.
27
+ */
28
+ typst_substitutions?: Record<string, string>;
29
+ }
9
30
  /** Replace straight quotes with typographic curly quotes */
10
31
  export declare function curly_quotes(text: string): string;
11
32
  /** Parse a font family name from user input. Accepts:
@@ -38,7 +59,8 @@ export declare function font_families_in_form(form: EmbedFormState): string[];
38
59
  *
39
60
  * custom_fonts supplies the sniffed serif/sans style of user-uploaded families (their bytes
40
61
  * travel separately to the compiler). Never reads a background image — that too is passed to
41
- * the generators out-of-band.
62
+ * the generators out-of-band. options.typst_substitutions replaces marker text in the blurb for
63
+ * this render only (see BuildSchemaOptions).
42
64
  */
43
- export declare function build_schema(form: EmbedFormState, custom_fonts?: CustomFontStyle[]): Record<string, unknown>;
65
+ export declare function build_schema(form: EmbedFormState, custom_fonts?: CustomFontStyle[], options?: BuildSchemaOptions): Record<string, unknown>;
44
66
  //# sourceMappingURL=form_schema.d.ts.map
@@ -1,5 +1,6 @@
1
1
  // Pure functions for building the cover generator schema from form state
2
- import { pm_to_typst } from 'pm-to-typst';
2
+ import { pm_to_typst, typst_renderer } from 'pm-to-typst';
3
+ import { escape_typst } from 'typst-utils';
3
4
  import { hex_override_to_hsl } from './colors.js';
4
5
  import { find_pattern } from './patterns.js';
5
6
  import { warn_unknown, text_dir } from './utils.js';
@@ -27,10 +28,20 @@ function pm_text(node) {
27
28
  * its own first strong letter, so an RTL paragraph gets RTL punctuation, list markers and
28
29
  * justified last line while its LTR neighbours are untouched. Blocks nested inside a list or
29
30
  * quote follow the enclosing block's direction.
31
+ * Marker text is replaced with its substitution (see BuildSchemaOptions) as each text node is
32
+ * rendered, so the marker is matched as the user typed it and only the text around it escaped.
30
33
  * Returns undefined when the blurb is empty.
31
34
  */
32
- function build_blurb(doc) {
33
- const typst = pm_to_typst(doc, { nodes: {
35
+ function build_blurb(doc, substitutions = {}) {
36
+ const marker_regex = substitution_regex(substitutions);
37
+ const typst = pm_to_typst(doc, { text: node => {
38
+ // Inline code is literal, so a marker inside it stays as typed
39
+ if (!marker_regex || (node.marks ?? []).some(mark => mark.type === 'code'))
40
+ return typst_renderer.text(node);
41
+ // Splitting on a capturing regex alternates text, marker, text, … (text may be empty)
42
+ return (node.text ?? '').split(marker_regex)
43
+ .map((piece, i) => i % 2 ? substitutions[piece] : escape_typst(piece)).join('');
44
+ }, nodes: {
34
45
  doc: (node, ctx) => (node.content ?? []).map(child => {
35
46
  const rendered = ctx.node(child);
36
47
  return text_dir(pm_text(child)) === 'rtl'
@@ -39,6 +50,15 @@ function build_blurb(doc) {
39
50
  } });
40
51
  return typst.trim() ? typst : undefined;
41
52
  }
53
+ // One capturing regex matching any marker, longest first so a marker containing another wins.
54
+ // Null when there is nothing to replace
55
+ function substitution_regex(substitutions) {
56
+ const markers = Object.keys(substitutions).filter(marker => marker)
57
+ .sort((a, b) => b.length - a.length);
58
+ if (markers.length === 0)
59
+ return null;
60
+ return new RegExp(`(${markers.map(m => m.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`);
61
+ }
42
62
  /** Parse a font family name from user input. Accepts:
43
63
  * - Plain font name: "Playwrite AR Guides"
44
64
  * - Specimen URL: https://fonts.google.com/specimen/Playwrite+AR+Guides
@@ -106,9 +126,10 @@ function build_font_config(raw, custom_fonts) {
106
126
  *
107
127
  * custom_fonts supplies the sniffed serif/sans style of user-uploaded families (their bytes
108
128
  * travel separately to the compiler). Never reads a background image — that too is passed to
109
- * the generators out-of-band.
129
+ * the generators out-of-band. options.typst_substitutions replaces marker text in the blurb for
130
+ * this render only (see BuildSchemaOptions).
110
131
  */
111
- export function build_schema(form, custom_fonts) {
132
+ export function build_schema(form, custom_fonts, options = {}) {
112
133
  const q = curly_quotes;
113
134
  // Size/print fields. A 'custom' service has no print spec, so bleed and spine come from the
114
135
  // form; every real service computes them from the page count instead
@@ -190,7 +211,7 @@ export function build_schema(form, custom_fonts) {
190
211
  author_margin_top: form.author_margin_top,
191
212
  author_margin_bottom: form.author_margin_bottom,
192
213
  // An empty blurb stays absent — there is nothing to render, not a default to apply
193
- blurb: build_blurb(form.blurb),
214
+ blurb: build_blurb(form.blurb, options.typst_substitutions),
194
215
  blurb_font: build_font_config(form.blurb_font, custom_fonts),
195
216
  blurb_size: form.blurb_size,
196
217
  blurb_color: hex_override_to_hsl(form.blurb_color) ?? undefined,
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ export type { FormState, EmbedFormState } from './form_state.js';
27
27
  export { SCHEMA_VERSION, RENDER_VERSION, SCHEMA_DEFAULTS, FORM_DEFAULTS } from './defaults.js';
28
28
  export { warn_unknown } from './utils.js';
29
29
  export { build_schema, curly_quotes, parse_font_family, normalize_font_family, font_families_in_form } from './form_schema.js';
30
- export type { CustomFontStyle } from './form_schema.js';
30
+ export type { CustomFontStyle, BuildSchemaOptions } from './form_schema.js';
31
31
  export { analyze_pixel_regions, get_builtin_bg, get_builtin_bg_regions, BG_PREVIEW_DIR } from './image_regions.js';
32
32
  export type { BuiltinBg } from './generated/builtin_bg_regions.js';
33
33
  export { derive_colors, hex_override_to_hsl, hex_to_hsl, is_dark_color } from './colors.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bookcover-core",
3
- "version": "0.22.3",
3
+ "version": "0.23.0",
4
4
  "license": "MIT-0",
5
5
  "repository": {
6
6
  "type": "git",