@uniweb/press 0.4.3 → 0.4.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/press",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Press your React components into downloadable files — Word, Excel, PDF. Registration-based pattern with lazy-loaded format adapters.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -52,3 +52,8 @@ export {
52
52
  // LaTeX strings that the adapter should pass through its body-text
53
53
  // escape pass unchanged. See src/latex/raw.js for rationale.
54
54
  export { markRawLatex, RAW_BEGIN, RAW_END } from './raw.js'
55
+
56
+ // LaTeX inset formatters — generic primitives that emit \cite{...} and
57
+ // \autoref{...} commands. Foundations supply the resolved keys; press
58
+ // supplies the emission shape + sentinel wrapping.
59
+ export { formatCite, formatAutoref } from './insets.js'
@@ -0,0 +1,83 @@
1
+ /**
2
+ * LaTeX inset formatters.
3
+ *
4
+ * Generic helpers that emit `\cite{...}` and `\autoref{...}` commands
5
+ * wrapped in raw-passthrough sentinels (see `./raw.js`). Foundations
6
+ * substitute the result into paragraph text via the inline-inset
7
+ * resolution pass; the LaTeX adapter's body-text escape pass leaves
8
+ * sentinel-bracketed segments unescaped.
9
+ *
10
+ * **Resolution stays foundation-side.** These helpers do not know how
11
+ * to look up bibliography records or xref entries — that's a foundation
12
+ * concern (book uses a per-website cite-registry; @uniweb/kit/xref's
13
+ * registry handles xref ids). The foundation passes already-resolved
14
+ * key arrays in. Press's job is the LaTeX emission shape:
15
+ *
16
+ * formatCite(['darwin1859', 'mendel1866'])
17
+ * -> '\cite{darwin1859,mendel1866}' (sentinel-wrapped)
18
+ *
19
+ * formatCite(['darwin1859'], { locator: '42' })
20
+ * -> '\cite[42]{darwin1859}' (sentinel-wrapped)
21
+ *
22
+ * formatCite(['darwin1859'], { suppressAuthor: true })
23
+ * -> '\citeyear{darwin1859}' (sentinel-wrapped)
24
+ *
25
+ * formatAutoref(['fig-doors', 'sec-method'])
26
+ * -> '\autoref{fig-doors}, \autoref{sec-method}' (sentinel-wrapped)
27
+ *
28
+ * Why these live in press: foundations doing document output already
29
+ * import `@uniweb/press`. The shape of these helpers — emit-LaTeX with
30
+ * sentinel wrapping — is exactly press's domain. Adding them here lets
31
+ * any future academic foundation (`@uniweb/scholar`, a thesis-only
32
+ * variant, an arXiv-flavored foundation) reuse the LaTeX emission
33
+ * vocabulary without re-deriving it.
34
+ */
35
+
36
+ import { markRawLatex } from './raw.js'
37
+
38
+ /**
39
+ * Emit a biblatex `\cite` (or `\citeyear`) command for one or more
40
+ * pre-resolved bibliography keys. Returns an empty string when the
41
+ * caller has filtered out every key (no resolved cites → no command).
42
+ *
43
+ * @param {string[]} keys - Resolved bibliography keys. Foundation has
44
+ * already filtered out any keys that don't appear in the website's
45
+ * bibliography (biblatex would fail on unknown keys at TeX-compile
46
+ * time; we drop them at substitution time so authors see clean
47
+ * output even with typos).
48
+ * @param {Object} [options]
49
+ * @param {string} [options.locator] - Page or location reference,
50
+ * becomes the optional `[N]` argument: `\cite[42]{key}`.
51
+ * @param {boolean} [options.suppressAuthor] - When true, emit
52
+ * `\citeyear` instead of `\cite` (author-date styles render only the
53
+ * year — useful for "(Darwin 1859)" → "1859" inline).
54
+ * @returns {string} Sentinel-wrapped LaTeX command, or '' when keys
55
+ * is empty.
56
+ */
57
+ export function formatCite(keys, options = {}) {
58
+ if (!Array.isArray(keys) || keys.length === 0) return ''
59
+ const cmd = options.suppressAuthor ? '\\citeyear' : '\\cite'
60
+ const list = keys.join(',')
61
+ const raw = options.locator
62
+ ? `${cmd}[${String(options.locator)}]{${list}}`
63
+ : `${cmd}{${list}}`
64
+ return markRawLatex(raw)
65
+ }
66
+
67
+ /**
68
+ * Emit one or more `\autoref{}` commands for pre-resolved xref ids.
69
+ * Multi-id clusters render as comma-separated autorefs because biblatex
70
+ * has no native multi-cite-style \autoref grouping; cleveref does (via
71
+ * \cref{a,b}) but adds a package dependency the v1 LaTeX template
72
+ * avoids.
73
+ *
74
+ * @param {string[]} ids - Resolved xref ids (foundation filtered out
75
+ * anything not present in the xref-registry).
76
+ * @returns {string} Sentinel-wrapped LaTeX command(s), or '' when ids
77
+ * is empty.
78
+ */
79
+ export function formatAutoref(ids) {
80
+ if (!Array.isArray(ids) || ids.length === 0) return ''
81
+ const refs = ids.map((id) => `\\autoref{${id}}`).join(', ')
82
+ return markRawLatex(refs)
83
+ }