@selfhelp/shared 1.0.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/LICENSE +381 -0
  3. package/README.md +266 -0
  4. package/dist/common-B96dASgu.d.mts +80 -0
  5. package/dist/common-B96dASgu.d.ts +80 -0
  6. package/dist/index-Bg60YTsF.d.mts +1743 -0
  7. package/dist/index-DEOVT86P.d.ts +1743 -0
  8. package/dist/index.d.mts +597 -0
  9. package/dist/index.d.ts +597 -0
  10. package/dist/index.js +796 -0
  11. package/dist/index.mjs +718 -0
  12. package/dist/registry/index.d.mts +2 -0
  13. package/dist/registry/index.d.ts +2 -0
  14. package/dist/registry/index.js +134 -0
  15. package/dist/registry/index.mjs +105 -0
  16. package/dist/theme/index.d.mts +63 -0
  17. package/dist/theme/index.d.ts +63 -0
  18. package/dist/theme/index.js +202 -0
  19. package/dist/theme/index.mjs +162 -0
  20. package/dist/theme/tailwind.d.mts +36 -0
  21. package/dist/theme/tailwind.d.ts +36 -0
  22. package/dist/theme/tailwind.js +125 -0
  23. package/dist/theme/tailwind.mjs +97 -0
  24. package/package.json +69 -0
  25. package/src/api/endpoints.ts +58 -0
  26. package/src/api/index.ts +5 -0
  27. package/src/assets/index.ts +5 -0
  28. package/src/assets/resolveAssetUrl.ts +44 -0
  29. package/src/cms-classes/allow-list.ts +158 -0
  30. package/src/cms-classes/classify.ts +90 -0
  31. package/src/cms-classes/index.ts +7 -0
  32. package/src/cms-classes/remap.ts +44 -0
  33. package/src/condition/evaluate.ts +121 -0
  34. package/src/condition/index.ts +5 -0
  35. package/src/index.ts +25 -0
  36. package/src/interpolation/index.ts +5 -0
  37. package/src/interpolation/replaceCalcedValues.ts +27 -0
  38. package/src/registry/index.ts +37 -0
  39. package/src/registry/styles.registry.ts +158 -0
  40. package/src/theme/index.ts +6 -0
  41. package/src/theme/tailwind.ts +73 -0
  42. package/src/theme/tokens.ts +177 -0
  43. package/src/types/api/auth.ts +46 -0
  44. package/src/types/api/envelope.ts +34 -0
  45. package/src/types/api/forms.ts +38 -0
  46. package/src/types/api/index.ts +9 -0
  47. package/src/types/api/language.ts +12 -0
  48. package/src/types/api/page.ts +9 -0
  49. package/src/types/auth.ts +110 -0
  50. package/src/types/index.ts +17 -0
  51. package/src/types/mantine/common.ts +142 -0
  52. package/src/types/pages.ts +107 -0
  53. package/src/types/styles/auth.ts +165 -0
  54. package/src/types/styles/base.ts +72 -0
  55. package/src/types/styles/composite.ts +131 -0
  56. package/src/types/styles/forms.ts +439 -0
  57. package/src/types/styles/interactive.ts +165 -0
  58. package/src/types/styles/layout.ts +184 -0
  59. package/src/types/styles/media.ts +68 -0
  60. package/src/types/styles/typography.ts +118 -0
  61. package/src/types/styles/unknown.ts +220 -0
  62. package/src/utils/index.ts +6 -0
  63. package/src/utils/spacing.ts +64 -0
  64. package/src/utils/transformPageData.ts +93 -0
