@shop-on/theme-server 0.8.0 → 0.9.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.
- package/dist/LICENSE.md +2 -2
- package/dist/graphql-env.d.ts +1244 -126
- package/dist/liquid.d.ts +115 -0
- package/dist/liquid.js +250 -2
- package/dist/metafields.js +11 -1
- package/dist/metaobject.js +1 -9
- package/dist/schema.storefront.graphql +518 -0
- package/package.json +4 -3
package/dist/liquid.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { FileInfo } from "./files.js";
|
|
2
|
+
import { MenuData } from "./menu.js";
|
|
3
|
+
import { MetaobjectData } from "./metaobject.js";
|
|
2
4
|
import { ProductData } from "./product.js";
|
|
3
5
|
import { Money } from "@shop-on/theme-runtime/money";
|
|
4
6
|
//#region src/liquid.d.ts
|
|
@@ -82,4 +84,117 @@ export declare function renderLiquid(args: {
|
|
|
82
84
|
html: string;
|
|
83
85
|
error?: string;
|
|
84
86
|
}>;
|
|
87
|
+
/** Already-sanitized HTML that renders unescaped: rich text settings (sanitized
|
|
88
|
+
* at write), page bodies (`Shop::Page` `sanitizes_html :body`) and rich text
|
|
89
|
+
* metafields and entry fields (`serializeRichText`, below). Any filter
|
|
90
|
+
* that reads it as a string gets `html` from `toString`, and its result is a
|
|
91
|
+
* plain string, which the escape then escapes. A `SafeHtml` drop field must
|
|
92
|
+
* be BOTH set through `hideSafeHtml` (non-enumerable, so `for`, `[0]`/`[1]`,
|
|
93
|
+
* `first`/`last` and `json` never reach it without naming it) AND listed in
|
|
94
|
+
* `SAFE_HTML_FIELDS` in @shop-on/theme-contract/custom-section (so the
|
|
95
|
+
* validator keeps the named read out of an attribute value). Either alone
|
|
96
|
+
* lets it into an attribute. */
|
|
97
|
+
export declare class SafeHtml {
|
|
98
|
+
readonly html: string;
|
|
99
|
+
constructor(html: string);
|
|
100
|
+
toString(): string;
|
|
101
|
+
}
|
|
102
|
+
export type CustomImage = {
|
|
103
|
+
url: string;
|
|
104
|
+
alt: string | null;
|
|
105
|
+
width: number | null;
|
|
106
|
+
height: number | null;
|
|
107
|
+
};
|
|
108
|
+
export declare function customImage(file: FileInfo | null | undefined): CustomImage | null;
|
|
109
|
+
export type CustomMetafield = {
|
|
110
|
+
type: string;
|
|
111
|
+
value: string;
|
|
112
|
+
values: string[];
|
|
113
|
+
html: SafeHtml | null;
|
|
114
|
+
};
|
|
115
|
+
export type CustomProduct = Omit<LiquidProduct, "featured_image" | "images"> & {
|
|
116
|
+
featured_image: CustomImage | null;
|
|
117
|
+
images: CustomImage[];
|
|
118
|
+
/** Locale-free; a template pipes it through `link_url`. */
|
|
119
|
+
url: string;
|
|
120
|
+
/** `namespace → key → { type, value, values, html }`; `values` is the parsed
|
|
121
|
+
* list for a `LIST_*` type, else []; `html` the HTML of a `RICH_TEXT` value,
|
|
122
|
+
* else null. `{}` but on the route product. */
|
|
123
|
+
metafields: Record<string, Record<string, CustomMetafield>>;
|
|
124
|
+
};
|
|
125
|
+
/** `metafields: true` for the route product only: a picker-resolved product's
|
|
126
|
+
* loader carries none the template could name (plan: "{} everywhere else"). */
|
|
127
|
+
export declare function customProduct(p: ProductData, opts?: {
|
|
128
|
+
metafields?: boolean;
|
|
129
|
+
}): CustomProduct;
|
|
130
|
+
export type CustomCollection = Omit<LiquidCollection, "image"> & {
|
|
131
|
+
image: CustomImage | null;
|
|
132
|
+
url: string;
|
|
133
|
+
/** A picker-resolved collection's products, at most its `limit`. The route
|
|
134
|
+
* collection has none (the grid section owns them). */
|
|
135
|
+
products?: CustomProduct[];
|
|
136
|
+
};
|
|
137
|
+
export declare function customCollection(c: LiquidCollectionSource, products?: readonly ProductData[]): CustomCollection;
|
|
138
|
+
export type CustomMenuLink = {
|
|
139
|
+
title: string;
|
|
140
|
+
url: string;
|
|
141
|
+
links: CustomMenuLink[];
|
|
142
|
+
};
|
|
143
|
+
export type CustomMenu = {
|
|
144
|
+
title: string;
|
|
145
|
+
links: CustomMenuLink[];
|
|
146
|
+
};
|
|
147
|
+
export declare function customMenu(menu: MenuData): CustomMenu;
|
|
148
|
+
export type CustomPage = {
|
|
149
|
+
title: string;
|
|
150
|
+
url: string;
|
|
151
|
+
content: SafeHtml;
|
|
152
|
+
};
|
|
153
|
+
/** `content` is `SafeHtml`: `Shop::Page` sanitizes `body` on every assignment
|
|
154
|
+
* (`sanitizes_html :body`, apps/api/app/models/shop/page.rb) and its
|
|
155
|
+
* translations are html-format rows, sanitized on write
|
|
156
|
+
* (`Shop::Translation#sanitize_html_value`). */
|
|
157
|
+
export declare function customPage(page: {
|
|
158
|
+
handle: string;
|
|
159
|
+
title: string;
|
|
160
|
+
bodyHtml: string;
|
|
161
|
+
}): CustomPage;
|
|
162
|
+
export type CustomEntryField = {
|
|
163
|
+
type: string;
|
|
164
|
+
value: string | null;
|
|
165
|
+
values: string[];
|
|
166
|
+
image: CustomImage | null;
|
|
167
|
+
html: SafeHtml | null;
|
|
168
|
+
};
|
|
169
|
+
export type CustomEntry = {
|
|
170
|
+
handle: string;
|
|
171
|
+
type: string;
|
|
172
|
+
name: string;
|
|
173
|
+
/** A nested entry reference stays its raw id, as in `MetaobjectData`. */
|
|
174
|
+
fields: Record<string, CustomEntryField>;
|
|
175
|
+
};
|
|
176
|
+
export declare function customEntry(entry: MetaobjectData): CustomEntry;
|
|
177
|
+
/** The drops a custom section's Liquid can reach (the validator's
|
|
178
|
+
* `CUSTOM_LIQUID_ROOTS`). `settings` holds the declared settings' values and
|
|
179
|
+
* the resolved pickers, built by `renderCustomSections` in
|
|
180
|
+
* @shop-on/theme-loader/custom-sections. */
|
|
181
|
+
export interface CustomLiquidContext {
|
|
182
|
+
settings: Record<string, unknown>;
|
|
183
|
+
product: CustomProduct | null;
|
|
184
|
+
collection: CustomCollection | null;
|
|
185
|
+
shop: LiquidShop;
|
|
186
|
+
}
|
|
187
|
+
/** Render a custom section's Liquid. NEVER throws: a parse or render failure,
|
|
188
|
+
* the render cap included, comes back as `{ html: "", error }`, so partial
|
|
189
|
+
* output never reaches the page. */
|
|
190
|
+
export declare function renderCustomLiquid(args: {
|
|
191
|
+
source: string;
|
|
192
|
+
context: CustomLiquidContext;
|
|
193
|
+
locale?: string;
|
|
194
|
+
/** Adds the request locale's prefix to a site path (`link_url`). */
|
|
195
|
+
localizePath?: (path: string) => string;
|
|
196
|
+
}): Promise<{
|
|
197
|
+
html: string;
|
|
198
|
+
error?: string;
|
|
199
|
+
}>;
|
|
85
200
|
//#endregion
|
package/dist/liquid.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { escapeHtmlAttr } from "./html.js";
|
|
2
|
+
import { parseListValues } from "./metafields.js";
|
|
3
|
+
import { Context, Liquid } from "liquidjs";
|
|
4
|
+
import { normalizeCustomUrl } from "@shop-on/theme-contract";
|
|
5
|
+
import { CUSTOM_LIQUID_PARSE_OPTIONS, restrictToCustomTags } from "@shop-on/theme-contract/custom-section";
|
|
2
6
|
import { formatMoney } from "@shop-on/theme-runtime/money";
|
|
7
|
+
import { productHref } from "@shop-on/theme-runtime/product-href";
|
|
8
|
+
import { serializeRichText } from "@shop-on/theme-runtime/richtext";
|
|
3
9
|
//#region src/liquid.ts
|
|
4
10
|
const ENGINES = /* @__PURE__ */ new Map();
|
|
5
11
|
const MAX_ENGINES = 16;
|
|
@@ -100,5 +106,247 @@ async function renderLiquid(args) {
|
|
|
100
106
|
};
|
|
101
107
|
}
|
|
102
108
|
}
|
|
109
|
+
/** Already-sanitized HTML that renders unescaped: rich text settings (sanitized
|
|
110
|
+
* at write), page bodies (`Shop::Page` `sanitizes_html :body`) and rich text
|
|
111
|
+
* metafields and entry fields (`serializeRichText`, below). Any filter
|
|
112
|
+
* that reads it as a string gets `html` from `toString`, and its result is a
|
|
113
|
+
* plain string, which the escape then escapes. A `SafeHtml` drop field must
|
|
114
|
+
* be BOTH set through `hideSafeHtml` (non-enumerable, so `for`, `[0]`/`[1]`,
|
|
115
|
+
* `first`/`last` and `json` never reach it without naming it) AND listed in
|
|
116
|
+
* `SAFE_HTML_FIELDS` in @shop-on/theme-contract/custom-section (so the
|
|
117
|
+
* validator keeps the named read out of an attribute value). Either alone
|
|
118
|
+
* lets it into an attribute. */
|
|
119
|
+
var SafeHtml = class {
|
|
120
|
+
html;
|
|
121
|
+
constructor(html) {
|
|
122
|
+
this.html = html;
|
|
123
|
+
}
|
|
124
|
+
toString() {
|
|
125
|
+
return this.html;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
function stringify(v) {
|
|
129
|
+
if (typeof v === "string") return v;
|
|
130
|
+
if (v == null) return "";
|
|
131
|
+
if (Array.isArray(v)) return v.map(stringify).join("");
|
|
132
|
+
return String(v);
|
|
133
|
+
}
|
|
134
|
+
const escapeText = (v) => escapeHtmlAttr(stringify(v));
|
|
135
|
+
/** Puts a `SafeHtml` on a drop as a non-enumerable own property: a template
|
|
136
|
+
* reads it only by naming it (`x.html`), which the validator sees. Iterating
|
|
137
|
+
* the drop (`for` pairs an object's enumerable keys) or
|
|
138
|
+
* serializing it (`json`) never hands it back unnamed. */
|
|
139
|
+
function hideSafeHtml(drop, key, value) {
|
|
140
|
+
return Object.defineProperty(drop, key, {
|
|
141
|
+
value,
|
|
142
|
+
enumerable: false
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const plainHtml = (v) => v instanceof SafeHtml ? v.html : v;
|
|
146
|
+
/** The `outputEscape` every `{{ }}` ends in. */
|
|
147
|
+
function escapeOutput(v) {
|
|
148
|
+
return v instanceof SafeHtml ? v.html : escapeText(v);
|
|
149
|
+
}
|
|
150
|
+
function hasControl(url) {
|
|
151
|
+
for (let i = 0; i < url.length; i++) {
|
|
152
|
+
const c = url.charCodeAt(i);
|
|
153
|
+
if (c < 32 || c === 127) return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
/** A URL the media attributes may load: the shop's API hands out absolute
|
|
158
|
+
* http(s) URLs; a site path is the shop too. */
|
|
159
|
+
function isMediaUrl(url) {
|
|
160
|
+
if (hasControl(url) || url.includes("\\")) return false;
|
|
161
|
+
return /^https?:\/\//i.test(url) || url.startsWith("/") && !url.startsWith("//");
|
|
162
|
+
}
|
|
163
|
+
/** `link_url`: a link the template may put in `href`. http(s) (a bare domain
|
|
164
|
+
* becomes https), a site path (localized), `#…`, `?…`, `mailto:` and `tel:`;
|
|
165
|
+
* anything else, protocol-relative `//host` included, is "". */
|
|
166
|
+
function linkUrl(value, localizePath) {
|
|
167
|
+
const raw = typeof value === "string" ? value : value !== null && typeof value === "object" && typeof value.url === "string" ? value.url : null;
|
|
168
|
+
if (raw === null) return "";
|
|
169
|
+
const trimmed = raw.trim();
|
|
170
|
+
if (hasControl(trimmed) || trimmed.startsWith("//")) return "";
|
|
171
|
+
if (trimmed.startsWith("?")) return trimmed.includes("\\") ? "" : trimmed;
|
|
172
|
+
const normalized = normalizeCustomUrl(trimmed);
|
|
173
|
+
if (!normalized.ok) return "";
|
|
174
|
+
if (normalized.url.startsWith("/")) return localizePath(normalized.url);
|
|
175
|
+
return normalized.url;
|
|
176
|
+
}
|
|
177
|
+
const IMAGE_SRCSET = /* @__PURE__ */ new WeakMap();
|
|
178
|
+
function customImage(file) {
|
|
179
|
+
if (!file || file.kind !== "image" || !file.url || !isMediaUrl(file.url)) return null;
|
|
180
|
+
const image = {
|
|
181
|
+
url: file.url,
|
|
182
|
+
alt: file.alt,
|
|
183
|
+
width: file.width,
|
|
184
|
+
height: file.height
|
|
185
|
+
};
|
|
186
|
+
IMAGE_SRCSET.set(image, file.srcset);
|
|
187
|
+
return image;
|
|
188
|
+
}
|
|
189
|
+
/** `image_url`: the drop's own URL, or the narrowest variant the loader
|
|
190
|
+
* fetched that is at least `width` wide (the widest when none is). A variant
|
|
191
|
+
* the stored URL can't be rewritten to: variants are separate files the API
|
|
192
|
+
* names, so only the ones the fetch already asked for exist here. */
|
|
193
|
+
function imageUrl(value, width) {
|
|
194
|
+
if (value === null || typeof value !== "object" || !IMAGE_SRCSET.has(value)) return "";
|
|
195
|
+
const image = value;
|
|
196
|
+
const want = Number(width);
|
|
197
|
+
const srcset = IMAGE_SRCSET.get(value);
|
|
198
|
+
if (!srcset || !Number.isFinite(want) || want <= 0) return image.url;
|
|
199
|
+
const variants = srcset.split(", ").map((entry) => /^(\S+) (\d+)w$/.exec(entry.trim())).filter((m) => m !== null && isMediaUrl(m[1])).map((m) => ({
|
|
200
|
+
url: m[1],
|
|
201
|
+
w: Number(m[2])
|
|
202
|
+
})).sort((a, b) => a.w - b.w);
|
|
203
|
+
if (variants.length === 0) return image.url;
|
|
204
|
+
return (variants.find((v) => v.w >= want) ?? variants.at(-1)).url;
|
|
205
|
+
}
|
|
206
|
+
/** A RICH_TEXT value as HTML, through the one serializer (it escapes every
|
|
207
|
+
* text value and allowlists the tags); null for any other type or a tree it
|
|
208
|
+
* refuses. */
|
|
209
|
+
function richTextHtml(type, value) {
|
|
210
|
+
if (type !== "RICH_TEXT" || value === null) return null;
|
|
211
|
+
const html = serializeRichText(value);
|
|
212
|
+
return html === null ? null : new SafeHtml(html);
|
|
213
|
+
}
|
|
214
|
+
const nonNull = (v) => v !== null;
|
|
215
|
+
function nestMetafields(map) {
|
|
216
|
+
const byNamespace = /* @__PURE__ */ new Map();
|
|
217
|
+
for (const [path, { type, value }] of Object.entries(map ?? {})) {
|
|
218
|
+
const dot = path.indexOf(".");
|
|
219
|
+
if (dot <= 0) continue;
|
|
220
|
+
const namespace = path.slice(0, dot);
|
|
221
|
+
const values = type.startsWith("LIST_") ? parseListValues(value) : [];
|
|
222
|
+
const keys = byNamespace.get(namespace) ?? [];
|
|
223
|
+
keys.push([path.slice(dot + 1), hideSafeHtml({
|
|
224
|
+
type,
|
|
225
|
+
value,
|
|
226
|
+
values
|
|
227
|
+
}, "html", richTextHtml(type, value))]);
|
|
228
|
+
byNamespace.set(namespace, keys);
|
|
229
|
+
}
|
|
230
|
+
return Object.fromEntries([...byNamespace].map(([namespace, keys]) => [namespace, Object.fromEntries(keys)]));
|
|
231
|
+
}
|
|
232
|
+
/** `metafields: true` for the route product only: a picker-resolved product's
|
|
233
|
+
* loader carries none the template could name (plan: "{} everywhere else"). */
|
|
234
|
+
function customProduct(p, opts = {}) {
|
|
235
|
+
const images = p.media.map(customImage).filter(nonNull);
|
|
236
|
+
return {
|
|
237
|
+
...liquidProduct(p),
|
|
238
|
+
featured_image: images[0] ?? null,
|
|
239
|
+
images,
|
|
240
|
+
url: productHref(p.handle) ?? "",
|
|
241
|
+
metafields: opts.metafields ? nestMetafields(p.metafields) : {}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function customCollection(c, products) {
|
|
245
|
+
return {
|
|
246
|
+
...liquidCollection(c),
|
|
247
|
+
image: customImage(c.image),
|
|
248
|
+
url: `/collections/${c.handle}`,
|
|
249
|
+
...products ? { products: products.map((p) => customProduct(p)) } : {}
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const MENU_DEPTH = 3;
|
|
253
|
+
function menuLink(item, depth) {
|
|
254
|
+
return {
|
|
255
|
+
title: item.title,
|
|
256
|
+
url: item.url ?? "",
|
|
257
|
+
links: depth < MENU_DEPTH ? (item.children ?? []).map((c) => menuLink(c, depth + 1)) : []
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function customMenu(menu) {
|
|
261
|
+
return {
|
|
262
|
+
title: menu.title,
|
|
263
|
+
links: menu.items.nodes.map((item) => menuLink(item, 1))
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/** `content` is `SafeHtml`: `Shop::Page` sanitizes `body` on every assignment
|
|
267
|
+
* (`sanitizes_html :body`, apps/api/app/models/shop/page.rb) and its
|
|
268
|
+
* translations are html-format rows, sanitized on write
|
|
269
|
+
* (`Shop::Translation#sanitize_html_value`). */
|
|
270
|
+
function customPage(page) {
|
|
271
|
+
return hideSafeHtml({
|
|
272
|
+
title: page.title,
|
|
273
|
+
url: `/pages/${page.handle}`
|
|
274
|
+
}, "content", new SafeHtml(page.bodyHtml));
|
|
275
|
+
}
|
|
276
|
+
function customEntry(entry) {
|
|
277
|
+
return {
|
|
278
|
+
handle: entry.handle,
|
|
279
|
+
type: entry.type,
|
|
280
|
+
name: entry.display_name,
|
|
281
|
+
fields: Object.fromEntries(Object.entries(entry.fields).map(([key, f]) => [key, hideSafeHtml({
|
|
282
|
+
type: f.type,
|
|
283
|
+
value: f.value,
|
|
284
|
+
values: f.values ?? [],
|
|
285
|
+
image: customImage(f.file)
|
|
286
|
+
}, "html", richTextHtml(f.type, f.value))]))
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const LOCALIZE = "localizePath";
|
|
290
|
+
const identity = (path) => path;
|
|
291
|
+
const CUSTOM_ENGINES = /* @__PURE__ */ new Map();
|
|
292
|
+
function customEngineFor(locale) {
|
|
293
|
+
const cached = CUSTOM_ENGINES.get(locale);
|
|
294
|
+
if (cached) return cached;
|
|
295
|
+
const engine = new Liquid({
|
|
296
|
+
...CUSTOM_LIQUID_PARSE_OPTIONS,
|
|
297
|
+
parseLimit: 64e3,
|
|
298
|
+
renderLimit: 20,
|
|
299
|
+
memoryLimit: 1e7,
|
|
300
|
+
root: [],
|
|
301
|
+
fs: DENY_FS,
|
|
302
|
+
strictFilters: false,
|
|
303
|
+
strictVariables: false,
|
|
304
|
+
outputEscape: escapeOutput
|
|
305
|
+
});
|
|
306
|
+
restrictToCustomTags(engine);
|
|
307
|
+
const Cycle = engine.tags.cycle;
|
|
308
|
+
class EscapedCycle extends Cycle {
|
|
309
|
+
*render(ctx, emitter) {
|
|
310
|
+
return escapeText(yield super.render(ctx, emitter));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
engine.registerTag("cycle", EscapedCycle);
|
|
314
|
+
engine.registerFilter("raw", (v) => v);
|
|
315
|
+
const map = engine.filters.map;
|
|
316
|
+
engine.registerFilter("map", function* (...args) {
|
|
317
|
+
return (yield map.apply(this, args)).map(plainHtml);
|
|
318
|
+
});
|
|
319
|
+
const groupBy = engine.filters.group_by;
|
|
320
|
+
engine.registerFilter("group_by", function* (...args) {
|
|
321
|
+
return (yield groupBy.apply(this, args)).map((g) => ({
|
|
322
|
+
name: plainHtml(g.name),
|
|
323
|
+
items: g.items
|
|
324
|
+
}));
|
|
325
|
+
});
|
|
326
|
+
engine.registerFilter("money", (v) => isMoney(v) ? formatMoney(v, locale) : "");
|
|
327
|
+
engine.registerFilter("link_url", function(v) {
|
|
328
|
+
return linkUrl(v, this.context.getRegister(LOCALIZE, identity));
|
|
329
|
+
});
|
|
330
|
+
engine.registerFilter("image_url", (v, width) => imageUrl(v, width));
|
|
331
|
+
if (CUSTOM_ENGINES.size < MAX_ENGINES) CUSTOM_ENGINES.set(locale, engine);
|
|
332
|
+
return engine;
|
|
333
|
+
}
|
|
334
|
+
/** Render a custom section's Liquid. NEVER throws: a parse or render failure,
|
|
335
|
+
* the render cap included, comes back as `{ html: "", error }`, so partial
|
|
336
|
+
* output never reaches the page. */
|
|
337
|
+
async function renderCustomLiquid(args) {
|
|
338
|
+
try {
|
|
339
|
+
const engine = customEngineFor(canonicalLocale(args.locale));
|
|
340
|
+
const templates = engine.parse(args.source);
|
|
341
|
+
const ctx = new Context(args.context, engine.options, {}, { liquid: engine });
|
|
342
|
+
ctx.setRegister(LOCALIZE, args.localizePath ?? identity);
|
|
343
|
+
return { html: await engine.render(templates, ctx) };
|
|
344
|
+
} catch (e) {
|
|
345
|
+
return {
|
|
346
|
+
html: "",
|
|
347
|
+
error: e instanceof Error ? e.message : String(e)
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
103
351
|
//#endregion
|
|
104
|
-
export { liquidCollection, liquidProduct, renderLiquid };
|
|
352
|
+
export { SafeHtml, customCollection, customEntry, customImage, customMenu, customPage, customProduct, liquidCollection, liquidProduct, renderCustomLiquid, renderLiquid };
|
package/dist/metafields.js
CHANGED
|
@@ -8,6 +8,16 @@ function toMetafieldMap(nodes) {
|
|
|
8
8
|
value: m.value
|
|
9
9
|
}]));
|
|
10
10
|
}
|
|
11
|
+
/** A `LIST_*` field's stored JSON array as strings; `[]` when it is not one. */
|
|
12
|
+
function parseListValues(raw) {
|
|
13
|
+
try {
|
|
14
|
+
const arr = JSON.parse(raw);
|
|
15
|
+
if (!Array.isArray(arr)) return [];
|
|
16
|
+
return arr.filter((x) => x != null).map(String);
|
|
17
|
+
} catch {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
11
21
|
/** The cached metafield read behind each route resource's `*_metafield`
|
|
12
22
|
* dynamic source. `read` returns the resource's metafield nodes, or `null`
|
|
13
23
|
* when the resource is missing. A resource with no published fields is `{}`
|
|
@@ -30,4 +40,4 @@ function metafieldsReader(name, read) {
|
|
|
30
40
|
return async (ref, locale) => await cached(ref, locale) ?? {};
|
|
31
41
|
}
|
|
32
42
|
//#endregion
|
|
33
|
-
export { metafieldsReader, toMetafieldMap };
|
|
43
|
+
export { metafieldsReader, parseListValues, toMetafieldMap };
|
package/dist/metaobject.js
CHANGED
|
@@ -2,6 +2,7 @@ import { memoTTL } from "./cache.js";
|
|
|
2
2
|
import { graphql, request } from "./graphql.js";
|
|
3
3
|
import { getContentVersion } from "./content-version.js";
|
|
4
4
|
import { toFileInfo } from "./files.js";
|
|
5
|
+
import { parseListValues } from "./metafields.js";
|
|
5
6
|
//#region src/metaobject.ts
|
|
6
7
|
const FetchMetaobjectQuery = graphql(`
|
|
7
8
|
query FetchMetaobject($id: ID!) {
|
|
@@ -45,15 +46,6 @@ const FetchMetaobjectQuery = graphql(`
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
`);
|
|
48
|
-
function parseListValues(raw) {
|
|
49
|
-
try {
|
|
50
|
-
const arr = JSON.parse(raw);
|
|
51
|
-
if (!Array.isArray(arr)) return [];
|
|
52
|
-
return arr.filter((x) => x != null).map(String);
|
|
53
|
-
} catch {
|
|
54
|
-
return [];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
49
|
const fetchMetaobjectCached = memoTTL(async (id) => {
|
|
58
50
|
const m = (await request(FetchMetaobjectQuery, { id })).metaobject;
|
|
59
51
|
if (!m) return null;
|