@parhelia/head 1.0.12903 → 1.0.13030
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/README.md +49 -12
- package/package.json +30 -2
- package/src/aem.d.ts +30 -0
- package/src/aem.js +80 -0
- package/src/bridge-core.d.ts +4 -0
- package/src/bridge-core.js +4 -0
- package/src/contentful.d.ts +29 -0
- package/src/contentful.js +185 -0
- package/src/orchardcore.d.ts +41 -0
- package/src/orchardcore.js +145 -0
- package/src/picture-crops.d.ts +168 -0
- package/src/picture-crops.js +355 -0
- package/src/sitecore.d.ts +53 -0
- package/src/sitecore.js +228 -0
|
@@ -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
|
+
}
|