@parhelia/head 0.0.0-stage → 1.0.12951
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 +8 -0
- package/README.md +231 -2
- package/package.json +133 -4
- package/src/bridge-core.d.ts +97 -0
- package/src/bridge-core.js +173 -0
- package/src/contentful.d.ts +34 -0
- package/src/contentful.js +213 -0
- package/src/env.d.ts +11 -0
- package/src/env.js +47 -0
- package/src/identity.d.ts +45 -0
- package/src/identity.js +137 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +13 -0
- package/src/magnolia.d.ts +60 -0
- package/src/magnolia.js +199 -0
- package/src/picture-crops.d.ts +168 -0
- package/src/picture-crops.js +355 -0
- package/src/refresh-dom.d.ts +9 -0
- package/src/refresh-dom.js +92 -0
- package/src/refresh.d.ts +14 -0
- package/src/refresh.js +63 -0
- package/src/rich-text-reader.js +263 -0
- package/src/rich-text.d.ts +17 -0
- package/src/rich-text.js +354 -0
- package/src/server.d.ts +38 -0
- package/src/server.js +113 -0
- package/src/settings.cjs +35 -0
- package/src/settings.d.cts +7 -0
- package/src/sitecore.d.ts +53 -0
- package/src/sitecore.js +228 -0
- package/src/slot-markup.d.ts +5 -0
- package/src/slot-markup.js +24 -0
- package/src/typo3.d.ts +34 -0
- package/src/typo3.js +110 -0
- package/src/umbraco.d.ts +51 -0
- package/src/umbraco.js +146 -0
- package/src/vite.d.ts +18 -0
- package/src/vite.js +53 -0
package/src/server.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/head/server` — the editor signal of a rendering head (server-side only: `getServerSideProps`, route handlers,
|
|
3
|
+
* server components, a proxy). Framework-neutral: it reads a request's headers whatever their shape (a Node request, a Web
|
|
4
|
+
* `Request`/`NextRequest`, `headers()` of the App Router) and a query as the Pages Router, the App Router or
|
|
5
|
+
* `URLSearchParams` hand it over.
|
|
6
|
+
*
|
|
7
|
+
* A request is the Parhelia editor's canvas when it arrives through Parhelia's rendering proxy, which forwards requests only
|
|
8
|
+
* for a signed-in editor and stamps them with `X-Parhelia-Editor: <editorSecret>` from the host entry
|
|
9
|
+
* (`parhelia.host.{name}`; the proxy drops whatever the browser sent under that name), and the head's
|
|
10
|
+
* `PARHELIA_EDITOR_SECRET` equals it — or when the request carries a live Parhelia preview session (its ticket was redeemed,
|
|
11
|
+
* signed with the head's preview key, for a code only the editor mints; `@parhelia/preview-ticket`). Query parameters (`parhelia=1`)
|
|
12
|
+
* only say WHICH editor request this is, never WHETHER it is one: a visitor can type them. No secret configured and no
|
|
13
|
+
* session = never the editor.
|
|
14
|
+
*/
|
|
15
|
+
import { isParheliaFrameRequest } from "./identity.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The request header Parhelia's rendering proxy carries the host entry's `editorSecret` in (lower case, as Node keys it).
|
|
19
|
+
* The name is the preview protocol's `editorSignalHeader` (`@parhelia/preview-ticket`'s `fixtures/preview-protocol.json`,
|
|
20
|
+
* .NET `PreviewProtocol.EditorSignalHeader`), pinned by `server.test.ts`.
|
|
21
|
+
*/
|
|
22
|
+
export const EDITOR_SIGNAL_HEADER = "x-parhelia-editor";
|
|
23
|
+
|
|
24
|
+
/** The environment variable a head keeps the host entry's `editorSecret` in (read at call time, never at import). */
|
|
25
|
+
export const EDITOR_SECRET_ENV = "PARHELIA_EDITOR_SECRET";
|
|
26
|
+
|
|
27
|
+
/** @param {() => string | undefined} value */
|
|
28
|
+
const read = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
return value() || "";
|
|
31
|
+
} catch {
|
|
32
|
+
return ""; // no `process` in this runtime
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** The head's configured editor secret, "" when none. */
|
|
37
|
+
const configuredEditorSecret = () => read(() => process.env[EDITOR_SECRET_ENV]);
|
|
38
|
+
|
|
39
|
+
const encoder = new TextEncoder();
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Equal strings, compared in time that depends only on their lengths (Web APIs only, so it runs on Node and the Edge alike).
|
|
43
|
+
* @param {string} supplied
|
|
44
|
+
* @param {string} expected
|
|
45
|
+
*/
|
|
46
|
+
const constantTimeEquals = (supplied, expected) => {
|
|
47
|
+
const a = encoder.encode(supplied);
|
|
48
|
+
const b = encoder.encode(expected);
|
|
49
|
+
if (a.length !== b.length) return false;
|
|
50
|
+
let difference = 0;
|
|
51
|
+
for (let index = 0; index < a.length; index++) difference |= /** @type {number} */ (a[index]) ^ /** @type {number} */ (b[index]);
|
|
52
|
+
return difference === 0;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* True only when a secret is configured and the header carries exactly that value (the first one when it was sent twice).
|
|
57
|
+
* @param {string | readonly string[] | null | undefined} headerValue
|
|
58
|
+
* @param {string | null | undefined} configuredSecret
|
|
59
|
+
*/
|
|
60
|
+
export function editorSignalAccepted(headerValue, configuredSecret) {
|
|
61
|
+
const secret = configuredSecret?.trim();
|
|
62
|
+
if (!secret) return false;
|
|
63
|
+
const supplied = Array.isArray(headerValue) ? headerValue[0] : headerValue;
|
|
64
|
+
return typeof supplied === "string" && constantTimeEquals(supplied, secret);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The editor signal header of a request: a Node request or `IncomingHttpHeaders`-like record, a Web `Request`, or a
|
|
69
|
+
* `Headers` (`await headers()` in the App Router).
|
|
70
|
+
* @param {{ headers?: unknown } | Headers | null | undefined} request
|
|
71
|
+
* @returns {string | readonly string[] | undefined}
|
|
72
|
+
*/
|
|
73
|
+
const signalHeaderOf = (request) => {
|
|
74
|
+
if (!request) return undefined;
|
|
75
|
+
const headers = typeof (/** @type {Headers} */ (request).get) === "function" ? request : /** @type {{ headers?: unknown }} */ (request).headers;
|
|
76
|
+
if (!headers || typeof headers !== "object") return undefined;
|
|
77
|
+
if (typeof (/** @type {Headers} */ (headers).get) === "function") return /** @type {Headers} */ (headers).get(EDITOR_SIGNAL_HEADER) ?? undefined;
|
|
78
|
+
const record = /** @type {Record<string, string | readonly string[] | undefined>} */ (headers);
|
|
79
|
+
if (EDITOR_SIGNAL_HEADER in record) return record[EDITOR_SIGNAL_HEADER];
|
|
80
|
+
const name = Object.keys(record).find((key) => key.toLowerCase() === EDITOR_SIGNAL_HEADER);
|
|
81
|
+
return name === undefined ? undefined : record[name];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Whether the request carries the rendering proxy's signal for this head's secret (`PARHELIA_EDITOR_SECRET` unless given).
|
|
86
|
+
* @param {{ headers?: unknown } | Headers | null | undefined} request
|
|
87
|
+
* @param {string | null | undefined} [configuredSecret]
|
|
88
|
+
*/
|
|
89
|
+
export function hasEditorSignal(request, configuredSecret = configuredEditorSecret()) {
|
|
90
|
+
return editorSignalAccepted(signalHeaderOf(request), configuredSecret);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether the query names one of Parhelia's frames: `parhelia=1` (`isParheliaFrameRequest`), which every edit URL the editor builds
|
|
95
|
+
* carries (a CMS's own edit-mode parameters are that CMS's heads' business, not this SDK's).
|
|
96
|
+
* @param {URLSearchParams | Record<string, string | string[] | undefined> | null | undefined} query
|
|
97
|
+
*/
|
|
98
|
+
export function isEditorFrameQuery(query) {
|
|
99
|
+
if (!query) return false;
|
|
100
|
+
return isParheliaFrameRequest(query);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The editing decision of a request: one of Parhelia's frames (query) that carries the proxy's signal for this head's
|
|
105
|
+
* secret, or a live preview session. Only then does a head render markers, the rendered coordinate and empty components.
|
|
106
|
+
* @param {{ headers?: unknown } | Headers | null | undefined} request
|
|
107
|
+
* @param {URLSearchParams | Record<string, string | string[] | undefined> | null | undefined} query
|
|
108
|
+
* @param {boolean} hasPreviewSession
|
|
109
|
+
* @param {string | null | undefined} [configuredSecret]
|
|
110
|
+
*/
|
|
111
|
+
export function isEditingRequest(request, query, hasPreviewSession, configuredSecret = configuredEditorSecret()) {
|
|
112
|
+
return isEditorFrameQuery(query) && (hasPreviewSession || hasEditorSignal(request, configuredSecret));
|
|
113
|
+
}
|
package/src/settings.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `@parhelia/head/settings` — the build settings a head's framework adapter reads while the build is configured
|
|
4
|
+
* (`withParhelia` of `@parhelia/next/config`, the `parhelia()` Vite plugin of `@parhelia/head/vite`). CommonJS so a
|
|
5
|
+
* `next.config.js` can `require` it as well as an ES module can import it.
|
|
6
|
+
*
|
|
7
|
+
* PARHELIA_RENDERING_PROXY_PATH `/parhelia/proxy/<head name>`: served below Parhelia's same-origin rendering proxy
|
|
8
|
+
* (the base path). `NEXT_PUBLIC_BASE_PATH` is read when it is unset; `/` means none.
|
|
9
|
+
* PARHELIA_ORIGIN the Parhelia editor's origin: it may frame the head directly, and the browser loads
|
|
10
|
+
* the editing bridge from it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const ORIGIN = /^https?:\/\/[^\s/;,'"]+$/i;
|
|
14
|
+
|
|
15
|
+
/** The Parhelia origin from the environment: trimmed, without a trailing slash; "" when unset. */
|
|
16
|
+
function originFromEnv(env = process.env) {
|
|
17
|
+
const value = String(env.PARHELIA_ORIGIN || "").trim().replace(/\/+$/, "");
|
|
18
|
+
if (!value) return "";
|
|
19
|
+
if (!ORIGIN.test(value))
|
|
20
|
+
throw new Error(`PARHELIA_ORIGIN must be an origin such as https://cms.example.com, not "${value}".`);
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The rendering-proxy base path from the environment; "" when unset or `/`. */
|
|
25
|
+
function basePathFromEnv(env = process.env) {
|
|
26
|
+
const value = String(env.PARHELIA_RENDERING_PROXY_PATH || env.NEXT_PUBLIC_BASE_PATH || "")
|
|
27
|
+
.trim()
|
|
28
|
+
.replace(/\/+$/, "");
|
|
29
|
+
if (!value) return "";
|
|
30
|
+
if (!value.startsWith("/") || /[\s?#]/.test(value))
|
|
31
|
+
throw new Error(`PARHELIA_RENDERING_PROXY_PATH must be a path such as /parhelia/proxy/marketing, not "${value}".`);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { originFromEnv, basePathFromEnv };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
2
|
+
|
|
3
|
+
/** The Parhelia origin from `PARHELIA_ORIGIN`: trimmed, without a trailing slash; "" when unset. Throws on a non-origin. */
|
|
4
|
+
export declare function originFromEnv(env?: Env): string;
|
|
5
|
+
|
|
6
|
+
/** The rendering-proxy base path from `PARHELIA_RENDERING_PROXY_PATH` (or `NEXT_PUBLIC_BASE_PATH`); "" when unset or `/`. */
|
|
7
|
+
export declare function basePathFromEnv(env?: Env): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Where the CM serves the editing bridge. */
|
|
2
|
+
export declare const SITECORE_BRIDGE_PATH: "/parhelia/editor-bridge/v1/parhelia-bridge.js";
|
|
3
|
+
/** The attribute that marks the bridge's script tag; a page that carries it gets no second one. */
|
|
4
|
+
export declare const SITECORE_BRIDGE_ATTRIBUTE: "data-parhelia-editor-bridge";
|
|
5
|
+
/** The canvas parameters the CM's request carries and the edit-mode render passes on. */
|
|
6
|
+
export declare const PARHELIA_RENDER_PARAMS: readonly ["parhelia", "parhelia_layout"];
|
|
7
|
+
|
|
8
|
+
/** The page the CM asked an editing host to render, read from its request. */
|
|
9
|
+
export type SitecoreRenderRequest = {
|
|
10
|
+
/** The editing secret the CM sent; the SDK's handler checks it. */
|
|
11
|
+
secret: string;
|
|
12
|
+
site: string;
|
|
13
|
+
language: string;
|
|
14
|
+
/** The item's id without braces. */
|
|
15
|
+
itemId: string;
|
|
16
|
+
itemPath: string;
|
|
17
|
+
/** The site-relative route of the item. */
|
|
18
|
+
route: string;
|
|
19
|
+
version?: string;
|
|
20
|
+
/** The canvas parameters (`parhelia`, `parhelia_layout`) the request carries. */
|
|
21
|
+
parhelia: Record<string, string>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** A refused request: the status and the `html` an editing host answers with. */
|
|
25
|
+
export type SitecoreRenderRefusal = { ok: false; status: 400 | 401; html: string };
|
|
26
|
+
|
|
27
|
+
export type SitecoreRenderRequestOptions = {
|
|
28
|
+
/** The query of the request itself, read for a canvas parameter the browser's request does not carry. */
|
|
29
|
+
fallbackQuery?: URLSearchParams;
|
|
30
|
+
/** The site when the layout data names none (for example the app's default site). */
|
|
31
|
+
defaultSite?: string;
|
|
32
|
+
/** The language when the request names none; `en` without one. */
|
|
33
|
+
defaultLanguage?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** The site-relative route of an item path: `/sitecore/content/<…>/<site>/About` is `/About`. */
|
|
37
|
+
export declare function sitecoreRoute(itemPath: string, site: string): string;
|
|
38
|
+
/** Reads the CM's render request (`{ args: [path, layoutData, viewBag], jssEditingSecret }`). */
|
|
39
|
+
export declare function readSitecoreRenderRequest(
|
|
40
|
+
body: unknown,
|
|
41
|
+
options?: SitecoreRenderRequestOptions,
|
|
42
|
+
): { ok: true; request: SitecoreRenderRequest } | SitecoreRenderRefusal;
|
|
43
|
+
/** The query of the edit-mode render the SDK's own handler takes, plus the canvas parameters. */
|
|
44
|
+
export declare function sitecoreEditingQuery(request: SitecoreRenderRequest): URLSearchParams;
|
|
45
|
+
/** Whether the canvas asked for this render (`parhelia=1`). */
|
|
46
|
+
export declare function isParheliaRender(request: SitecoreRenderRequest): boolean;
|
|
47
|
+
/** The editing bridge's address on the CM; a SitecoreAI CM is always reached over HTTPS. */
|
|
48
|
+
export declare function parheliaBridgeUrl(sitecoreOrigin: string): string;
|
|
49
|
+
/** The page with the editing bridge's script before `</head>`; a page that carries it already is returned as it is. */
|
|
50
|
+
export declare function addParheliaBridge(
|
|
51
|
+
html: string,
|
|
52
|
+
options: { sitecoreOrigin: string; bridgeUrl?: string; source?: string },
|
|
53
|
+
): string;
|
package/src/sitecore.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/head/sitecore` — **a SitecoreAI editing host's side of Parhelia's canvas**, framework-free.
|
|
3
|
+
*
|
|
4
|
+
* On SitecoreAI the editor runs inside the CM, and an editing host refuses to be framed by another site. So the canvas frames the
|
|
5
|
+
* CM, and the CM renders the page the way Experience Editor always did: Sitecore's JSS code on the CM sends the page's layout data
|
|
6
|
+
* as a `POST` to the editing host's `/api/editing/render` and serves the `{ "html": … }` it gets back. The SDKs Sitecore ships
|
|
7
|
+
* today answer only Pages' `GET`, so the editing host has to take that `POST` itself:
|
|
8
|
+
*
|
|
9
|
+
* 1. read the CM's request ({@link readSitecoreRenderRequest}): the item, site, language, version, route and editing secret;
|
|
10
|
+
* 2. render it in edit mode through the SDK's own handler, with the parameters Pages would have sent ({@link sitecoreEditingQuery});
|
|
11
|
+
* 3. add Parhelia's editing bridge to the page when the canvas asked for it ({@link addParheliaBridge}), and answer `{ "html": … }`.
|
|
12
|
+
*
|
|
13
|
+
* Steps 1 and 3 live here; step 2 is the framework's (`@parhelia/next/sitecore` for the Content SDK on Next.js). The bridge loads
|
|
14
|
+
* from the CM (`/parhelia/editor-bridge/v1/parhelia-bridge.js`) and talks only to the CM's origin, the frame's parent.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Where the CM serves the editing bridge. */
|
|
18
|
+
export const SITECORE_BRIDGE_PATH = "/parhelia/editor-bridge/v1/parhelia-bridge.js";
|
|
19
|
+
|
|
20
|
+
/** The attribute that marks the bridge's script tag; a page that carries it gets no second one. */
|
|
21
|
+
export const SITECORE_BRIDGE_ATTRIBUTE = "data-parhelia-editor-bridge";
|
|
22
|
+
|
|
23
|
+
/** The canvas parameters the CM's request carries and the edit-mode render passes on. */
|
|
24
|
+
export const PARHELIA_RENDER_PARAMS = Object.freeze(["parhelia", "parhelia_layout"]);
|
|
25
|
+
|
|
26
|
+
const CONTENT_ROOT = "/sitecore/content/";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A trimmed non-empty string (or a number as text); undefined for anything else.
|
|
30
|
+
* @param {unknown} value
|
|
31
|
+
* @returns {string | undefined}
|
|
32
|
+
*/
|
|
33
|
+
function text(value) {
|
|
34
|
+
if (typeof value === "string") return value.trim() || undefined;
|
|
35
|
+
if (typeof value === "number") return String(value);
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A JSON string parsed into an object; undefined when it is not one.
|
|
41
|
+
* @param {unknown} value
|
|
42
|
+
* @returns {Record<string, any> | undefined}
|
|
43
|
+
*/
|
|
44
|
+
function json(value) {
|
|
45
|
+
if (typeof value !== "string") return undefined;
|
|
46
|
+
try {
|
|
47
|
+
const parsed = JSON.parse(value);
|
|
48
|
+
return parsed && typeof parsed === "object" ? parsed : undefined;
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {unknown} value
|
|
56
|
+
* @returns {Record<string, any> | undefined}
|
|
57
|
+
*/
|
|
58
|
+
function object(value) {
|
|
59
|
+
return value && typeof value === "object" ? /** @type {Record<string, any>} */ (value) : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @param {Record<string, any> | undefined} context */
|
|
63
|
+
function siteName(context) {
|
|
64
|
+
const site = context?.site;
|
|
65
|
+
return typeof site === "string" ? text(site) : text(object(site)?.name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The query of the browser's request to the CM, which the view bag names: its URL, then its query string.
|
|
70
|
+
* @param {Record<string, any> | undefined} httpRequest
|
|
71
|
+
*/
|
|
72
|
+
function browserQuery(httpRequest) {
|
|
73
|
+
const query = new URLSearchParams();
|
|
74
|
+
const url = text(httpRequest?.url) ?? text(httpRequest?.rawUrl) ?? text(httpRequest?.pathAndQuery);
|
|
75
|
+
if (url) {
|
|
76
|
+
try {
|
|
77
|
+
new URL(url, "http://localhost").searchParams.forEach((value, key) => query.set(key, value));
|
|
78
|
+
} catch {
|
|
79
|
+
// A malformed URL names no parameters.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const queryString = text(httpRequest?.queryString);
|
|
83
|
+
if (queryString)
|
|
84
|
+
new URLSearchParams(queryString.startsWith("?") ? queryString.slice(1) : queryString).forEach((value, key) => query.set(key, value));
|
|
85
|
+
return query;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The site-relative route of an item: `/sitecore/content/<…>/<site>/About` is `/About` for the site `<site>`. A path outside
|
|
90
|
+
* `/sitecore/content/`, or one that does not name the site, is the route as it stands.
|
|
91
|
+
* @param {string} itemPath
|
|
92
|
+
* @param {string} site
|
|
93
|
+
*/
|
|
94
|
+
export function sitecoreRoute(itemPath, site) {
|
|
95
|
+
const path = itemPath.startsWith("/") ? itemPath : `/${itemPath}`;
|
|
96
|
+
const lower = path.toLowerCase();
|
|
97
|
+
if (!lower.startsWith(CONTENT_ROOT)) return path;
|
|
98
|
+
const siteSegment = `/${site.toLowerCase()}`;
|
|
99
|
+
const siteRoot = lower.lastIndexOf(siteSegment);
|
|
100
|
+
if (siteRoot < CONTENT_ROOT.length) return path;
|
|
101
|
+
return path.slice(siteRoot + siteSegment.length) || "/";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Reads the CM's render request: the JSON body Sitecore's JSS code on the CM posts to `/api/editing/render`
|
|
106
|
+
* (`{ args: [path, layoutData, viewBag], jssEditingSecret }`, the two last arguments as JSON strings).
|
|
107
|
+
*
|
|
108
|
+
* `fallbackQuery` is the query of the request itself, read for a canvas parameter the browser's request does not carry. `defaultSite` and
|
|
109
|
+
* `defaultLanguage` stand in when the layout data names no site or language. A request without the editing secret is refused with
|
|
110
|
+
* `401`; one without an item, or with no site to render it in, with `400`.
|
|
111
|
+
* @param {unknown} body
|
|
112
|
+
* @param {{ fallbackQuery?: URLSearchParams, defaultSite?: string, defaultLanguage?: string }} [options]
|
|
113
|
+
*/
|
|
114
|
+
export function readSitecoreRenderRequest(body, options = {}) {
|
|
115
|
+
const payload = object(body);
|
|
116
|
+
if (!payload) return refusal(400, "Invalid Sitecore editor render payload");
|
|
117
|
+
|
|
118
|
+
const args = Array.isArray(payload.args) ? payload.args : [];
|
|
119
|
+
const layoutData = json(args[1]);
|
|
120
|
+
const viewBag = json(args[2]);
|
|
121
|
+
const context = object(layoutData?.sitecore?.context);
|
|
122
|
+
const route = object(layoutData?.sitecore?.route);
|
|
123
|
+
const httpContext = object(viewBag?.httpContext) ?? json(viewBag?.httpContext);
|
|
124
|
+
const httpRequest = object(httpContext?.request);
|
|
125
|
+
|
|
126
|
+
const secret = text(payload.jssEditingSecret);
|
|
127
|
+
if (!secret) return refusal(401, "Missing Sitecore editor render secret");
|
|
128
|
+
|
|
129
|
+
const itemId = (text(route?.itemId) ?? text(context?.itemId) ?? text(context?.itemID))?.replace(/[{}]/g, "");
|
|
130
|
+
const itemPath = text(args[0]) ?? text(context?.itemPath) ?? text(httpRequest?.path);
|
|
131
|
+
if (!itemId || !itemPath) return refusal(400, "Missing Sitecore editor render item data");
|
|
132
|
+
|
|
133
|
+
const site = siteName(context) ?? text(options.defaultSite);
|
|
134
|
+
if (!site) return refusal(400, "Missing Sitecore editor render site");
|
|
135
|
+
const language = text(viewBag?.language) ?? text(context?.language) ?? text(options.defaultLanguage) ?? "en";
|
|
136
|
+
|
|
137
|
+
const query = browserQuery(httpRequest);
|
|
138
|
+
const fallback = new URLSearchParams(options.fallbackQuery);
|
|
139
|
+
/** @type {Record<string, string>} */
|
|
140
|
+
const parhelia = {};
|
|
141
|
+
for (const name of PARHELIA_RENDER_PARAMS) {
|
|
142
|
+
const value = query.get(name) || fallback.get(name);
|
|
143
|
+
if (value) parhelia[name] = value;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
ok: true,
|
|
148
|
+
request: {
|
|
149
|
+
secret,
|
|
150
|
+
site,
|
|
151
|
+
language,
|
|
152
|
+
itemId,
|
|
153
|
+
itemPath,
|
|
154
|
+
route: sitecoreRoute(itemPath, site),
|
|
155
|
+
version: text(route?.version) ?? text(context?.version),
|
|
156
|
+
parhelia,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {400 | 401} status
|
|
163
|
+
* @param {string} message
|
|
164
|
+
* @returns {{ ok: false, status: 400 | 401, html: string }}
|
|
165
|
+
*/
|
|
166
|
+
function refusal(status, message) {
|
|
167
|
+
return { ok: false, status, html: `<html><body>${message}</body></html>` };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The query of the edit-mode render the SDK's own handler takes, as Pages would have sent it, plus the canvas parameters.
|
|
172
|
+
* @param {{ secret: string, site: string, language: string, itemId: string, route: string, version?: string, parhelia: Record<string, string> }} request
|
|
173
|
+
*/
|
|
174
|
+
export function sitecoreEditingQuery(request) {
|
|
175
|
+
const query = new URLSearchParams({
|
|
176
|
+
secret: request.secret,
|
|
177
|
+
sc_site: request.site,
|
|
178
|
+
sc_itemid: request.itemId,
|
|
179
|
+
sc_lang: request.language,
|
|
180
|
+
route: request.route,
|
|
181
|
+
mode: "edit",
|
|
182
|
+
});
|
|
183
|
+
for (const [name, value] of Object.entries(request.parhelia)) query.set(name, value);
|
|
184
|
+
if (request.version) query.set("sc_version", request.version);
|
|
185
|
+
return query;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Whether the canvas asked for this render (`parhelia=1`), so the page gets the bridge.
|
|
190
|
+
* @param {{ parhelia: Record<string, string> }} request
|
|
191
|
+
*/
|
|
192
|
+
export function isParheliaRender(request) {
|
|
193
|
+
return request.parhelia.parhelia === "1";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The editing bridge's address on the CM. A SitecoreAI CM (`*.sitecorecloud.io`) is always reached over HTTPS.
|
|
198
|
+
* @param {string} sitecoreOrigin
|
|
199
|
+
*/
|
|
200
|
+
export function parheliaBridgeUrl(sitecoreOrigin) {
|
|
201
|
+
const base = /^https?:\/\//i.test(sitecoreOrigin.trim()) ? sitecoreOrigin.trim() : `https://${sitecoreOrigin.trim()}`;
|
|
202
|
+
const url = new URL(SITECORE_BRIDGE_PATH, base);
|
|
203
|
+
if (url.protocol === "http:" && url.hostname.endsWith(".sitecorecloud.io")) url.protocol = "https:";
|
|
204
|
+
return url.toString();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @param {string} value */
|
|
208
|
+
function attribute(value) {
|
|
209
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The page with the editing bridge's script before `</head>` (at the start when it has no head). The bridge talks only to the CM,
|
|
214
|
+
* the frame's parent. A page that already carries the bridge is returned as it is.
|
|
215
|
+
* @param {string} html
|
|
216
|
+
* @param {{ sitecoreOrigin: string, bridgeUrl?: string, source?: string }} options
|
|
217
|
+
*/
|
|
218
|
+
export function addParheliaBridge(html, options) {
|
|
219
|
+
if (html.includes(`${SITECORE_BRIDGE_ATTRIBUTE}="runtime"`)) return html;
|
|
220
|
+
const parentOrigin = new URL(parheliaBridgeUrl(options.sitecoreOrigin)).origin;
|
|
221
|
+
const src = options.bridgeUrl ?? parheliaBridgeUrl(options.sitecoreOrigin);
|
|
222
|
+
const tag =
|
|
223
|
+
`<script src="${attribute(src)}" ${SITECORE_BRIDGE_ATTRIBUTE}="runtime" data-parhelia-parent-origin="${attribute(parentOrigin)}"` +
|
|
224
|
+
` data-parhelia-allowed-parent-origins="${attribute(parentOrigin)}" data-parhelia-source="${attribute(options.source ?? "sitecore")}"` +
|
|
225
|
+
` defer></script>`;
|
|
226
|
+
const headEnd = html.indexOf("</head>");
|
|
227
|
+
return headEnd >= 0 ? html.slice(0, headEnd) + tag + html.slice(headEnd) : tag + html;
|
|
228
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const EMPTY_SLOT_ATTRIBUTE: "data-parhelia-empty-slot";
|
|
2
|
+
export declare const EMPTY_SLOT_MIN_HEIGHT: "6rem";
|
|
3
|
+
export declare function slotStartAttributes(name: string, description?: string): { "data-placeholder-start": string; "data-description": string };
|
|
4
|
+
export declare function slotEndAttributes(name: string): { "data-placeholder-end": string };
|
|
5
|
+
export declare function emptySlotAttributes(name: string): { "data-parhelia-empty-slot": string };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slot markers without a framework: the attributes of Parhelia's slot sentinels (inert `<script>` elements around a
|
|
3
|
+
* slot's rendering) and of the empty block an empty slot renders in Parhelia's frames, where the editor draws its drop
|
|
4
|
+
* zone. `ParheliaSlot` of `@parhelia/react` renders them with React; a head without React writes the same elements.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** The attribute of the block an empty slot renders in Parhelia's frames; its value is the slot's key. */
|
|
8
|
+
export const EMPTY_SLOT_ATTRIBUTE = "data-parhelia-empty-slot";
|
|
9
|
+
|
|
10
|
+
/** The default height of an empty slot's block. */
|
|
11
|
+
export const EMPTY_SLOT_MIN_HEIGHT = "6rem";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The opening sentinel `<script>`'s attributes.
|
|
15
|
+
* @param {string} name the slot's key as Parhelia names it
|
|
16
|
+
* @param {string} [description] its label (default `name`)
|
|
17
|
+
*/
|
|
18
|
+
export const slotStartAttributes = (name, description) => ({ "data-placeholder-start": name, "data-description": description || name });
|
|
19
|
+
|
|
20
|
+
/** The closing sentinel `<script>`'s attributes. @param {string} name */
|
|
21
|
+
export const slotEndAttributes = (name) => ({ "data-placeholder-end": name });
|
|
22
|
+
|
|
23
|
+
/** The empty block's attributes (its height is the head's: `min-height`, default `EMPTY_SLOT_MIN_HEIGHT`). @param {string} name */
|
|
24
|
+
export const emptySlotAttributes = (name) => ({ [EMPTY_SLOT_ATTRIBUTE]: name });
|
package/src/typo3.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** One rendered field's identity, as a TYPO3 render knows it. */
|
|
2
|
+
export type T3FieldRef = {
|
|
3
|
+
/** The record's TCA table (`tt_content`, `pages`, a Content Blocks table). */
|
|
4
|
+
table: string;
|
|
5
|
+
/** The item's uid (a translation row is named by its parent). */
|
|
6
|
+
uid: string | number;
|
|
7
|
+
/** The TCA column. */
|
|
8
|
+
field: string;
|
|
9
|
+
/** The language id of the row that was rendered (-1: a record of all languages). */
|
|
10
|
+
language: string | number;
|
|
11
|
+
/** The field's element holds rich text. */
|
|
12
|
+
richText?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** The vocabulary's wire name. */
|
|
16
|
+
export declare const TYPO3_MARKER_VOCABULARY: "typo3";
|
|
17
|
+
/** The one entity kind a field marker names. */
|
|
18
|
+
export declare const T3_RECORD_KIND: "record";
|
|
19
|
+
export declare const T3_TABLE_ATTRIBUTE: string;
|
|
20
|
+
export declare const T3_UID_ATTRIBUTE: string;
|
|
21
|
+
export declare const T3_FIELD_ATTRIBUTE: string;
|
|
22
|
+
export declare const T3_LANGUAGE_ATTRIBUTE: string;
|
|
23
|
+
export declare const T3_RICHTEXT_ATTRIBUTE: string;
|
|
24
|
+
/** The language id of a record of all languages. */
|
|
25
|
+
export declare const T3_ALL_LANGUAGES: "-1";
|
|
26
|
+
/** Parhelia addresses a TYPO3 record as its current state in the mount's workspace. */
|
|
27
|
+
export declare const PARHELIA_TYPO3_VERSION: "0";
|
|
28
|
+
|
|
29
|
+
/** The key of a record marker, `{table}:{uid}`; undefined for a malformed table or uid. */
|
|
30
|
+
export declare function t3RecordKey(table: string, uid: string | number): string | undefined;
|
|
31
|
+
/** Reads a record key `{table}:{uid}`; undefined when it is not one. */
|
|
32
|
+
export declare function parseT3RecordKey(key: string): { table: string; uid: string } | undefined;
|
|
33
|
+
/** The field marker attributes of one rendered field; `{}` outside the editor. */
|
|
34
|
+
export declare function t3FieldAttributes(ref: T3FieldRef, edit: boolean): Record<string, string>;
|
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
|
+
};
|