@parhelia/head 0.0.0-stage → 1.0.12903

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/src/typo3.js ADDED
@@ -0,0 +1,110 @@
1
+ /**
2
+ * `@parhelia/head/typo3` — **the TYPO3 marker names, and the helpers a head renders them with** (TYPO3 plan TY-D15). This module is
3
+ * the one JavaScript source: the editor's `typo3` vocabulary (`@parhelia/core`, `bridge/vocabularies/typo3/`) reads them by importing
4
+ * this module, a headless TYPO3 head renders them, the Parhelia connector inside TYPO3 writes them in its editing renders
5
+ * (`Markup\FieldMarker`, `Markup\ComponentSentinel`, `Markup\SlotSentinel`), and the TYPO3 store answers them
6
+ * (`Parhelia.Typo3.Protocol.MarkerNames`, `RecordAddress`). All of them are pinned to one test vector,
7
+ * `providers/typo3/protocol-vectors/typo3.markers.json` (the connector wire's vectors, which the connector's image build and the
8
+ * provider's tests read too): a renamed attribute fails a test in each language.
9
+ *
10
+ * Fields carry the **TYPO3 vocabulary** (store-resolved: a render knows the record and the column, never Parhelia's field ids, so the
11
+ * page's store answers which item and field a marker names):
12
+ *
13
+ * data-t3-table="<TCA table>" data-t3-uid="<uid>" the record (entity kind `record`, key `{table}:{uid}`); the uid is the item's,
14
+ * so a translation row is named by its parent
15
+ * data-t3-field="<column>" the field (the TCA column)
16
+ * data-t3-language="<language id>" the language of the row that was rendered (-1: a record of all languages)
17
+ * data-t3-richtext on a rich text field's element
18
+ *
19
+ * The language id is TYPO3's, not a Parhelia locale: an edit writes in the page's locale, which the canvas knows.
20
+ *
21
+ * Components and slots use Parhelia's own sentinels, written by the connector: a component is a `data-component-start` / `-end`
22
+ * script pair carrying the record's Guid (its Parhelia item id) beside its native id (`data-t3-table`, `data-t3-uid`,
23
+ * `data-t3-language`); a column is the slot `data-placeholder-start="<colPos>"`.
24
+ *
25
+ * Markers appear in Parhelia's editing renders only; every other render is TYPO3's own markup.
26
+ */
27
+
28
+ /** The vocabulary's wire name: what the canvas sends with the markers it found, and what the TYPO3 store states. */
29
+ export const TYPO3_MARKER_VOCABULARY = "typo3";
30
+
31
+ /** The one entity kind a field marker names: a TYPO3 record. */
32
+ export const T3_RECORD_KIND = "record";
33
+
34
+ export const T3_TABLE_ATTRIBUTE = "data-t3-table";
35
+ export const T3_UID_ATTRIBUTE = "data-t3-uid";
36
+ export const T3_FIELD_ATTRIBUTE = "data-t3-field";
37
+ export const T3_LANGUAGE_ATTRIBUTE = "data-t3-language";
38
+ export const T3_RICHTEXT_ATTRIBUTE = "data-t3-richtext";
39
+
40
+ /** The language id of a record of all languages. */
41
+ export const T3_ALL_LANGUAGES = "-1";
42
+
43
+ /** Parhelia addresses a TYPO3 record as its current state in the mount's workspace (version 0). */
44
+ export const PARHELIA_TYPO3_VERSION = "0";
45
+
46
+ const TABLE = /^[a-z0-9_]{1,64}$/;
47
+ const UID = /^[1-9][0-9]{0,9}$/;
48
+ const MAX_UID = 2147483647;
49
+ const LANGUAGE = /^(?:-1|0|[1-9][0-9]{0,9})$/;
50
+
51
+ /**
52
+ * The uid as TYPO3 spells it, or undefined when it is none (positive decimal, no sign or leading zero, at most 2147483647).
53
+ * @param {string | number} uid
54
+ * @returns {string | undefined}
55
+ */
56
+ const uidOf = uid => {
57
+ const text = typeof uid === "number" && Number.isInteger(uid) ? String(uid) : typeof uid === "string" ? uid : "";
58
+ return UID.test(text) && Number(text) <= MAX_UID ? text : undefined;
59
+ };
60
+
61
+ /**
62
+ * The key of a record marker, `{table}:{uid}`; undefined for a table that is no TCA table name or a uid that is no record's.
63
+ * Nothing is folded: a capital letter, a sign, a space or a leading zero is no key.
64
+ * @param {string} table
65
+ * @param {string | number} uid
66
+ * @returns {string | undefined}
67
+ */
68
+ export const t3RecordKey = (table, uid) => {
69
+ const digits = uidOf(uid);
70
+ return typeof table === "string" && TABLE.test(table) && digits ? `${table}:${digits}` : undefined;
71
+ };
72
+
73
+ /**
74
+ * Reads a record key `{table}:{uid}`; undefined when it is not one.
75
+ * @param {string} key
76
+ * @returns {{ table: string; uid: string } | undefined}
77
+ */
78
+ export const parseT3RecordKey = key => {
79
+ if (typeof key !== "string") return undefined;
80
+ const colon = key.indexOf(":");
81
+ if (colon <= 0 || colon !== key.lastIndexOf(":")) return undefined;
82
+ const table = key.slice(0, colon);
83
+ const uid = key.slice(colon + 1);
84
+ return t3RecordKey(table, uid) === key ? { table, uid } : undefined;
85
+ };
86
+
87
+ /**
88
+ * The field marker attributes of one rendered field, in the connector's order; `{}` outside the editor. Throws for a malformed record,
89
+ * field name or language.
90
+ * @param {import("./typo3.js").T3FieldRef} ref
91
+ * @param {boolean} edit
92
+ * @returns {Record<string, string>}
93
+ */
94
+ export const t3FieldAttributes = (ref, edit) => {
95
+ if (!edit) return {};
96
+ const key = t3RecordKey(ref.table, ref.uid);
97
+ if (!key) throw new TypeError(`A TYPO3 field marker needs a table and a uid; got ${JSON.stringify([ref.table, ref.uid])}.`);
98
+ if (!ref.field || !TABLE.test(ref.field)) throw new TypeError(`A TYPO3 field marker needs the column's name; got ${JSON.stringify(ref.field)}.`);
99
+ const language = String(ref.language);
100
+ if (!LANGUAGE.test(language)) throw new TypeError(`A TYPO3 field marker needs a language id; got ${JSON.stringify(ref.language)}.`);
101
+ /** @type {Record<string, string>} */
102
+ const attributes = {
103
+ [T3_TABLE_ATTRIBUTE]: ref.table,
104
+ [T3_UID_ATTRIBUTE]: /** @type {string} */ (uidOf(ref.uid)),
105
+ [T3_FIELD_ATTRIBUTE]: ref.field,
106
+ [T3_LANGUAGE_ATTRIBUTE]: language,
107
+ };
108
+ if (ref.richText) attributes[T3_RICHTEXT_ATTRIBUTE] = "";
109
+ return attributes;
110
+ };
@@ -0,0 +1,51 @@
1
+ /** The item kinds an Umbraco field marker names. */
2
+ export type UmbItemKind = "document" | "block" | "media";
3
+
4
+ /** One rendered field's identity, as a head knows it. */
5
+ export type UmbFieldRef = {
6
+ kind: UmbItemKind;
7
+ /** The item's key as the Delivery API spells it (a GUID). */
8
+ key: string;
9
+ /** The property alias. */
10
+ property: string;
11
+ /** The rendered culture for a culture-variant property; null for an invariant one (`*`). */
12
+ culture: string | null;
13
+ /** The culture whose value is shown instead (language fallback), when it is not the rendered culture's own. */
14
+ fallbackFrom?: string | null;
15
+ };
16
+
17
+ /** The vocabulary's wire name. */
18
+ export declare const UMBRACO_MARKER_VOCABULARY: "umbraco";
19
+ /** The item kinds a field marker names. */
20
+ export declare const UMB_ITEM_KINDS: readonly UmbItemKind[];
21
+ /** The attribute naming the item of each kind. */
22
+ export declare const UMB_KIND_ATTRIBUTE: Readonly<Record<UmbItemKind, string>>;
23
+ export declare const UMB_PROPERTY_ATTRIBUTE: string;
24
+ export declare const UMB_CULTURE_ATTRIBUTE: string;
25
+ export declare const UMB_FALLBACK_ATTRIBUTE: string;
26
+ export declare const UMB_RICHTEXT_ATTRIBUTE: string;
27
+ export declare const UMB_REFUSED_ATTRIBUTE: string;
28
+ export declare const UMB_CONTENT_TYPE_ATTRIBUTE: string;
29
+ /** The culture a field marker states for an invariant property. */
30
+ export declare const INVARIANT_CULTURE: "*";
31
+ /** Parhelia addresses an Umbraco item as its current state. */
32
+ export declare const PARHELIA_UMBRACO_VERSION: "0";
33
+ /** Umbraco's rich text block anchor tags (Delivery markup). */
34
+ export declare const UMB_RTE_BLOCK_TAGS: readonly string[];
35
+ /** The anchor's block key attribute (Delivery markup). */
36
+ export declare const UMB_RTE_BLOCK_KEY_ATTRIBUTE: string;
37
+
38
+ /** The field marker attributes of one rendered field; `{}` outside the editor. Throws for a malformed key or alias. */
39
+ export declare function umbFieldAttributes(ref: UmbFieldRef, edit: boolean): Record<string, string>;
40
+ /** The attribute saying a field element holds rich text; `{}` outside the editor. */
41
+ export declare function umbRichTextAttributes(edit: boolean): Record<string, string>;
42
+ /** The attribute naming a picked media item on its rendered image; `{}` outside the editor. */
43
+ export declare function umbPickedMediaAttributes(mediaKey: string, edit: boolean): Record<string, string>;
44
+ /** The attribute naming why a property is not shown. */
45
+ export declare function umbRefusedAttributes(refusal: string): Record<string, string>;
46
+ /** The root slot key of a Block List property or a Block Grid: the property alias. */
47
+ export declare function rootSlotKey(propertyAlias: string): string;
48
+ /** The slot key of a Block Grid area: `{areaAlias}_{OWNER-BLOCK-KEY}`. */
49
+ export declare function areaSlotKey(areaAlias: string, ownerBlockKey: string): string;
50
+ /** The attributes of a block's component-start script. */
51
+ export declare function componentStartAttributes(blockKey: string, contentType: string, culture: string): Record<string, string>;
package/src/umbraco.js ADDED
@@ -0,0 +1,146 @@
1
+ /**
2
+ * `@parhelia/head/umbraco` — **the Umbraco marker names, and the helpers a head renders them with** (Umbraco program
3
+ * umb-vocab). This module is the one source: an Umbraco head (the Next.js reference head imports it) renders these
4
+ * attributes, the editor's `umbraco` vocabulary (`@parhelia/core`, `bridge/vocabularies/umbraco/`) reads them by importing
5
+ * this module, and the Razor tag helpers of the Parhelia.Umbraco package take the same names from
6
+ * `Parhelia.Umbraco.Protocol.ParheliaMarkerNames`. Both languages are pinned to one test vector,
7
+ * `umbraco.markers.json` beside this file: a renamed attribute fails a test in each language.
8
+ *
9
+ * Fields carry the **Umbraco vocabulary** (store-resolved: the Delivery API answers property aliases and element keys, never
10
+ * Parhelia's field ids, so the page's store answers which item and field a marker names):
11
+ *
12
+ * data-umb-document="<document key>" | data-umb-block="<block content key>" | data-umb-media="<media key>"
13
+ * data-umb-property="<property alias>"
14
+ * data-umb-culture="<culture>" (a culture-variant property) | "*" (an invariant property)
15
+ * data-umb-fallback="<culture>" only when the value shown is the fallback culture's (language fallback)
16
+ * data-umb-richtext on a rich text field's element
17
+ * data-umb-refused="<name>" a property the package refused to render (never a field)
18
+ *
19
+ * Components and slots use Parhelia's own sentinels: a block is a component (`data-component-start` / `-end` scripts
20
+ * carrying the block's content key, which is its Parhelia item id; no `data-typeid`: the store answers it from the
21
+ * composition); a Block List property and a Block Grid root are slots keyed by the property alias, a Block Grid area is the
22
+ * slot `{areaAlias}_{OWNER-BLOCK-KEY}` (owner = the block holding the area, its content key uppercase and braced).
23
+ *
24
+ * A head renders every marker in Parhelia's editing frames only (`edit`); outside them the attributes are absent.
25
+ */
26
+
27
+ /** The vocabulary's wire name: what the canvas sends with the markers it found, and what the Umbraco store states. */
28
+ export const UMBRACO_MARKER_VOCABULARY = "umbraco";
29
+
30
+ /** The item kinds a field marker names (the entity kind the canvas sends). */
31
+ export const UMB_ITEM_KINDS = Object.freeze(/** @type {const} */ (["document", "block", "media"]));
32
+
33
+ /** The attribute naming the item of each kind (exactly one per field element). */
34
+ export const UMB_KIND_ATTRIBUTE = Object.freeze({
35
+ document: "data-umb-document",
36
+ block: "data-umb-block",
37
+ media: "data-umb-media",
38
+ });
39
+ export const UMB_PROPERTY_ATTRIBUTE = "data-umb-property";
40
+ export const UMB_CULTURE_ATTRIBUTE = "data-umb-culture";
41
+ export const UMB_FALLBACK_ATTRIBUTE = "data-umb-fallback";
42
+ export const UMB_RICHTEXT_ATTRIBUTE = "data-umb-richtext";
43
+ export const UMB_REFUSED_ATTRIBUTE = "data-umb-refused";
44
+ export const UMB_CONTENT_TYPE_ATTRIBUTE = "data-umb-content-type";
45
+
46
+ /** The culture a field marker states for an invariant property. */
47
+ export const INVARIANT_CULTURE = "*";
48
+
49
+ /** Parhelia addresses an Umbraco item as its current state (version 0): Umbraco keeps no numbered versions for it. */
50
+ export const PARHELIA_UMBRACO_VERSION = "0";
51
+
52
+ /**
53
+ * Umbraco's own spelling of a rich text block anchor in the Delivery API's markup (`<umb-rte-block data-content-id>`; the
54
+ * stored form spells the key `data-content-key`). A head keeps the anchor and renders the block inside it.
55
+ */
56
+ export const UMB_RTE_BLOCK_TAGS = Object.freeze(/** @type {const} */ (["umb-rte-block", "umb-rte-block-inline"]));
57
+ export const UMB_RTE_BLOCK_KEY_ATTRIBUTE = "data-content-id";
58
+
59
+ const GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
60
+ const ALIAS = /^[A-Za-z_][A-Za-z0-9_]*$/;
61
+
62
+ /**
63
+ * The field marker attributes of one rendered field; `{}` outside the editor. Throws for a malformed key or alias.
64
+ * @param {import("./umbraco.js").UmbFieldRef} ref
65
+ * @param {boolean} edit
66
+ * @returns {Record<string, string>}
67
+ */
68
+ export const umbFieldAttributes = (ref, edit) => {
69
+ if (!edit) return {};
70
+ if (!GUID.test(ref.key)) throw new TypeError(`An Umbraco field marker needs a GUID key; got ${JSON.stringify(ref.key)}.`);
71
+ if (!ALIAS.test(ref.property)) throw new TypeError(`An Umbraco field marker needs a property alias; got ${JSON.stringify(ref.property)}.`);
72
+ /** @type {Record<string, string>} */
73
+ const attributes = {
74
+ [UMB_KIND_ATTRIBUTE[ref.kind]]: ref.key.toLowerCase(),
75
+ [UMB_PROPERTY_ATTRIBUTE]: ref.property,
76
+ [UMB_CULTURE_ATTRIBUTE]: ref.culture ?? INVARIANT_CULTURE,
77
+ };
78
+ if (ref.fallbackFrom && ref.fallbackFrom !== ref.culture) attributes[UMB_FALLBACK_ATTRIBUTE] = ref.fallbackFrom;
79
+ return attributes;
80
+ };
81
+
82
+ /**
83
+ * The attribute saying a field element holds rich text; `{}` outside the editor.
84
+ * @param {boolean} edit
85
+ * @returns {Record<string, string>}
86
+ */
87
+ export const umbRichTextAttributes = edit => (edit ? { [UMB_RICHTEXT_ATTRIBUTE]: "" } : {});
88
+
89
+ /**
90
+ * The attribute naming a picked media item on its rendered image (not a field marker: no property); `{}` outside the editor.
91
+ * @param {string} mediaKey
92
+ * @param {boolean} edit
93
+ * @returns {Record<string, string>}
94
+ */
95
+ export const umbPickedMediaAttributes = (mediaKey, edit) => (edit ? { [UMB_KIND_ATTRIBUTE.media]: mediaKey } : {});
96
+
97
+ /**
98
+ * The attribute naming why a property is not shown (the package's refusal, e.g. `unattributable-converter`): rendered for
99
+ * every request, never a field.
100
+ * @param {string} refusal
101
+ * @returns {Record<string, string>}
102
+ */
103
+ export const umbRefusedAttributes = refusal => ({ [UMB_REFUSED_ATTRIBUTE]: refusal });
104
+
105
+ /**
106
+ * The root slot key of a Block List property or a Block Grid: the property alias.
107
+ * @param {string} propertyAlias
108
+ * @returns {string}
109
+ */
110
+ export const rootSlotKey = propertyAlias => {
111
+ if (!propertyAlias) throw new TypeError("A slot key needs a property alias.");
112
+ return propertyAlias;
113
+ };
114
+
115
+ /**
116
+ * The slot key of a Block Grid area: `{areaAlias}_{OWNER-BLOCK-KEY}` (the owning block's key, uppercase and braced).
117
+ * @param {string} areaAlias
118
+ * @param {string} ownerBlockKey
119
+ * @returns {string}
120
+ */
121
+ export const areaSlotKey = (areaAlias, ownerBlockKey) => {
122
+ if (!areaAlias) throw new TypeError("An area slot key needs the area alias.");
123
+ if (!GUID.test(ownerBlockKey))
124
+ throw new TypeError(`An area slot key needs the owner block's GUID key; got ${JSON.stringify(ownerBlockKey)}.`);
125
+ return `${areaAlias}_{${ownerBlockKey.toUpperCase()}}`;
126
+ };
127
+
128
+ /**
129
+ * The attributes of a block's component-start script.
130
+ * @param {string} blockKey
131
+ * @param {string} contentType
132
+ * @param {string} culture
133
+ * @returns {Record<string, string>}
134
+ */
135
+ export const componentStartAttributes = (blockKey, contentType, culture) => {
136
+ if (!GUID.test(blockKey)) throw new TypeError(`A component boundary needs the block's GUID key; got ${JSON.stringify(blockKey)}.`);
137
+ const key = blockKey.toLowerCase();
138
+ return {
139
+ "data-component-start": key,
140
+ "data-itemid": key,
141
+ "data-component-name": contentType,
142
+ [UMB_CONTENT_TYPE_ATTRIBUTE]: contentType,
143
+ "data-language": culture,
144
+ "data-version": PARHELIA_UMBRACO_VERSION,
145
+ };
146
+ };
package/src/vite.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ /**
4
+ * The Vite plugin of a rendering head: `base` below the rendering proxy, `PARHELIA_BASE_PATH` / `PARHELIA_ORIGIN`
5
+ * inlined into browser and server code, this package bundled for the server.
6
+ */
7
+ export declare function parhelia(options?: { env?: Readonly<Record<string, string | undefined>> }): Plugin;
8
+
9
+ /** What the plugin adds to a Vite config (`base` only when `configuredBase` is undefined). */
10
+ export declare function parheliaViteSettings(
11
+ env: Readonly<Record<string, string | undefined>>,
12
+ configuredBase: string | undefined,
13
+ ): {
14
+ base?: string;
15
+ define: Record<string, string>;
16
+ optimizeDeps: { exclude: string[] };
17
+ ssr: { noExternal: string[] };
18
+ };
package/src/vite.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * `@parhelia/head/vite` — `parhelia()`, the Vite plugin of a rendering head on a Vite-based framework (TanStack Start,
3
+ * React Router, plain Vite SSR). The counterpart of `withParhelia` (`@parhelia/next/config`):
4
+ *
5
+ * - `base`: the head is served below Parhelia's rendering proxy (`PARHELIA_RENDERING_PROXY_PATH`), assets included. A
6
+ * `base` the config sets itself is kept. The plugin runs first (`enforce: "pre"`; list it before the framework's), so
7
+ * a framework that derives its router's base path from `base` while it is configured sees it (TanStack Start does).
8
+ * - `define`: `process.env.PARHELIA_BASE_PATH` / `PARHELIA_ORIGIN` are inlined into the browser's and the server's code,
9
+ * so `withBasePath`, the bridge and `contentfulLivePreviewOrigins` read the build's values on both sides. Only
10
+ * these two: the editor secret and the preview key stay server-side environment variables.
11
+ * - The Parhelia head packages are bundled for the server (`ssr.noExternal`) and not pre-bundled (`optimizeDeps.exclude`),
12
+ * so the defines reach their code.
13
+ *
14
+ * The CSP framing rule for a directly framed head is the server's response header, outside the build: allow
15
+ * `PARHELIA_ORIGIN` in `frame-ancestors` where the head sets its headers.
16
+ */
17
+ import settings from "./settings.cjs";
18
+
19
+ /** The packages that read the build values: bundled for the server and not pre-bundled, so the defines reach them. */
20
+ const PACKAGE_NAMES = ["@parhelia/head", "@parhelia/react", "@parhelia/astro"];
21
+
22
+ /**
23
+ * What the plugin adds to a Vite config (`base` only when the config sets none): also used by the Astro integration,
24
+ * which owns `base` itself.
25
+ * @param {Readonly<Record<string, string | undefined>>} env
26
+ * @param {string | undefined} configuredBase
27
+ */
28
+ export function parheliaViteSettings(env, configuredBase) {
29
+ const origin = settings.originFromEnv(env);
30
+ const basePath = settings.basePathFromEnv(env);
31
+ return {
32
+ ...(basePath && configuredBase === undefined ? { base: `${basePath}/` } : {}),
33
+ define: {
34
+ "process.env.PARHELIA_BASE_PATH": JSON.stringify(basePath),
35
+ "process.env.PARHELIA_ORIGIN": JSON.stringify(origin),
36
+ },
37
+ optimizeDeps: { exclude: PACKAGE_NAMES },
38
+ ssr: { noExternal: PACKAGE_NAMES },
39
+ };
40
+ }
41
+
42
+ /** @param {{ env?: Readonly<Record<string, string | undefined>> }} [options] */
43
+ export function parhelia(options = {}) {
44
+ const env = options.env ?? process.env;
45
+ // Read once, at once: a value that is not an origin or a path fails the build where it is configured.
46
+ parheliaViteSettings(env, undefined);
47
+ return {
48
+ name: "parhelia",
49
+ enforce: "pre",
50
+ /** @param {{ base?: string }} config */
51
+ config: (config) => parheliaViteSettings(env, config.base),
52
+ };
53
+ }