@latex-kit/components 0.0.0-stage → 0.2.1

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,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2026 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,102 @@
1
- # Temporary Holding Version
1
+ # @latex-kit/components
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
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/latex-kit/actions/workflows/ci.yml">
9
+ <img height="20" src="https://github.com/constructive-io/latex-kit/actions/workflows/ci.yml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/constructive-io/latex-kit/blob/main/LICENSE">
12
+ <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
13
+ </a>
14
+ <a href="https://www.npmjs.com/package/@latex-kit/components">
15
+ <img height="20" src="https://img.shields.io/npm/v/@latex-kit/components.svg"/>
16
+ </a>
17
+ </p>
18
+
19
+ <p align="center">TypeScript components for latex-kit templates: tables, signature blocks, exhibits, bilingual names.</p>
20
+
21
+
22
+ A component is a TypeScript function from a value to LaTeX. A template calls
23
+ it with `\component{name}{path}`; the value at `path` (or the whole record,
24
+ when `path` is empty) is passed in. Long prose stays in `.tex`; components
25
+ are for the parts that are easier to compute: tables, repeated signature
26
+ blocks, exhibits.
27
+
28
+ ## Built in
29
+
30
+ | Component | Value | Prints |
31
+ |---|---|---|
32
+ | `signatures` | a list of parties (or one) | signature blocks, two side by side |
33
+ | `exhibits` | a list of `{ title, body }` or strings | each on a new page, "Exhibit A", "Exhibit B", … |
34
+ | `bilingual` | `{ en: '…', ja: '…' }` | the record's language first: `株式会社サンプル (Sample KK)` |
35
+ | `address` | an `Address` | line1, line2, "City, Region Postal", country on separate lines |
36
+
37
+ ```latex
38
+ \component{signatures}{parties}
39
+ \component{exhibits}{exhibits}
40
+ \component{bilingual}{company.name}
41
+ ```
42
+
43
+ ## Your own
44
+
45
+ `components/<name>.ts` in a latex-kit project:
46
+
47
+ ```ts
48
+ import { defineComponent, money, table } from 'latex-kit';
49
+
50
+ export default defineComponent((data: any, ctx) =>
51
+ table(
52
+ data.items,
53
+ [
54
+ { header: 'Description', value: 'description' },
55
+ { header: 'Qty', value: 'quantity', format: 'number', align: 'r' },
56
+ { header: 'Amount', value: 'amount', format: 'money:USD', align: 'r' },
57
+ ],
58
+ ctx,
59
+ { footer: [['Total', '', money(ctx, data.total, 'USD')]] }
60
+ )
61
+ );
62
+ ```
63
+
64
+ ```latex
65
+ \component{line-items}{}
66
+ ```
67
+
68
+ Return `tex`-tagged LaTeX (values are escaped, the literal parts are not) or
69
+ a plain string (escaped). `ctx` has the variant, locale, document ID and
70
+ whole record, plus:
71
+
72
+ | | |
73
+ |---|---|
74
+ | `ctx.format(value, 'money:USD')` | the template filters, as plain text |
75
+ | `ctx.signature(party)` | the block `\signature` prints |
76
+ | `ctx.missing('path')` | a red marker in drafts; an error in final builds |
77
+
78
+ Helpers: `table`, `exhibit`, `signatures`, `bilingual`, `address`, `money`,
79
+ `date`, `number`, `percent`, `get`, and `tex` / `raw` / `escapeLatex` from
80
+ `@latex-kit/escape`. A blank build calls components with no value; the
81
+ helpers print labelled blanks.
82
+
83
+ ## Clauses in TypeScript
84
+
85
+ `clauses/<id>.ts` next to the `.tex` clauses:
86
+
87
+ ```ts
88
+ import { defineClause } from 'latex-kit';
89
+
90
+ export default defineClause({
91
+ title: 'Term',
92
+ version: 2,
93
+ body: 'This Agreement lasts \\field{termMonths|words} (\\field{termMonths}) months.',
94
+ });
95
+ ```
96
+
97
+ `body` is template text, checked like any clause. `render: (data, ctx) => tex\`…\``
98
+ builds the clause in code instead.
99
+
100
+ ## License
101
+
102
+ MIT
package/address.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /** A postal address on separate lines: line1, line2, "City, Region Postal", country. */
4
+ export declare function address(value: unknown, ctx: Pick<ComponentContext, 'missing' | 'variant' | 'path'>): RawLatex;
package/address.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.address = address;
4
+ const escape_1 = require("@latex-kit/escape");
5
+ /** A postal address on separate lines: line1, line2, "City, Region Postal", country. */
6
+ function address(value, ctx) {
7
+ const p = ctx.path || 'address';
8
+ if (ctx.variant === 'blank')
9
+ return new escape_1.RawLatex(`\\lkblank{${p}.line1}\\\\\n\\lkblank{${p}.city}`);
10
+ if (!value || typeof value !== 'object')
11
+ return ctx.missing(p);
12
+ const a = value;
13
+ const s = (k) => (typeof a[k] === 'string' && a[k] !== '' ? a[k] : undefined);
14
+ const cityLine = [s('city'), [s('region'), s('postalCode')].filter(Boolean).join(' ')].filter(Boolean).join(', ');
15
+ const lines = [s('line1'), s('line2'), cityLine || undefined, s('country')].filter((l) => !!l);
16
+ if (lines.length === 0)
17
+ return ctx.missing(p);
18
+ return new escape_1.RawLatex(lines.map((l) => (0, escape_1.escapeLatex)(l)).join('\\\\\n'));
19
+ }
package/bilingual.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /**
4
+ * A name or phrase in two languages, the record's language first:
5
+ * `{ ja: '株式会社サンプル', en: 'Sample KK' }` in a ja-JP record prints
6
+ * "株式会社サンプル (Sample KK)"; in en-US, "Sample KK (株式会社サンプル)".
7
+ */
8
+ export declare function bilingual(value: unknown, ctx: Pick<ComponentContext, 'locale' | 'missing' | 'variant' | 'path'>): RawLatex;
package/bilingual.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bilingual = bilingual;
4
+ const escape_1 = require("@latex-kit/escape");
5
+ /**
6
+ * A name or phrase in two languages, the record's language first:
7
+ * `{ ja: '株式会社サンプル', en: 'Sample KK' }` in a ja-JP record prints
8
+ * "株式会社サンプル (Sample KK)"; in en-US, "Sample KK (株式会社サンプル)".
9
+ */
10
+ function bilingual(value, ctx) {
11
+ if (ctx.variant === 'blank')
12
+ return new escape_1.RawLatex(`\\lkblank{${ctx.path || 'name'}}`);
13
+ if (typeof value === 'string')
14
+ return new escape_1.RawLatex((0, escape_1.escapeLatex)(value));
15
+ if (!value || typeof value !== 'object')
16
+ return ctx.missing(ctx.path || 'name');
17
+ const entries = Object.entries(value).filter((e) => typeof e[1] === 'string' && e[1] !== '');
18
+ if (entries.length === 0)
19
+ return ctx.missing(ctx.path || 'name');
20
+ const lang = ctx.locale.toLowerCase();
21
+ const primary = lang.split('-')[0];
22
+ const rank = (tag) => {
23
+ const t = tag.toLowerCase();
24
+ return t === lang ? 0 : t.split('-')[0] === primary ? 1 : 2;
25
+ };
26
+ const sorted = [...entries].sort((a, b) => rank(a[0]) - rank(b[0]));
27
+ const [first, ...rest] = sorted.map(([, v]) => (0, escape_1.escapeLatex)(v));
28
+ return new escape_1.RawLatex(rest.length ? `${first} (${rest.join(', ')})` : first);
29
+ }
package/clause.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { Component } from '@latex-kit/template';
2
+ /**
3
+ * A clause written in TypeScript: `clauses/<id>.ts`.
4
+ *
5
+ * export default defineClause({
6
+ * title: 'Term',
7
+ * version: 2,
8
+ * body: 'This Agreement lasts \\field{termMonths|words} months.',
9
+ * });
10
+ *
11
+ * `body` is template text, checked like a .tex clause. `render` builds the
12
+ * clause from the record's data instead, like a component.
13
+ */
14
+ export interface ClauseDefinition {
15
+ title?: string;
16
+ version?: string | number;
17
+ body?: string;
18
+ render?: Component;
19
+ }
20
+ export declare function defineClause(def: ClauseDefinition): ClauseDefinition;
package/clause.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineClause = defineClause;
4
+ function defineClause(def) {
5
+ if (def.body === undefined && def.render === undefined)
6
+ throw new Error('defineClause: give a body (template text) or a render function');
7
+ return def;
8
+ }
@@ -0,0 +1,4 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /** A postal address on separate lines: line1, line2, "City, Region Postal", country. */
4
+ export declare function address(value: unknown, ctx: Pick<ComponentContext, 'missing' | 'variant' | 'path'>): RawLatex;
package/esm/address.js ADDED
@@ -0,0 +1,16 @@
1
+ import { escapeLatex, RawLatex } from '@latex-kit/escape';
2
+ /** A postal address on separate lines: line1, line2, "City, Region Postal", country. */
3
+ export function address(value, ctx) {
4
+ const p = ctx.path || 'address';
5
+ if (ctx.variant === 'blank')
6
+ return new RawLatex(`\\lkblank{${p}.line1}\\\\\n\\lkblank{${p}.city}`);
7
+ if (!value || typeof value !== 'object')
8
+ return ctx.missing(p);
9
+ const a = value;
10
+ const s = (k) => (typeof a[k] === 'string' && a[k] !== '' ? a[k] : undefined);
11
+ const cityLine = [s('city'), [s('region'), s('postalCode')].filter(Boolean).join(' ')].filter(Boolean).join(', ');
12
+ const lines = [s('line1'), s('line2'), cityLine || undefined, s('country')].filter((l) => !!l);
13
+ if (lines.length === 0)
14
+ return ctx.missing(p);
15
+ return new RawLatex(lines.map((l) => escapeLatex(l)).join('\\\\\n'));
16
+ }
@@ -0,0 +1,8 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /**
4
+ * A name or phrase in two languages, the record's language first:
5
+ * `{ ja: '株式会社サンプル', en: 'Sample KK' }` in a ja-JP record prints
6
+ * "株式会社サンプル (Sample KK)"; in en-US, "Sample KK (株式会社サンプル)".
7
+ */
8
+ export declare function bilingual(value: unknown, ctx: Pick<ComponentContext, 'locale' | 'missing' | 'variant' | 'path'>): RawLatex;
@@ -0,0 +1,26 @@
1
+ import { escapeLatex, RawLatex } from '@latex-kit/escape';
2
+ /**
3
+ * A name or phrase in two languages, the record's language first:
4
+ * `{ ja: '株式会社サンプル', en: 'Sample KK' }` in a ja-JP record prints
5
+ * "株式会社サンプル (Sample KK)"; in en-US, "Sample KK (株式会社サンプル)".
6
+ */
7
+ export function bilingual(value, ctx) {
8
+ if (ctx.variant === 'blank')
9
+ return new RawLatex(`\\lkblank{${ctx.path || 'name'}}`);
10
+ if (typeof value === 'string')
11
+ return new RawLatex(escapeLatex(value));
12
+ if (!value || typeof value !== 'object')
13
+ return ctx.missing(ctx.path || 'name');
14
+ const entries = Object.entries(value).filter((e) => typeof e[1] === 'string' && e[1] !== '');
15
+ if (entries.length === 0)
16
+ return ctx.missing(ctx.path || 'name');
17
+ const lang = ctx.locale.toLowerCase();
18
+ const primary = lang.split('-')[0];
19
+ const rank = (tag) => {
20
+ const t = tag.toLowerCase();
21
+ return t === lang ? 0 : t.split('-')[0] === primary ? 1 : 2;
22
+ };
23
+ const sorted = [...entries].sort((a, b) => rank(a[0]) - rank(b[0]));
24
+ const [first, ...rest] = sorted.map(([, v]) => escapeLatex(v));
25
+ return new RawLatex(rest.length ? `${first} (${rest.join(', ')})` : first);
26
+ }
@@ -0,0 +1,20 @@
1
+ import type { Component } from '@latex-kit/template';
2
+ /**
3
+ * A clause written in TypeScript: `clauses/<id>.ts`.
4
+ *
5
+ * export default defineClause({
6
+ * title: 'Term',
7
+ * version: 2,
8
+ * body: 'This Agreement lasts \\field{termMonths|words} months.',
9
+ * });
10
+ *
11
+ * `body` is template text, checked like a .tex clause. `render` builds the
12
+ * clause from the record's data instead, like a component.
13
+ */
14
+ export interface ClauseDefinition {
15
+ title?: string;
16
+ version?: string | number;
17
+ body?: string;
18
+ render?: Component;
19
+ }
20
+ export declare function defineClause(def: ClauseDefinition): ClauseDefinition;
package/esm/clause.js ADDED
@@ -0,0 +1,5 @@
1
+ export function defineClause(def) {
2
+ if (def.body === undefined && def.render === undefined)
3
+ throw new Error('defineClause: give a body (template text) or a render function');
4
+ return def;
5
+ }
@@ -0,0 +1,14 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import { ComponentContext } from '@latex-kit/template';
3
+ export interface Exhibit {
4
+ title?: string;
5
+ /** Plain text; blank lines separate paragraphs, single line breaks are spaces. */
6
+ body?: string;
7
+ }
8
+ /** One exhibit: a new page headed "Exhibit A" (B, C, …), then the body. */
9
+ export declare function exhibit(title: string | undefined, body: RawLatex | string, label: string): RawLatex;
10
+ /**
11
+ * `\component{exhibits}{exhibits}`: every exhibit in a list, each on its own
12
+ * page. Items are `{ title, body }` or plain strings.
13
+ */
14
+ export declare function exhibits(value: unknown, ctx: ComponentContext): RawLatex;
@@ -0,0 +1,33 @@
1
+ import { escapeLatex, RawLatex } from '@latex-kit/escape';
2
+ import { isRawLatex } from '@latex-kit/template';
3
+ /** One exhibit: a new page headed "Exhibit A" (B, C, …), then the body. */
4
+ export function exhibit(title, body, label) {
5
+ const b = isRawLatex(body)
6
+ ? body.value
7
+ : body
8
+ .split(/\n\s*\n/)
9
+ .map((p) => escapeLatex(p.trim(), { newlines: 'space' }))
10
+ .filter(Boolean)
11
+ .join('\n\n');
12
+ return new RawLatex(`\\lkexhibit{${title ? escapeLatex(title) : ''}}{exhibit:${label}}\n${b}\n`);
13
+ }
14
+ /**
15
+ * `\component{exhibits}{exhibits}`: every exhibit in a list, each on its own
16
+ * page. Items are `{ title, body }` or plain strings.
17
+ */
18
+ export function exhibits(value, ctx) {
19
+ if (ctx.variant === 'blank')
20
+ return exhibit(undefined, new RawLatex(`\\lkblank{${ctx.path || 'exhibits'}}`), 'blank');
21
+ if (value === undefined || value === null)
22
+ return new RawLatex('');
23
+ if (!Array.isArray(value))
24
+ throw new Error(`exhibits: expected a list, got ${typeof value}`);
25
+ return new RawLatex(value
26
+ .map((item, i) => {
27
+ if (typeof item === 'string')
28
+ return exhibit(undefined, item, String(i + 1)).value;
29
+ const e = (item ?? {});
30
+ return exhibit(e.title, e.body ?? '', String(i + 1)).value;
31
+ })
32
+ .join(''));
33
+ }
@@ -0,0 +1,11 @@
1
+ import type { ComponentContext } from '@latex-kit/template';
2
+ /** `money(ctx, 1200, 'USD')` → "$1,200.00" in the record's locale. Plain text. */
3
+ export declare function money(ctx: ComponentContext, value: unknown, currency?: string): string;
4
+ /** `date(ctx, '2026-11-25')` → "November 25, 2026". Styles: long, short, medium, full, iso. */
5
+ export declare function date(ctx: ComponentContext, value: unknown, style?: 'long' | 'short' | 'medium' | 'full' | 'iso'): string;
6
+ /** `number(ctx, 1234.5, 2)` → "1,234.50". */
7
+ export declare function number(ctx: ComponentContext, value: unknown, digits?: number): string;
8
+ /** `percent(ctx, 0.08)` → "8%". */
9
+ export declare function percent(ctx: ComponentContext, value: unknown, digits?: number): string;
10
+ /** Read `a.b.0.c` from an object; undefined when any step is missing. */
11
+ export declare function get(obj: unknown, path: string): unknown;
package/esm/format.js ADDED
@@ -0,0 +1,26 @@
1
+ /** `money(ctx, 1200, 'USD')` → "$1,200.00" in the record's locale. Plain text. */
2
+ export function money(ctx, value, currency) {
3
+ return ctx.format(value, currency ? `money:${currency}` : 'money');
4
+ }
5
+ /** `date(ctx, '2026-11-25')` → "November 25, 2026". Styles: long, short, medium, full, iso. */
6
+ export function date(ctx, value, style = 'long') {
7
+ return ctx.format(value, `date:${style}`);
8
+ }
9
+ /** `number(ctx, 1234.5, 2)` → "1,234.50". */
10
+ export function number(ctx, value, digits) {
11
+ return ctx.format(value, digits === undefined ? 'number' : `number:${digits}`);
12
+ }
13
+ /** `percent(ctx, 0.08)` → "8%". */
14
+ export function percent(ctx, value, digits) {
15
+ return ctx.format(value, digits === undefined ? 'percent' : `percent:${digits}`);
16
+ }
17
+ /** Read `a.b.0.c` from an object; undefined when any step is missing. */
18
+ export function get(obj, path) {
19
+ let v = obj;
20
+ for (const seg of path.split('.')) {
21
+ if (v === null || typeof v !== 'object')
22
+ return undefined;
23
+ v = Array.isArray(v) ? (/^\d+$/.test(seg) ? v[Number(seg)] : undefined) : Object.prototype.hasOwnProperty.call(v, seg) ? v[seg] : undefined;
24
+ }
25
+ return v;
26
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Components for latex-kit templates. A component is a TypeScript function
3
+ * from a value to LaTeX; a template calls it with `\component{name}{path}`.
4
+ * The built-in ones are in {@link BUILTIN_COMPONENTS}; a project adds its own
5
+ * in `components/<name>.ts` with {@link defineComponent}.
6
+ */
7
+ import type { Component } from '@latex-kit/template';
8
+ export * from './address';
9
+ export * from './bilingual';
10
+ export * from './clause';
11
+ export * from './exhibits';
12
+ export * from './format';
13
+ export * from './signatures';
14
+ export * from './table';
15
+ export { escapeLatex, raw, RawLatex, tex } from '@latex-kit/escape';
16
+ export type { Component, ComponentContext } from '@latex-kit/template';
17
+ /** Type a project component: `export default defineComponent((value, ctx) => tex\`…\`)`. */
18
+ export declare function defineComponent<T = unknown>(fn: (value: T, ctx: Parameters<Component>[1]) => ReturnType<Component>): Component;
19
+ /** Components every template can use without defining them. */
20
+ export declare const BUILTIN_COMPONENTS: Record<string, Component>;
package/esm/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import { address } from './address';
2
+ import { bilingual } from './bilingual';
3
+ import { exhibits } from './exhibits';
4
+ import { signatures } from './signatures';
5
+ export * from './address';
6
+ export * from './bilingual';
7
+ export * from './clause';
8
+ export * from './exhibits';
9
+ export * from './format';
10
+ export * from './signatures';
11
+ export * from './table';
12
+ export { escapeLatex, raw, RawLatex, tex } from '@latex-kit/escape';
13
+ /** Type a project component: `export default defineComponent((value, ctx) => tex\`…\`)`. */
14
+ export function defineComponent(fn) {
15
+ return fn;
16
+ }
17
+ /** Components every template can use without defining them. */
18
+ export const BUILTIN_COMPONENTS = {
19
+ address: (value, ctx) => address(value, ctx),
20
+ bilingual: (value, ctx) => bilingual(value, ctx),
21
+ exhibits: (value, ctx) => exhibits(value, ctx),
22
+ signatures: (value, ctx) => signatures(value, ctx),
23
+ };
@@ -0,0 +1,7 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /**
4
+ * Signature blocks for every party in a list, two side by side:
5
+ * `\component{signatures}{parties}`. A single party works too.
6
+ */
7
+ export declare function signatures(value: unknown, ctx: ComponentContext): RawLatex;
@@ -0,0 +1,22 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ /**
3
+ * Signature blocks for every party in a list, two side by side:
4
+ * `\component{signatures}{parties}`. A single party works too.
5
+ */
6
+ export function signatures(value, ctx) {
7
+ if (ctx.variant === 'blank') {
8
+ const one = ctx.signature(undefined, `${ctx.path || 'parties'}.0`).value;
9
+ return new RawLatex(`\\par\\noindent ${one}\\hfill ${ctx.signature(undefined, `${ctx.path || 'parties'}.1`).value}\\par`);
10
+ }
11
+ const list = Array.isArray(value) ? value : value === undefined || value === null ? null : [value];
12
+ if (!list)
13
+ return ctx.missing(ctx.path || 'parties');
14
+ const blocks = list.map((p, i) => ctx.signature(p, Array.isArray(value) ? `${ctx.path}.${i}` : ctx.path).value);
15
+ let out = '';
16
+ for (let i = 0; i < blocks.length; i += 2) {
17
+ out += `\\par\\noindent ${blocks[i]}${blocks[i + 1] ? `\\hfill ${blocks[i + 1]}` : ''}\\par`;
18
+ if (i + 2 < blocks.length)
19
+ out += '\\vspace{2em}';
20
+ }
21
+ return new RawLatex(out);
22
+ }
package/esm/table.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import { ComponentContext } from '@latex-kit/template';
3
+ export interface Column<Row = Record<string, unknown>> {
4
+ header: string;
5
+ /** A field of the row (`description`, `price.amount`) or a function of the row. */
6
+ value: string | ((row: Row, index: number) => unknown);
7
+ /** Filters, as in `\field`: `money:USD`, `number:2`, `date:short`. */
8
+ format?: string;
9
+ /** `l`, `c`, `r`, or `X` (takes the remaining width). Default: X for the first column, l after. */
10
+ align?: 'l' | 'c' | 'r' | 'X';
11
+ }
12
+ export type Cell = string | number | RawLatex | null | undefined;
13
+ export interface TableOptions {
14
+ /** Rows after the body, under a rule: totals. Strings are escaped. */
15
+ footer?: Cell[][];
16
+ /** Table width. Default `\linewidth`. */
17
+ width?: string;
18
+ }
19
+ /**
20
+ * A booktabs table from a list of rows:
21
+ *
22
+ * table(items, [
23
+ * { header: 'Description', value: 'description' },
24
+ * { header: 'Amount', value: 'amount', format: 'money:USD', align: 'r' },
25
+ * ], ctx, { footer: [['Total', money(ctx, total, 'USD')]] })
26
+ *
27
+ * In a blank build (no rows) it prints one row of labelled blanks.
28
+ */
29
+ export declare function table<Row = Record<string, unknown>>(rows: readonly Row[] | undefined | null, columns: Column<Row>[], ctx: ComponentContext, options?: TableOptions): RawLatex;
package/esm/table.js ADDED
@@ -0,0 +1,55 @@
1
+ import { escapeLatex, RawLatex } from '@latex-kit/escape';
2
+ import { isRawLatex } from '@latex-kit/template';
3
+ import { get } from './format';
4
+ function cellTex(c) {
5
+ if (c === null || c === undefined)
6
+ return '';
7
+ if (isRawLatex(c))
8
+ return c.value;
9
+ return escapeLatex(String(c));
10
+ }
11
+ /**
12
+ * A booktabs table from a list of rows:
13
+ *
14
+ * table(items, [
15
+ * { header: 'Description', value: 'description' },
16
+ * { header: 'Amount', value: 'amount', format: 'money:USD', align: 'r' },
17
+ * ], ctx, { footer: [['Total', money(ctx, total, 'USD')]] })
18
+ *
19
+ * In a blank build (no rows) it prints one row of labelled blanks.
20
+ */
21
+ export function table(rows, columns, ctx, options = {}) {
22
+ if (columns.length === 0)
23
+ throw new Error('table: no columns');
24
+ const hasX = columns.some((c) => c.align === 'X');
25
+ const spec = columns.map((c, i) => c.align ?? (i === 0 && !hasX ? 'X' : 'l')).join('');
26
+ const line = (cells) => `${cells.join(' & ')} \\\\\n`;
27
+ let body = '';
28
+ if (ctx.variant === 'blank' || !Array.isArray(rows)) {
29
+ if (ctx.variant !== 'blank')
30
+ return new RawLatex(ctx.missing(ctx.path || 'table rows').value);
31
+ body = line(columns.map((c) => `\\lkblank{${typeof c.value === 'string' ? c.value : c.header}}`));
32
+ }
33
+ else {
34
+ rows.forEach((row, i) => {
35
+ body += line(columns.map((c) => {
36
+ const v = typeof c.value === 'function' ? c.value(row, i) : get(row, c.value);
37
+ if (isRawLatex(v))
38
+ return v.value;
39
+ if (v === undefined || v === null || v === '')
40
+ return '';
41
+ return escapeLatex(c.format ? ctx.format(v, c.format) : String(v));
42
+ }));
43
+ });
44
+ }
45
+ let out = `\\begin{tabularx}{${options.width ?? '\\linewidth'}}{@{}${spec}@{}}\n\\toprule\n`;
46
+ out += line(columns.map((c) => escapeLatex(c.header)));
47
+ out += '\\midrule\n' + body;
48
+ if (options.footer?.length) {
49
+ out += '\\midrule\n';
50
+ for (const r of options.footer)
51
+ out += line(r.map(cellTex));
52
+ }
53
+ out += '\\bottomrule\n\\end{tabularx}';
54
+ return new RawLatex(out);
55
+ }
package/exhibits.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import { ComponentContext } from '@latex-kit/template';
3
+ export interface Exhibit {
4
+ title?: string;
5
+ /** Plain text; blank lines separate paragraphs, single line breaks are spaces. */
6
+ body?: string;
7
+ }
8
+ /** One exhibit: a new page headed "Exhibit A" (B, C, …), then the body. */
9
+ export declare function exhibit(title: string | undefined, body: RawLatex | string, label: string): RawLatex;
10
+ /**
11
+ * `\component{exhibits}{exhibits}`: every exhibit in a list, each on its own
12
+ * page. Items are `{ title, body }` or plain strings.
13
+ */
14
+ export declare function exhibits(value: unknown, ctx: ComponentContext): RawLatex;
package/exhibits.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exhibit = exhibit;
4
+ exports.exhibits = exhibits;
5
+ const escape_1 = require("@latex-kit/escape");
6
+ const template_1 = require("@latex-kit/template");
7
+ /** One exhibit: a new page headed "Exhibit A" (B, C, …), then the body. */
8
+ function exhibit(title, body, label) {
9
+ const b = (0, template_1.isRawLatex)(body)
10
+ ? body.value
11
+ : body
12
+ .split(/\n\s*\n/)
13
+ .map((p) => (0, escape_1.escapeLatex)(p.trim(), { newlines: 'space' }))
14
+ .filter(Boolean)
15
+ .join('\n\n');
16
+ return new escape_1.RawLatex(`\\lkexhibit{${title ? (0, escape_1.escapeLatex)(title) : ''}}{exhibit:${label}}\n${b}\n`);
17
+ }
18
+ /**
19
+ * `\component{exhibits}{exhibits}`: every exhibit in a list, each on its own
20
+ * page. Items are `{ title, body }` or plain strings.
21
+ */
22
+ function exhibits(value, ctx) {
23
+ if (ctx.variant === 'blank')
24
+ return exhibit(undefined, new escape_1.RawLatex(`\\lkblank{${ctx.path || 'exhibits'}}`), 'blank');
25
+ if (value === undefined || value === null)
26
+ return new escape_1.RawLatex('');
27
+ if (!Array.isArray(value))
28
+ throw new Error(`exhibits: expected a list, got ${typeof value}`);
29
+ return new escape_1.RawLatex(value
30
+ .map((item, i) => {
31
+ if (typeof item === 'string')
32
+ return exhibit(undefined, item, String(i + 1)).value;
33
+ const e = (item ?? {});
34
+ return exhibit(e.title, e.body ?? '', String(i + 1)).value;
35
+ })
36
+ .join(''));
37
+ }
package/format.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ComponentContext } from '@latex-kit/template';
2
+ /** `money(ctx, 1200, 'USD')` → "$1,200.00" in the record's locale. Plain text. */
3
+ export declare function money(ctx: ComponentContext, value: unknown, currency?: string): string;
4
+ /** `date(ctx, '2026-11-25')` → "November 25, 2026". Styles: long, short, medium, full, iso. */
5
+ export declare function date(ctx: ComponentContext, value: unknown, style?: 'long' | 'short' | 'medium' | 'full' | 'iso'): string;
6
+ /** `number(ctx, 1234.5, 2)` → "1,234.50". */
7
+ export declare function number(ctx: ComponentContext, value: unknown, digits?: number): string;
8
+ /** `percent(ctx, 0.08)` → "8%". */
9
+ export declare function percent(ctx: ComponentContext, value: unknown, digits?: number): string;
10
+ /** Read `a.b.0.c` from an object; undefined when any step is missing. */
11
+ export declare function get(obj: unknown, path: string): unknown;
package/format.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.money = money;
4
+ exports.date = date;
5
+ exports.number = number;
6
+ exports.percent = percent;
7
+ exports.get = get;
8
+ /** `money(ctx, 1200, 'USD')` → "$1,200.00" in the record's locale. Plain text. */
9
+ function money(ctx, value, currency) {
10
+ return ctx.format(value, currency ? `money:${currency}` : 'money');
11
+ }
12
+ /** `date(ctx, '2026-11-25')` → "November 25, 2026". Styles: long, short, medium, full, iso. */
13
+ function date(ctx, value, style = 'long') {
14
+ return ctx.format(value, `date:${style}`);
15
+ }
16
+ /** `number(ctx, 1234.5, 2)` → "1,234.50". */
17
+ function number(ctx, value, digits) {
18
+ return ctx.format(value, digits === undefined ? 'number' : `number:${digits}`);
19
+ }
20
+ /** `percent(ctx, 0.08)` → "8%". */
21
+ function percent(ctx, value, digits) {
22
+ return ctx.format(value, digits === undefined ? 'percent' : `percent:${digits}`);
23
+ }
24
+ /** Read `a.b.0.c` from an object; undefined when any step is missing. */
25
+ function get(obj, path) {
26
+ let v = obj;
27
+ for (const seg of path.split('.')) {
28
+ if (v === null || typeof v !== 'object')
29
+ return undefined;
30
+ v = Array.isArray(v) ? (/^\d+$/.test(seg) ? v[Number(seg)] : undefined) : Object.prototype.hasOwnProperty.call(v, seg) ? v[seg] : undefined;
31
+ }
32
+ return v;
33
+ }
package/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Components for latex-kit templates. A component is a TypeScript function
3
+ * from a value to LaTeX; a template calls it with `\component{name}{path}`.
4
+ * The built-in ones are in {@link BUILTIN_COMPONENTS}; a project adds its own
5
+ * in `components/<name>.ts` with {@link defineComponent}.
6
+ */
7
+ import type { Component } from '@latex-kit/template';
8
+ export * from './address';
9
+ export * from './bilingual';
10
+ export * from './clause';
11
+ export * from './exhibits';
12
+ export * from './format';
13
+ export * from './signatures';
14
+ export * from './table';
15
+ export { escapeLatex, raw, RawLatex, tex } from '@latex-kit/escape';
16
+ export type { Component, ComponentContext } from '@latex-kit/template';
17
+ /** Type a project component: `export default defineComponent((value, ctx) => tex\`…\`)`. */
18
+ export declare function defineComponent<T = unknown>(fn: (value: T, ctx: Parameters<Component>[1]) => ReturnType<Component>): Component;
19
+ /** Components every template can use without defining them. */
20
+ export declare const BUILTIN_COMPONENTS: Record<string, Component>;
package/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.BUILTIN_COMPONENTS = exports.tex = exports.RawLatex = exports.raw = exports.escapeLatex = void 0;
18
+ exports.defineComponent = defineComponent;
19
+ const address_1 = require("./address");
20
+ const bilingual_1 = require("./bilingual");
21
+ const exhibits_1 = require("./exhibits");
22
+ const signatures_1 = require("./signatures");
23
+ __exportStar(require("./address"), exports);
24
+ __exportStar(require("./bilingual"), exports);
25
+ __exportStar(require("./clause"), exports);
26
+ __exportStar(require("./exhibits"), exports);
27
+ __exportStar(require("./format"), exports);
28
+ __exportStar(require("./signatures"), exports);
29
+ __exportStar(require("./table"), exports);
30
+ var escape_1 = require("@latex-kit/escape");
31
+ Object.defineProperty(exports, "escapeLatex", { enumerable: true, get: function () { return escape_1.escapeLatex; } });
32
+ Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return escape_1.raw; } });
33
+ Object.defineProperty(exports, "RawLatex", { enumerable: true, get: function () { return escape_1.RawLatex; } });
34
+ Object.defineProperty(exports, "tex", { enumerable: true, get: function () { return escape_1.tex; } });
35
+ /** Type a project component: `export default defineComponent((value, ctx) => tex\`…\`)`. */
36
+ function defineComponent(fn) {
37
+ return fn;
38
+ }
39
+ /** Components every template can use without defining them. */
40
+ exports.BUILTIN_COMPONENTS = {
41
+ address: (value, ctx) => (0, address_1.address)(value, ctx),
42
+ bilingual: (value, ctx) => (0, bilingual_1.bilingual)(value, ctx),
43
+ exhibits: (value, ctx) => (0, exhibits_1.exhibits)(value, ctx),
44
+ signatures: (value, ctx) => (0, signatures_1.signatures)(value, ctx),
45
+ };
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@latex-kit/components",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.1",
4
+ "description": "TypeScript components for latex-kit templates: tables, signature blocks, exhibits, bilingual names",
5
+ "author": "Constructive <developers@constructive.io>",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/latex-kit",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/latex-kit"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/latex-kit/issues"
21
+ },
22
+ "devDependencies": {
23
+ "makage": "^0.8.0"
24
+ },
25
+ "keywords": [
26
+ "latex",
27
+ "components",
28
+ "tables",
29
+ "signatures",
30
+ "documents"
31
+ ],
32
+ "dependencies": {
33
+ "@latex-kit/escape": "0.3.1",
34
+ "@latex-kit/template": "0.4.1"
35
+ },
36
+ "scripts": {
37
+ "copy": "makage assets",
38
+ "clean": "makage clean",
39
+ "build": "makage build",
40
+ "build:dev": "makage build --dev",
41
+ "lint": "eslint . --fix",
42
+ "test": "jest",
43
+ "test:watch": "jest --watch"
44
+ }
6
45
  }