@@ -0,0 +1,158 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Tailwind classes the CMS may emit in `css_mobile`. Anything outside
7
+ * this list (or the remap table) is dropped on mobile with a dev-only
8
+ * warning. Web doesn't apply this filter (it has full Tailwind +
9
+ * Mantine CSS) — only the mobile renderer does.
10
+ *
11
+ * Patterns we accept: a literal class string, OR a value-pattern using
12
+ * the `IClassPattern` shape (e.g. `mt-{xs,sm,md,lg,xl,1..12}`).
13
+ *
14
+ * Keep this list curated. Adding a class also means making sure Uniwind
15
+ * supports it on Native; if it doesn't, add an entry to `remap.ts` instead.
16
+ */
17
+
18
+ export interface IClassPattern {
19
+ /** Prefix including the dash, e.g. 'mt-', 'p-', 'text-'. */
20
+ prefix: string;
21
+ /** Allowed value tokens after the prefix. */
22
+ values: readonly string[];
23
+ }
24
+
25
+ /** Mantine spacing tokens we anchor on (matches `theme/tokens.ts`). */
26
+ export const SPACING_VALUES = ['none', 'xs', 'sm', 'md', 'lg', 'xl'] as const;
27
+
28
+ /** Mantine size tokens. */
29
+ export const SIZE_VALUES = ['xs', 'sm', 'md', 'lg', 'xl'] as const;
30
+
31
+ /** Mantine palette names. */
32
+ export const COLOR_NAMES = [
33
+ 'gray',
34
+ 'red',
35
+ 'pink',
36
+ 'grape',
37
+ 'violet',
38
+ 'indigo',
39
+ 'blue',
40
+ 'cyan',
41
+ 'teal',
42
+ 'green',
43
+ 'lime',
44
+ 'yellow',
45
+ 'orange',
46
+ 'dark',
47
+ ] as const;
48
+
49
+ /** Mantine palette name + scale 0..9 (e.g. `text-blue-6`, `bg-gray-1`). */
50
+ const COLOR_SCALE_VALUES = COLOR_NAMES.flatMap((c) => Array.from({ length: 10 }, (_, i) => `${c}-${i}`));
51
+
52
+ const NUMERIC_1_12 = Array.from({ length: 12 }, (_, i) => String(i + 1));
53
+ const NUMERIC_0_12 = ['0', ...NUMERIC_1_12];
54
+
55
+ /**
56
+ * Allow-listed class patterns. Each entry: `<prefix><value>` is allowed.
57
+ * Atomic literal classes that need no value go in `LITERAL_CLASSES`.
58
+ */
59
+ export const CLASS_PATTERNS: readonly IClassPattern[] = [
60
+ // spacing
61
+ { prefix: 'm-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
62
+ { prefix: 'mt-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
63
+ { prefix: 'mb-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
64
+ { prefix: 'ms-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
65
+ { prefix: 'me-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
66
+ { prefix: 'ml-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
67
+ { prefix: 'mr-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
68
+ { prefix: 'mx-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
69
+ { prefix: 'my-', values: [...SPACING_VALUES, ...NUMERIC_0_12, 'auto'] },
70
+ { prefix: 'p-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
71
+ { prefix: 'pt-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
72
+ { prefix: 'pb-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
73
+ { prefix: 'ps-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
74
+ { prefix: 'pe-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
75
+ { prefix: 'pl-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
76
+ { prefix: 'pr-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
77
+ { prefix: 'px-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
78
+ { prefix: 'py-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
79
+ { prefix: 'gap-', values: [...SPACING_VALUES, ...NUMERIC_0_12] },
80
+ // sizing
81
+ { prefix: 'w-', values: [...SIZE_VALUES, 'full', 'auto', 'fit', '1/2', '1/3', '2/3', '1/4', '3/4'] },
82
+ { prefix: 'h-', values: [...SIZE_VALUES, 'full', 'auto', 'fit'] },
83
+ { prefix: 'min-w-', values: ['0', 'full', 'fit'] },
84
+ { prefix: 'min-h-', values: ['0', 'full', 'fit', 'screen'] },
85
+ { prefix: 'max-w-', values: [...SIZE_VALUES, 'full', 'fit', 'screen', 'none'] },
86
+ { prefix: 'max-h-', values: [...SIZE_VALUES, 'full', 'screen', 'none'] },
87
+ // typography
88
+ { prefix: 'text-', values: [...SIZE_VALUES, 'left', 'center', 'right', 'justify', ...COLOR_SCALE_VALUES] },
89
+ { prefix: 'font-', values: ['thin', 'light', 'normal', 'medium', 'semibold', 'bold', 'extrabold', 'black'] },
90
+ { prefix: 'leading-', values: ['none', 'tight', 'snug', 'normal', 'relaxed', 'loose'] },
91
+ { prefix: 'tracking-', values: ['tighter', 'tight', 'normal', 'wide', 'wider', 'widest'] },
92
+ // background
93
+ { prefix: 'bg-', values: ['transparent', 'white', 'black', ...COLOR_SCALE_VALUES] },
94
+ // border
95
+ { prefix: 'border-', values: ['0', '1', '2', '4', ...COLOR_SCALE_VALUES] },
96
+ { prefix: 'rounded-', values: ['none', ...SIZE_VALUES, 'full'] },
97
+ // flex
98
+ { prefix: 'flex-', values: ['1', 'auto', 'initial', 'none', 'row', 'col', 'row-reverse', 'col-reverse', 'wrap', 'nowrap'] },
99
+ { prefix: 'items-', values: ['start', 'end', 'center', 'baseline', 'stretch'] },
100
+ { prefix: 'justify-', values: ['start', 'end', 'center', 'between', 'around', 'evenly'] },
101
+ { prefix: 'self-', values: ['auto', 'start', 'end', 'center', 'stretch', 'baseline'] },
102
+ // grid
103
+ { prefix: 'col-span-', values: NUMERIC_1_12 },
104
+ { prefix: 'row-span-', values: NUMERIC_1_12 },
105
+ // opacity
106
+ { prefix: 'opacity-', values: ['0', '25', '50', '75', '100'] },
107
+ // z-index
108
+ { prefix: 'z-', values: ['0', '10', '20', '30', '40', '50', 'auto'] },
109
+ ];
110
+
111
+ /** Atomic literal classes (no value tail) safe to pass through. */
112
+ export const LITERAL_CLASSES: ReadonlySet<string> = new Set([
113
+ 'flex',
114
+ 'inline-flex',
115
+ 'block',
116
+ 'inline-block',
117
+ 'inline',
118
+ 'hidden',
119
+ 'absolute',
120
+ 'relative',
121
+ 'sticky',
122
+ 'fixed',
123
+ 'overflow-hidden',
124
+ 'overflow-visible',
125
+ 'overflow-scroll',
126
+ 'overflow-auto',
127
+ 'overflow-x-hidden',
128
+ 'overflow-y-hidden',
129
+ 'overflow-x-auto',
130
+ 'overflow-y-auto',
131
+ 'rounded',
132
+ 'border',
133
+ 'shadow',
134
+ 'shadow-sm',
135
+ 'shadow-md',
136
+ 'shadow-lg',
137
+ 'shadow-xl',
138
+ 'italic',
139
+ 'underline',
140
+ 'line-through',
141
+ 'no-underline',
142
+ 'uppercase',
143
+ 'lowercase',
144
+ 'capitalize',
145
+ 'normal-case',
146
+ 'truncate',
147
+ 'whitespace-nowrap',
148
+ 'whitespace-pre',
149
+ 'whitespace-pre-wrap',
150
+ 'pointer-events-none',
151
+ 'pointer-events-auto',
152
+ 'select-none',
153
+ 'select-text',
154
+ 'opacity-50',
155
+ 'cursor-pointer',
156
+ 'cursor-default',
157
+ 'cursor-not-allowed',
158
+ ]);
@@ -0,0 +1,90 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * `classifyClass()` — one-shot decision for a single CMS class on mobile.
7
+ *
8
+ * Returns a tuple describing what the renderer should do:
9
+ * - 'allow' — pass the class through unchanged
10
+ * - 'remap' — pass the rewritten class through (`to` is the new value)
11
+ * - 'drop' — do not pass the class through; renderer should warn
12
+ * in development.
13
+ */
14
+
15
+ import { CLASS_PATTERNS, LITERAL_CLASSES } from './allow-list';
16
+ import { findRemap } from './remap';
17
+
18
+ export type TClassDecision =
19
+ | { kind: 'allow'; className: string }
20
+ | { kind: 'remap'; from: string; to: string; reason?: string }
21
+ | { kind: 'drop'; className: string; reason?: string };
22
+
23
+ function matchesPattern(className: string): boolean {
24
+ for (const pattern of CLASS_PATTERNS) {
25
+ if (!className.startsWith(pattern.prefix)) continue;
26
+ const tail = className.slice(pattern.prefix.length);
27
+ if (pattern.values.includes(tail)) return true;
28
+ }
29
+ return false;
30
+ }
31
+
32
+ export function classifyClass(rawClass: string): TClassDecision {
33
+ const className = rawClass.trim();
34
+ if (!className) return { kind: 'drop', className, reason: 'empty class' };
35
+
36
+ // Variant prefixes (`md:`, `dark:`, `hover:`) — strip the prefix
37
+ // before matching, but the remap table can also intercept them.
38
+ const remap = findRemap(className);
39
+ if (remap) {
40
+ if (remap.to === null) {
41
+ return { kind: 'drop', className, reason: remap.reason };
42
+ }
43
+ return { kind: 'remap', from: className, to: remap.to, reason: remap.reason };
44
+ }
45
+
46
+ // arbitrary value tail like `w-[120px]` — pass through, Uniwind handles it.
47
+ if (className.includes('[') && className.endsWith(']')) {
48
+ return { kind: 'allow', className };
49
+ }
50
+
51
+ if (LITERAL_CLASSES.has(className)) {
52
+ return { kind: 'allow', className };
53
+ }
54
+
55
+ if (matchesPattern(className)) {
56
+ return { kind: 'allow', className };
57
+ }
58
+
59
+ return { kind: 'drop', className, reason: 'not in mobile allow-list' };
60
+ }
61
+
62
+ /**
63
+ * Run classifyClass over a whitespace-separated class string and return
64
+ * the resulting list of allowed classes. The optional `onWarn` callback
65
+ * fires for every drop/remap (use this in dev to spot CMS-side issues).
66
+ */
67
+ export function classifyClassString(
68
+ raw: string | null | undefined,
69
+ onWarn?: (decision: Exclude<TClassDecision, { kind: 'allow' }>) => void
70
+ ): string[] {
71
+ if (!raw) return [];
72
+ const out: string[] = [];
73
+ for (const cls of raw.split(/\s+/)) {
74
+ if (!cls) continue;
75
+ const decision = classifyClass(cls);
76
+ switch (decision.kind) {
77
+ case 'allow':
78
+ out.push(decision.className);
79
+ break;
80
+ case 'remap':
81
+ out.push(decision.to);
82
+ if (onWarn) onWarn(decision);
83
+ break;
84
+ case 'drop':
85
+ if (onWarn) onWarn(decision);
86
+ break;
87
+ }
88
+ }
89
+ return out;
90
+ }
@@ -0,0 +1,7 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './allow-list';
6
+ export * from './remap';
7
+ export * from './classify';
@@ -0,0 +1,44 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Web -> mobile class remap. Some Tailwind classes are valid on web but
7
+ * have no mobile equivalent (or have a different name in Uniwind). This
8
+ * table runs BEFORE the allow-list check.
9
+ *
10
+ * Rules of thumb:
11
+ * - drop classes that don't make sense on mobile (`select-text` is a no-op on RN, `cursor-*`, `hover:*`).
12
+ * - rewrite logical pairs (e.g. mobile prefers `text-center` over `text-left` defaults — leave alone).
13
+ * - alias common Mantine-flavored utility names users may type.
14
+ */
15
+
16
+ export interface IClassRemap {
17
+ /** Class to rewrite. */
18
+ from: string;
19
+ /** Replacement class, or null to drop the class entirely. */
20
+ to: string | null;
21
+ /** Optional reason surfaced via dev warning. */
22
+ reason?: string;
23
+ }
24
+
25
+ export const CLASS_REMAP: readonly IClassRemap[] = [
26
+ // hover/focus on mobile only fires for keyboard nav; safe to drop.
27
+ { from: 'hover:bg-gray-100', to: null, reason: 'hover not applicable on mobile' },
28
+ { from: 'hover:underline', to: null, reason: 'hover not applicable on mobile' },
29
+ // cursor classes are no-ops on RN.
30
+ { from: 'cursor-pointer', to: null, reason: 'no cursor on RN' },
31
+ { from: 'cursor-default', to: null, reason: 'no cursor on RN' },
32
+ { from: 'cursor-not-allowed', to: null, reason: 'no cursor on RN' },
33
+ // select-text behaves slightly differently on RN; keep but as a soft no-op.
34
+ { from: 'select-text', to: null, reason: 'unsupported on RN' },
35
+ // legacy aliases
36
+ { from: 'mantine-fw', to: 'w-full' },
37
+ { from: 'mantine-h-full', to: 'h-full' },
38
+ ];
39
+
40
+ const REMAP_MAP = new Map<string, IClassRemap>(CLASS_REMAP.map((r) => [r.from, r]));
41
+
42
+ export function findRemap(className: string): IClassRemap | undefined {
43
+ return REMAP_MAP.get(className);
44
+ }
@@ -0,0 +1,121 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Client-side mirror of the backend `ConditionService::evaluateCondition`.
7
+ * Uses `json-logic-js` so the same JSON-Logic expressions evaluated on
8
+ * the server are evaluated identically here.
9
+ *
10
+ * Backend system variables (see ConditionService docblock):
11
+ * - language: ISO locale code, e.g. 'de-CH'
12
+ * - current_date / current_datetime / current_time
13
+ * - page_keyword
14
+ * - platform: 'web' | 'mobile'
15
+ *
16
+ * Anything else passed in `extraVars` is merged on top.
17
+ */
18
+
19
+ import jsonLogic from 'json-logic-js';
20
+
21
+ export type TPlatform = 'web' | 'mobile';
22
+
23
+ export interface IConditionContext {
24
+ platform: TPlatform;
25
+ language?: string;
26
+ page_keyword?: string;
27
+ current_date?: string;
28
+ current_datetime?: string;
29
+ current_time?: string;
30
+ [extraKey: string]: unknown;
31
+ }
32
+
33
+ export interface IEvaluatedCondition {
34
+ /** Whether the condition allowed the section to render. */
35
+ visible: boolean;
36
+ /** Optional debug payload (only set when `withDebug` is true). */
37
+ debug?: {
38
+ condition: string | null;
39
+ result: boolean;
40
+ variables: Record<string, unknown>;
41
+ error?: string;
42
+ };
43
+ }
44
+
45
+ /**
46
+ * Build a default context with current date/time + the platform
47
+ * variable. Pass any extra vars (data-bound section variables, etc).
48
+ */
49
+ export function buildConditionContext(
50
+ platform: TPlatform,
51
+ extraVars: Record<string, unknown> = {},
52
+ now: Date = new Date()
53
+ ): IConditionContext {
54
+ const pad = (n: number): string => String(n).padStart(2, '0');
55
+ const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
56
+ const time = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
57
+ return {
58
+ platform,
59
+ current_date: date,
60
+ current_time: time,
61
+ current_datetime: `${date} ${time}`,
62
+ ...extraVars,
63
+ };
64
+ }
65
+
66
+ /**
67
+ * Evaluate a JSON-Logic condition string. A null/empty/whitespace
68
+ * condition is treated as `visible: true` (no condition).
69
+ */
70
+ export function evaluateCondition(
71
+ condition: string | null | undefined,
72
+ context: IConditionContext,
73
+ options: { withDebug?: boolean } = {}
74
+ ): IEvaluatedCondition {
75
+ const trimmed = (condition ?? '').toString().trim();
76
+ if (!trimmed) {
77
+ return options.withDebug
78
+ ? { visible: true, debug: { condition: null, result: true, variables: { ...context } } }
79
+ : { visible: true };
80
+ }
81
+
82
+ let parsed: unknown;
83
+ try {
84
+ parsed = JSON.parse(trimmed);
85
+ } catch (err) {
86
+ return options.withDebug
87
+ ? {
88
+ visible: false,
89
+ debug: {
90
+ condition: trimmed,
91
+ result: false,
92
+ variables: { ...context },
93
+ error: `Invalid JSON: ${(err as Error).message}`,
94
+ },
95
+ }
96
+ : { visible: false };
97
+ }
98
+
99
+ try {
100
+ // jsonLogic typings are loose; the result of any boolean rule is
101
+ // a boolean, but rules like {var: 'x'} can return raw values, so
102
+ // we coerce.
103
+ const raw = jsonLogic.apply(parsed as jsonLogic.RulesLogic, context as Record<string, unknown>);
104
+ const visible = Boolean(raw);
105
+ return options.withDebug
106
+ ? { visible, debug: { condition: trimmed, result: visible, variables: { ...context } } }
107
+ : { visible };
108
+ } catch (err) {
109
+ return options.withDebug
110
+ ? {
111
+ visible: false,
112
+ debug: {
113
+ condition: trimmed,
114
+ result: false,
115
+ variables: { ...context },
116
+ error: (err as Error).message,
117
+ },
118
+ }
119
+ : { visible: false };
120
+ }
121
+ }
@@ -0,0 +1,5 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './evaluate';
package/src/index.ts ADDED
@@ -0,0 +1,25 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * @selfhelp/shared — public surface.
7
+ *
8
+ * Each subpath also has its own export:
9
+ * - `@selfhelp/shared/registry`
10
+ * - `@selfhelp/shared/theme`
11
+ * - `@selfhelp/shared/tailwind`
12
+ *
13
+ * The default export aggregates everything so consumers can do:
14
+ * import { STYLE_REGISTRY, replaceCalcedValues, ENDPOINTS } from '@selfhelp/shared';
15
+ */
16
+
17
+ export * from './types';
18
+ export * from './registry';
19
+ export * from './api';
20
+ export * from './theme';
21
+ export * from './interpolation';
22
+ export * from './condition';
23
+ export * from './cms-classes';
24
+ export * from './assets';
25
+ export * from './utils';
@@ -0,0 +1,5 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './replaceCalcedValues';
@@ -0,0 +1,27 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Mirrors the backend `PageDb::replace_calced_values()` regex:
7
+ *
8
+ * ~{{({{)?(.*?)(}})?}}~s
9
+ *
10
+ * Replaces every `{{name}}` or `{{{{name}}}}` occurrence in the input
11
+ * string with `values[name]`. Unknown placeholders are left intact so
12
+ * editors notice them and so we never accidentally render `undefined`.
13
+ */
14
+
15
+ const PLACEHOLDER_RE = /{{({{)?([^{}]+?)(}})?}}/g;
16
+
17
+ export type TCalcedValues = Record<string, string | number | boolean | null | undefined>;
18
+
19
+ export function replaceCalcedValues(input: string | null | undefined, values: TCalcedValues): string {
20
+ if (input === null || input === undefined) return '';
21
+ return String(input).replace(PLACEHOLDER_RE, (full, _open, key) => {
22
+ const trimmed = String(key).trim();
23
+ const value = values[trimmed];
24
+ if (value === undefined || value === null) return full;
25
+ return String(value);
26
+ });
27
+ }
@@ -0,0 +1,37 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import type { TStyleName } from '../types/styles/unknown';
6
+ import { STYLE_REGISTRY } from './styles.registry';
7
+
8
+ export * from './styles.registry';
9
+
10
+ /**
11
+ * Helper for renderers: produces a typed map shape that requires every
12
+ * registry key to be implemented. Pass this to your dispatcher and let
13
+ * TypeScript enforce 100% style coverage.
14
+ *
15
+ * type IStyleImpls = TStyleImplMap<React.FC<{ style: TStyle }>>;
16
+ *
17
+ * `keyof TStyleImplMap<...>` is exactly the union of style names.
18
+ */
19
+ export type TStyleImplMap<TImpl> = {
20
+ readonly [K in TStyleName]: TImpl;
21
+ };
22
+
23
+ /**
24
+ * Get an entry from the registry safely. Returns undefined if the
25
+ * style_name isn't registered (the renderer should fall back to
26
+ * UnknownStyle).
27
+ */
28
+ export function getRegistryEntry(name: string): (typeof STYLE_REGISTRY)[TStyleName] | undefined {
29
+ return (STYLE_REGISTRY as Record<string, (typeof STYLE_REGISTRY)[TStyleName]>)[name];
30
+ }
31
+
32
+ /**
33
+ * Type guard to narrow a string to TStyleName.
34
+ */
35
+ export function isKnownStyleName(name: string): name is TStyleName {
36
+ return Object.prototype.hasOwnProperty.call(STYLE_REGISTRY, name);
37
+ }