@@ -0,0 +1,7 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import type { ComponentContext } from '@latex-kit/template';
3
+ /**
4
+ * Signature blocks for every party in a list, two side by side:
5
+ * `\component{signatures}{parties}`. A single party works too.
6
+ */
7
+ export declare function signatures(value: unknown, ctx: ComponentContext): RawLatex;
package/signatures.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signatures = signatures;
4
+ const escape_1 = require("@latex-kit/escape");
5
+ /**
6
+ * Signature blocks for every party in a list, two side by side:
7
+ * `\component{signatures}{parties}`. A single party works too.
8
+ */
9
+ function signatures(value, ctx) {
10
+ if (ctx.variant === 'blank') {
11
+ const one = ctx.signature(undefined, `${ctx.path || 'parties'}.0`).value;
12
+ return new escape_1.RawLatex(`\\par\\noindent ${one}\\hfill ${ctx.signature(undefined, `${ctx.path || 'parties'}.1`).value}\\par`);
13
+ }
14
+ const list = Array.isArray(value) ? value : value === undefined || value === null ? null : [value];
15
+ if (!list)
16
+ return ctx.missing(ctx.path || 'parties');
17
+ const blocks = list.map((p, i) => ctx.signature(p, Array.isArray(value) ? `${ctx.path}.${i}` : ctx.path).value);
18
+ let out = '';
19
+ for (let i = 0; i < blocks.length; i += 2) {
20
+ out += `\\par\\noindent ${blocks[i]}${blocks[i + 1] ? `\\hfill ${blocks[i + 1]}` : ''}\\par`;
21
+ if (i + 2 < blocks.length)
22
+ out += '\\vspace{2em}';
23
+ }
24
+ return new escape_1.RawLatex(out);
25
+ }
package/table.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { RawLatex } from '@latex-kit/escape';
2
+ import { ComponentContext } from '@latex-kit/template';
3
+ export interface Column<Row = Record<string, unknown>> {
4
+ header: string;
5
+ /** A field of the row (`description`, `price.amount`) or a function of the row. */
6
+ value: string | ((row: Row, index: number) => unknown);
7
+ /** Filters, as in `\field`: `money:USD`, `number:2`, `date:short`. */
8
+ format?: string;
9
+ /** `l`, `c`, `r`, or `X` (takes the remaining width). Default: X for the first column, l after. */
10
+ align?: 'l' | 'c' | 'r' | 'X';
11
+ }
12
+ export type Cell = string | number | RawLatex | null | undefined;
13
+ export interface TableOptions {
14
+ /** Rows after the body, under a rule: totals. Strings are escaped. */
15
+ footer?: Cell[][];
16
+ /** Table width. Default `\linewidth`. */
17
+ width?: string;
18
+ }
19
+ /**
20
+ * A booktabs table from a list of rows:
21
+ *
22
+ * table(items, [
23
+ * { header: 'Description', value: 'description' },
24
+ * { header: 'Amount', value: 'amount', format: 'money:USD', align: 'r' },
25
+ * ], ctx, { footer: [['Total', money(ctx, total, 'USD')]] })
26
+ *
27
+ * In a blank build (no rows) it prints one row of labelled blanks.
28
+ */
29
+ export declare function table<Row = Record<string, unknown>>(rows: readonly Row[] | undefined | null, columns: Column<Row>[], ctx: ComponentContext, options?: TableOptions): RawLatex;
package/table.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.table = table;
4
+ const escape_1 = require("@latex-kit/escape");
5
+ const template_1 = require("@latex-kit/template");
6
+ const format_1 = require("./format");
7
+ function cellTex(c) {
8
+ if (c === null || c === undefined)
9
+ return '';
10
+ if ((0, template_1.isRawLatex)(c))
11
+ return c.value;
12
+ return (0, escape_1.escapeLatex)(String(c));
13
+ }
14
+ /**
15
+ * A booktabs table from a list of rows:
16
+ *
17
+ * table(items, [
18
+ * { header: 'Description', value: 'description' },
19
+ * { header: 'Amount', value: 'amount', format: 'money:USD', align: 'r' },
20
+ * ], ctx, { footer: [['Total', money(ctx, total, 'USD')]] })
21
+ *
22
+ * In a blank build (no rows) it prints one row of labelled blanks.
23
+ */
24
+ function table(rows, columns, ctx, options = {}) {
25
+ if (columns.length === 0)
26
+ throw new Error('table: no columns');
27
+ const hasX = columns.some((c) => c.align === 'X');
28
+ const spec = columns.map((c, i) => c.align ?? (i === 0 && !hasX ? 'X' : 'l')).join('');
29
+ const line = (cells) => `${cells.join(' & ')} \\\\\n`;
30
+ let body = '';
31
+ if (ctx.variant === 'blank' || !Array.isArray(rows)) {
32
+ if (ctx.variant !== 'blank')
33
+ return new escape_1.RawLatex(ctx.missing(ctx.path || 'table rows').value);
34
+ body = line(columns.map((c) => `\\lkblank{${typeof c.value === 'string' ? c.value : c.header}}`));
35
+ }
36
+ else {
37
+ rows.forEach((row, i) => {
38
+ body += line(columns.map((c) => {
39
+ const v = typeof c.value === 'function' ? c.value(row, i) : (0, format_1.get)(row, c.value);
40
+ if ((0, template_1.isRawLatex)(v))
41
+ return v.value;
42
+ if (v === undefined || v === null || v === '')
43
+ return '';
44
+ return (0, escape_1.escapeLatex)(c.format ? ctx.format(v, c.format) : String(v));
45
+ }));
46
+ });
47
+ }
48
+ let out = `\\begin{tabularx}{${options.width ?? '\\linewidth'}}{@{}${spec}@{}}\n\\toprule\n`;
49
+ out += line(columns.map((c) => (0, escape_1.escapeLatex)(c.header)));
50
+ out += '\\midrule\n' + body;
51
+ if (options.footer?.length) {
52
+ out += '\\midrule\n';
53
+ for (const r of options.footer)
54
+ out += line(r.map(cellTex));
55
+ }
56
+ out += '\\bottomrule\n\\end{tabularx}';
57
+ return new escape_1.RawLatex(out);
58
+ }