@tunasand/sdk 0.0.0-stage → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/README.md +516 -2
  3. package/dist/analytics.d.ts +1 -0
  4. package/dist/analytics.js +43 -0
  5. package/dist/app.d.ts +1 -0
  6. package/dist/app.js +825 -0
  7. package/dist/apps-9u4XMi7E.js +114 -0
  8. package/dist/body.d.ts +1 -0
  9. package/dist/body.js +455 -0
  10. package/dist/bridge-DYAYGV7t.js +74 -0
  11. package/dist/bridge.d.ts +1 -0
  12. package/dist/bridge.js +189 -0
  13. package/dist/cli.d.ts +1 -0
  14. package/dist/cli.js +283 -0
  15. package/dist/client-Derrexwr.js +159 -0
  16. package/dist/customers-C_vJY2_v.js +84 -0
  17. package/dist/customers.d.ts +1 -0
  18. package/dist/customers.js +185 -0
  19. package/dist/declared-C_GvPiGF.js +397 -0
  20. package/dist/delivery.d.ts +1 -0
  21. package/dist/delivery.js +394 -0
  22. package/dist/dev.d.ts +1 -0
  23. package/dist/dev.js +93 -0
  24. package/dist/embed-R-OvDYAw.js +90 -0
  25. package/dist/errorLabels-CPKiPabZ.js +1510 -0
  26. package/dist/i18n.d.ts +1 -0
  27. package/dist/i18n.js +4 -0
  28. package/dist/integrations.d.ts +1 -0
  29. package/dist/integrations.js +62 -0
  30. package/dist/ja-C1HEWWTQ.js +241 -0
  31. package/dist/ja-C6lX0CeJ.js +213 -0
  32. package/dist/model-pmXzGX12.js +152 -0
  33. package/dist/model.d.ts +1 -0
  34. package/dist/model.js +345 -0
  35. package/dist/page-E_cYgZx-.js +138 -0
  36. package/dist/preview.d.ts +1 -0
  37. package/dist/preview.js +1 -0
  38. package/dist/profile-D6t1QMQh.js +223 -0
  39. package/dist/schema-BPHL7Qr-.js +115 -0
  40. package/dist/schema-UQJWRyfJ.js +995 -0
  41. package/dist/schema.d.ts +1 -0
  42. package/dist/schema.js +1 -0
  43. package/dist/src-sazhzYui.js +71 -0
  44. package/dist/types/access/src/handoff.d.ts +52 -0
  45. package/dist/types/access/src/index.d.ts +5 -0
  46. package/dist/types/access/src/jws.d.ts +5 -0
  47. package/dist/types/access/src/jwt.d.ts +35 -0
  48. package/dist/types/access/src/oidc.d.ts +50 -0
  49. package/dist/types/access/src/signed.d.ts +15 -0
  50. package/dist/types/analytics/src/day.d.ts +2 -0
  51. package/dist/types/analytics/src/index.d.ts +5 -0
  52. package/dist/types/analytics/src/schedule.d.ts +2 -0
  53. package/dist/types/analytics/src/shared.d.ts +21 -0
  54. package/dist/types/analytics/src/source.d.ts +19 -0
  55. package/dist/types/analytics/src/store.d.ts +24 -0
  56. package/dist/types/analytics/src/view.d.ts +33 -0
  57. package/dist/types/body/src/components.d.ts +25 -0
  58. package/dist/types/body/src/declared.d.ts +34 -0
  59. package/dist/types/body/src/derive.d.ts +13 -0
  60. package/dist/types/body/src/description.d.ts +8 -0
  61. package/dist/types/body/src/embed.d.ts +16 -0
  62. package/dist/types/body/src/href.d.ts +4 -0
  63. package/dist/types/body/src/html.d.ts +30 -0
  64. package/dist/types/body/src/index.d.ts +19 -0
  65. package/dist/types/body/src/inline.d.ts +20 -0
  66. package/dist/types/body/src/labels/en.d.ts +90 -0
  67. package/dist/types/body/src/labels/ja.d.ts +153 -0
  68. package/dist/types/body/src/library.d.ts +113 -0
  69. package/dist/types/body/src/markdown.d.ts +14 -0
  70. package/dist/types/body/src/normalize.d.ts +5 -0
  71. package/dist/types/body/src/problems.d.ts +103 -0
  72. package/dist/types/body/src/profile.d.ts +80 -0
  73. package/dist/types/body/src/render.d.ts +29 -0
  74. package/dist/types/body/src/schema.d.ts +6 -0
  75. package/dist/types/body/src/standard.d.ts +20 -0
  76. package/dist/types/body/src/types.d.ts +140 -0
  77. package/dist/types/cms-contract/src/apiKeys.d.ts +41 -0
  78. package/dist/types/cms-contract/src/app.d.ts +35 -0
  79. package/dist/types/cms-contract/src/appDependencies.d.ts +265 -0
  80. package/dist/types/cms-contract/src/appProblems.d.ts +22 -0
  81. package/dist/types/cms-contract/src/apps.d.ts +195 -0
  82. package/dist/types/cms-contract/src/assets.d.ts +12 -0
  83. package/dist/types/cms-contract/src/authorize.d.ts +146 -0
  84. package/dist/types/cms-contract/src/bridge.d.ts +318 -0
  85. package/dist/types/cms-contract/src/consents.d.ts +97 -0
  86. package/dist/types/cms-contract/src/customerLabels.d.ts +33 -0
  87. package/dist/types/cms-contract/src/customers.d.ts +266 -0
  88. package/dist/types/cms-contract/src/embeds.d.ts +165 -0
  89. package/dist/types/cms-contract/src/errorLabels.d.ts +269 -0
  90. package/dist/types/cms-contract/src/errorLabelsEn.d.ts +228 -0
  91. package/dist/types/cms-contract/src/errors.d.ts +105 -0
  92. package/dist/types/cms-contract/src/failureStatus.d.ts +124 -0
  93. package/dist/types/cms-contract/src/functionLabels.d.ts +5 -0
  94. package/dist/types/cms-contract/src/functions.d.ts +144 -0
  95. package/dist/types/cms-contract/src/identifiers.d.ts +117 -0
  96. package/dist/types/cms-contract/src/import.d.ts +277 -0
  97. package/dist/types/cms-contract/src/issueLabels.d.ts +435 -0
  98. package/dist/types/cms-contract/src/issueLabelsEn.d.ts +217 -0
  99. package/dist/types/cms-contract/src/migration.d.ts +103 -0
  100. package/dist/types/cms-contract/src/paths.d.ts +23 -0
  101. package/dist/types/cms-contract/src/permissionLabels.d.ts +8 -0
  102. package/dist/types/cms-contract/src/permissions.d.ts +77 -0
  103. package/dist/types/cms-contract/src/problems.d.ts +34 -0
  104. package/dist/types/cms-contract/src/roles.d.ts +57 -0
  105. package/dist/types/cms-contract/src/settingProblems.d.ts +13 -0
  106. package/dist/types/cms-contract/src/settings.d.ts +19 -0
  107. package/dist/types/cms-contract/src/siteArtifact.d.ts +45 -0
  108. package/dist/types/cms-contract/src/siteDeclaration.d.ts +70 -0
  109. package/dist/types/cms-contract/src/transfer.d.ts +57 -0
  110. package/dist/types/cms-contract/src/urlLabel.d.ts +13 -0
  111. package/dist/types/cms-contract/src/views.d.ts +21 -0
  112. package/dist/types/cms-contract/src/webhookLabels.d.ts +7 -0
  113. package/dist/types/cms-contract/src/webhooks.d.ts +139 -0
  114. package/dist/types/i18n/src/index.d.ts +50 -0
  115. package/dist/types/i18n/src/problems.d.ts +21 -0
  116. package/dist/types/model/src/asset.d.ts +111 -0
  117. package/dist/types/model/src/audience.d.ts +38 -0
  118. package/dist/types/model/src/brand.d.ts +25 -0
  119. package/dist/types/model/src/capabilities.d.ts +28 -0
  120. package/dist/types/model/src/changes.d.ts +34 -0
  121. package/dist/types/model/src/check.d.ts +15 -0
  122. package/dist/types/model/src/contact.d.ts +22 -0
  123. package/dist/types/model/src/image.d.ts +38 -0
  124. package/dist/types/model/src/index.d.ts +26 -0
  125. package/dist/types/model/src/input.d.ts +26 -0
  126. package/dist/types/model/src/integrity.d.ts +79 -0
  127. package/dist/types/model/src/json.d.ts +2 -0
  128. package/dist/types/model/src/labels/en.d.ts +169 -0
  129. package/dist/types/model/src/labels/ja.d.ts +198 -0
  130. package/dist/types/model/src/legacy.d.ts +2 -0
  131. package/dist/types/model/src/media.d.ts +5 -0
  132. package/dist/types/model/src/mediaUrl.d.ts +67 -0
  133. package/dist/types/model/src/model.d.ts +415 -0
  134. package/dist/types/model/src/page.d.ts +52 -0
  135. package/dist/types/model/src/problems.d.ts +184 -0
  136. package/dist/types/model/src/refs.d.ts +27 -0
  137. package/dist/types/model/src/rules.d.ts +15 -0
  138. package/dist/types/model/src/schema.d.ts +58 -0
  139. package/dist/types/model/src/screens.d.ts +76 -0
  140. package/dist/types/model/src/selfOps.d.ts +30 -0
  141. package/dist/types/model/src/time.d.ts +25 -0
  142. package/dist/types/model/src/typesSource.d.ts +8 -0
  143. package/dist/types/model/src/values.d.ts +24 -0
  144. package/dist/types/platform/src/defaultModel.d.ts +8 -0
  145. package/dist/types/sdk/src/analytics.d.ts +2 -0
  146. package/dist/types/sdk/src/app.d.ts +20 -0
  147. package/dist/types/sdk/src/body.d.ts +2 -0
  148. package/dist/types/sdk/src/bridge.d.ts +92 -0
  149. package/dist/types/sdk/src/cli.d.ts +2 -0
  150. package/dist/types/sdk/src/customers.d.ts +190 -0
  151. package/dist/types/sdk/src/delivery/cache.d.ts +18 -0
  152. package/dist/types/sdk/src/delivery/changes.d.ts +28 -0
  153. package/dist/types/sdk/src/delivery/client.d.ts +90 -0
  154. package/dist/types/sdk/src/delivery/fields.d.ts +48 -0
  155. package/dist/types/sdk/src/delivery/index.d.ts +9 -0
  156. package/dist/types/sdk/src/delivery/media.d.ts +28 -0
  157. package/dist/types/sdk/src/delivery/query.d.ts +85 -0
  158. package/dist/types/sdk/src/delivery/schema.d.ts +19 -0
  159. package/dist/types/sdk/src/delivery/values.d.ts +82 -0
  160. package/dist/types/sdk/src/delivery.d.ts +3 -0
  161. package/dist/types/sdk/src/dev.d.ts +29 -0
  162. package/dist/types/sdk/src/i18n.d.ts +12 -0
  163. package/dist/types/sdk/src/integrations.d.ts +33 -0
  164. package/dist/types/sdk/src/model.d.ts +5 -0
  165. package/dist/types/sdk/src/preview.d.ts +1 -0
  166. package/dist/types/sdk/src/schema.d.ts +21 -0
  167. package/dist/types/sdk/src/site.d.ts +43 -0
  168. package/dist/typesSource-bO0jFw1I.js +88 -0
  169. package/package.json +86 -3
package/dist/app.js ADDED
@@ -0,0 +1,825 @@
1
+ import { g as JSON_BODY_MAX, h as ERROR_STATUS, n as errorBodyFor } from "./errorLabels-CPKiPabZ.js";
2
+ import { c as formatMessage, d as problemOf, f as problemText } from "./ja-C1HEWWTQ.js";
3
+ import { i as CUSTOMER_STATUSES } from "./customers-C_vJY2_v.js";
4
+ import { i as bodyProblemText, n as embedOfUrl } from "./embed-R-OvDYAw.js";
5
+ import { a as mergeStyle, c as ALIGNS, n as isSafeCssValue, o as sortMarks, r as HTML_TAG_MARKS, s as isSafeHref, u as isTableCell } from "./profile-D6t1QMQh.js";
6
+ import { b as isPublishable, t as AUDIENCES } from "./model-pmXzGX12.js";
7
+ import { parseDocument } from "htmlparser2";
8
+ // ../body/src/normalize.ts
9
+ const keyOf = (inline) => "component" in inline ? null : JSON.stringify([
10
+ sortMarks(inline.marks),
11
+ inline.link ?? null,
12
+ inline.style ?? null
13
+ ]);
14
+ /** 装飾・リンク・見た目のない文字か */
15
+ const isBare = (t) => !t.marks?.length && !t.link && !t.style;
16
+ /** インラインの断片の列を正規化 */
17
+ function normalizeRich(rich) {
18
+ const out = [];
19
+ for (const inline of rich) {
20
+ if (typeof inline !== "string" && "component" in inline) {
21
+ out.push(inline);
22
+ continue;
23
+ }
24
+ const piece = typeof inline === "string" ? inline : isBare(inline) ? inline.text : {
25
+ text: inline.text,
26
+ ...inline.marks?.length ? { marks: sortMarks(inline.marks) } : {},
27
+ ...inline.link ? { link: inline.link } : {},
28
+ ...inline.style ? { style: inline.style } : {}
29
+ };
30
+ if (!(typeof piece === "string" ? piece : piece.text)) continue;
31
+ const last = out.at(-1);
32
+ if (typeof piece === "string" && typeof last === "string") out[out.length - 1] = last + piece;
33
+ else if (typeof piece !== "string" && last !== void 0 && typeof last !== "string" && keyOf(last) !== null && keyOf(last) === keyOf(piece)) out[out.length - 1] = {
34
+ ...last,
35
+ text: last.text + piece.text
36
+ };
37
+ else out.push(piece);
38
+ }
39
+ return out;
40
+ }
41
+ const normalizeList = (list) => ({
42
+ ...list,
43
+ items: list.items.map((i) => ({
44
+ ...i,
45
+ content: normalizeRich(i.content),
46
+ ...i.children ? { children: normalizeList(i.children) } : {}
47
+ }))
48
+ });
49
+ const normalizeCell = (cell) => isTableCell(cell) ? {
50
+ ...cell,
51
+ content: cell.content.map(normalizeRich)
52
+ } : normalizeRich(cell);
53
+ /** 本文を正規化 */
54
+ function normalizeBody(body) {
55
+ return body.map((block) => {
56
+ switch (block.type) {
57
+ case "paragraph":
58
+ case "heading": return {
59
+ ...block,
60
+ content: normalizeRich(block.content)
61
+ };
62
+ case "list": return normalizeList(block);
63
+ case "quote": return {
64
+ ...block,
65
+ content: block.content.map(normalizeRich)
66
+ };
67
+ case "table": return {
68
+ ...block,
69
+ ...block.head ? { head: block.head.map(normalizeRich) } : {},
70
+ rows: block.rows.map((r) => r.map(normalizeCell))
71
+ };
72
+ default: return block;
73
+ }
74
+ });
75
+ }
76
+ // ../body/src/html.ts
77
+ /** 内容ごと捨てる要素 */
78
+ const DANGEROUS = /* @__PURE__ */ new Set([
79
+ "script",
80
+ "style",
81
+ "frame",
82
+ "frameset",
83
+ "object",
84
+ "embed",
85
+ "applet",
86
+ "form",
87
+ "input",
88
+ "button",
89
+ "textarea",
90
+ "select",
91
+ "option",
92
+ "meta",
93
+ "link",
94
+ "base",
95
+ "svg",
96
+ "math",
97
+ "noscript",
98
+ "template",
99
+ "canvas",
100
+ "dialog",
101
+ "head",
102
+ "title"
103
+ ]);
104
+ const BLOCK_TAGS = /* @__PURE__ */ new Set([
105
+ "p",
106
+ "h1",
107
+ "h2",
108
+ "h3",
109
+ "h4",
110
+ "h5",
111
+ "h6",
112
+ "ul",
113
+ "ol",
114
+ "blockquote",
115
+ "pre",
116
+ "figure",
117
+ "table",
118
+ "hr",
119
+ "div",
120
+ "section",
121
+ "article",
122
+ "iframe",
123
+ "img",
124
+ "header",
125
+ "footer",
126
+ "aside",
127
+ "main",
128
+ "nav"
129
+ ]);
130
+ const ALIGN_SET = new Set(ALIGNS);
131
+ const STYLE_KEYS = {
132
+ color: "color",
133
+ "background-color": "background",
134
+ background: "background",
135
+ "font-size": "size"
136
+ };
137
+ /** 本文の意味を持たない属性(捨てても数えない) */
138
+ const SILENT_ATTRS = /* @__PURE__ */ new Set([
139
+ "id",
140
+ "width",
141
+ "height",
142
+ "alt",
143
+ "src",
144
+ "href",
145
+ "colspan",
146
+ "rowspan",
147
+ "target",
148
+ "rel",
149
+ "title",
150
+ "style",
151
+ "class",
152
+ "data-filename",
153
+ "data-embed-type",
154
+ "data-mime-type",
155
+ "data-iframely-url",
156
+ "allow",
157
+ "allowfullscreen",
158
+ "frameborder",
159
+ "loading",
160
+ "scrolling"
161
+ ]);
162
+ const isElement = (n) => n.type === "tag" || n.type === "script" || n.type === "style";
163
+ /** style 属性の項目(名前は小文字) */
164
+ function declarations(el) {
165
+ const out = /* @__PURE__ */ new Map();
166
+ for (const part of (el.attribs.style ?? "").split(";")) {
167
+ const i = part.indexOf(":");
168
+ if (i < 0) continue;
169
+ const key = part.slice(0, i).trim().toLowerCase();
170
+ const value = part.slice(i + 1).trim();
171
+ if (key && value) out.set(key, value);
172
+ }
173
+ return out;
174
+ }
175
+ const textOf = (nodes) => nodes.map((n) => n.type === "text" ? n.data : isElement(n) ? n.name === "br" ? "\n" : textOf(n.children) : "").join("");
176
+ const intOf = (v) => {
177
+ const n = v === void 0 ? NaN : Number.parseInt(v, 10);
178
+ return Number.isFinite(n) && n > 0 ? n : void 0;
179
+ };
180
+ /** HTML を本文に(コピーできないものは dropped) */
181
+ function bodyFromHtml(html, options) {
182
+ const counts = /* @__PURE__ */ new Map();
183
+ const drop = (what) => counts.set(what, (counts.get(what) ?? 0) + 1);
184
+ const alignOf = (el) => {
185
+ const v = declarations(el).get("text-align")?.toLowerCase();
186
+ return v && ALIGN_SET.has(v) ? v : void 0;
187
+ };
188
+ /** 要素の属性と style のうち、コピーできないものを数える(used はコピーした style の項目) */
189
+ const audit = (el, used = []) => {
190
+ for (const name of Object.keys(el.attribs)) if (name.startsWith("on")) drop(bodyProblemText("drop_attribute", { name }));
191
+ else if (!SILENT_ATTRS.has(name) && !name.startsWith("data-") && !name.startsWith("aria-")) drop(bodyProblemText("drop_attribute", { name }));
192
+ for (const key of declarations(el).keys()) if (!used.includes(key)) drop(bodyProblemText("drop_style", { key }));
193
+ };
194
+ const classesOf = (el) => (el.attribs.class ?? "").split(/\s+/).filter(Boolean);
195
+ /** span などの見た目(コピーできない class と style の項目は数える) */
196
+ const styleOfElement = (el) => {
197
+ const decls = declarations(el);
198
+ const style = {};
199
+ const used = [];
200
+ for (const [key, kind] of Object.entries(STYLE_KEYS)) {
201
+ const value = decls.get(key);
202
+ if (!value) continue;
203
+ used.push(key);
204
+ if (!isSafeCssValue(kind, value)) {
205
+ drop(bodyProblemText("drop_style_value", { key }));
206
+ continue;
207
+ }
208
+ const name = options.nameOf?.(kind, value);
209
+ style[kind] = {
210
+ ...name ? { name } : {},
211
+ value
212
+ };
213
+ }
214
+ if (decls.has("text-align")) used.push("text-align");
215
+ const kept = classesOf(el).filter((c) => {
216
+ if (options.styleClass?.(c)) return true;
217
+ drop(bodyProblemText("drop_class", { name: c }));
218
+ return false;
219
+ });
220
+ if (kept.length) style.styles = kept;
221
+ audit(el, used);
222
+ return Object.keys(style).length ? style : void 0;
223
+ };
224
+ const linkOf = (el) => {
225
+ const href = el.attribs.href?.trim();
226
+ if (!href) return void 0;
227
+ if (!isSafeHref(href)) {
228
+ drop(bodyProblemText("drop_unsafe_link"));
229
+ return;
230
+ }
231
+ const rel = el.attribs.rel?.trim();
232
+ return {
233
+ href,
234
+ ...el.attribs.title ? { title: el.attribs.title } : {},
235
+ ...rel ? { rel } : {},
236
+ ...el.attribs.target === "_blank" ? { target: "_blank" } : {}
237
+ };
238
+ };
239
+ /** インラインの断片(ブロックの要素に出会ったら呼び出し側が分ける) */
240
+ const inlines = (nodes, ctx) => nodes.flatMap((node) => {
241
+ if (node.type === "text") {
242
+ const text = node.data.replace(/[\t\r\n ]+/g, " ");
243
+ if (!text) return [];
244
+ return [ctx.marks.length || ctx.link || ctx.style ? {
245
+ text,
246
+ ...ctx.marks.length ? { marks: [...ctx.marks] } : {},
247
+ ...ctx.link ? { link: ctx.link } : {},
248
+ ...ctx.style ? { style: ctx.style } : {}
249
+ } : text];
250
+ }
251
+ if (!isElement(node)) return [];
252
+ const name = node.name.toLowerCase();
253
+ if (DANGEROUS.has(name) || name === "iframe") {
254
+ drop(bodyProblemText("drop_element", { name }));
255
+ return [];
256
+ }
257
+ if (name === "br") return ["\n"];
258
+ if (name === "img") {
259
+ if (ctx.images) ctx.images.push({
260
+ img: node,
261
+ ...ctx.link ? { link: ctx.link } : {}
262
+ });
263
+ else drop(bodyProblemText("drop_inline_image"));
264
+ return [];
265
+ }
266
+ const mark = HTML_TAG_MARKS[name];
267
+ if (mark) {
268
+ audit(node);
269
+ return inlines(node.children, {
270
+ ...ctx,
271
+ marks: ctx.marks.includes(mark) ? ctx.marks : [...ctx.marks, mark]
272
+ });
273
+ }
274
+ if (name === "a") {
275
+ const link = linkOf(node);
276
+ return inlines(node.children, {
277
+ ...ctx,
278
+ ...link ? { link } : {}
279
+ });
280
+ }
281
+ if (name === "span" || name === "font") {
282
+ const style = styleOfElement(node);
283
+ return inlines(node.children, {
284
+ ...ctx,
285
+ style: mergeStyle(ctx.style, style)
286
+ });
287
+ }
288
+ if (!BLOCK_TAGS.has(name)) drop(bodyProblemText("drop_element", { name }));
289
+ return inlines(node.children, ctx);
290
+ });
291
+ /** 断片の列(images があれば中の画像をそこへ集める) */
292
+ const rich = (nodes, images) => {
293
+ const trimmed = [...normalizeRich(inlines(nodes, {
294
+ marks: [],
295
+ ...images ? { images } : {}
296
+ }))];
297
+ const edge = (i, f) => {
298
+ const x = trimmed[i];
299
+ if (typeof x === "string") trimmed[i] = f(x);
300
+ else if (x && "text" in x && !("component" in x)) trimmed[i] = {
301
+ ...x,
302
+ text: f(x.text)
303
+ };
304
+ };
305
+ edge(0, (s) => s.replace(/^ +/, ""));
306
+ edge(trimmed.length - 1, (s) => s.replace(/ +$/, ""));
307
+ return normalizeRich(trimmed);
308
+ };
309
+ const hasText = (r) => r.some((i) => typeof i === "string" ? i.trim() : "text" in i ? (i.text ?? "").trim() : true);
310
+ /** 集めた画像を画像のブロックに(where は数える場所) */
311
+ const hoistedBlocks = (images, where) => images.flatMap(({ img, link }) => {
312
+ drop(bodyProblemText("drop_hoisted_image", { where }));
313
+ const image = imageOf(img, link ? { link } : {});
314
+ return image ? [image] : [];
315
+ });
316
+ /** ブロックの要素で区切って文字の連なりを段落にする(区切りの要素は onBreak、連なりは onRun) */
317
+ const segment = (nodes, isBreak, onRun, onBreak) => {
318
+ let run = [];
319
+ const flush = () => {
320
+ if (run.length) onRun(run);
321
+ run = [];
322
+ };
323
+ for (const node of nodes) if (isElement(node) && isBreak(node)) {
324
+ flush();
325
+ onBreak(node);
326
+ } else run.push(node);
327
+ flush();
328
+ };
329
+ /** キャプション・表の説明の文字(文字だけの項目のため、装飾は捨てて数える、中の画像は images へ) */
330
+ const captionText = (el, images) => {
331
+ if (rich(el.children, images).some((i) => typeof i !== "string")) drop(bodyProblemText("drop_caption_marks"));
332
+ return textOf(el.children.filter((c) => !(isElement(c) && c.name === "img"))).trim();
333
+ };
334
+ const HEADINGS = /* @__PURE__ */ new Set([
335
+ "h1",
336
+ "h2",
337
+ "h3",
338
+ "h4",
339
+ "h5",
340
+ "h6"
341
+ ]);
342
+ const CONTAINERS = /* @__PURE__ */ new Set([
343
+ "div",
344
+ "section",
345
+ "article",
346
+ "header",
347
+ "footer",
348
+ "aside",
349
+ "main",
350
+ "nav"
351
+ ]);
352
+ /** 段落の列: 引用・表のセルの中(where は数える場所、本文の型で段落しか持てない場所)
353
+ p は1段落、入れ物は中を段落に、リスト・見出し・引用・コード・表は段落にコピーして数える、画像は images へ(後へ出す) */
354
+ const paragraphsOf = (nodes, where, images) => {
355
+ const out = [];
356
+ const push = (r) => {
357
+ if (hasText(r)) out.push(r);
358
+ };
359
+ segment(nodes, (el) => BLOCK_TAGS.has(el.name.toLowerCase()), (run) => push(rich(run, images)), (el) => {
360
+ const name = el.name.toLowerCase();
361
+ if (name === "p") push(rich(el.children, images));
362
+ else if (name === "ul" || name === "ol") {
363
+ drop(bodyProblemText("drop_flattened", {
364
+ where,
365
+ what: "list"
366
+ }));
367
+ listParagraphs(listOf(el, images), "").forEach(push);
368
+ } else if (HEADINGS.has(name)) {
369
+ drop(bodyProblemText("drop_flattened", {
370
+ where,
371
+ what: "heading"
372
+ }));
373
+ push(rich(el.children, images));
374
+ } else if (name === "blockquote") {
375
+ drop(bodyProblemText("drop_flattened", {
376
+ where,
377
+ what: "quote"
378
+ }));
379
+ paragraphsOf(el.children, where, images).forEach(push);
380
+ } else if (name === "pre") {
381
+ drop(bodyProblemText("drop_flattened", {
382
+ where,
383
+ what: "code"
384
+ }));
385
+ push([textOf(el.children).replace(/\n$/, "")]);
386
+ } else if (name === "table") {
387
+ drop(bodyProblemText("drop_flattened", {
388
+ where,
389
+ what: "table"
390
+ }));
391
+ for (const tr of findAll(el, (e) => e.name === "tr")) push(normalizeRich(tr.children.filter((c) => isElement(c) && (c.name === "td" || c.name === "th")).flatMap((td, i) => [...i ? [" / "] : [], ...paragraphsOf(td.children, where, images).flatMap((r, j) => [...j ? [" "] : [], ...r])])));
392
+ } else if (name === "img") images.push({ img: el });
393
+ else if (name === "figure") {
394
+ drop(bodyProblemText("drop_flattened", {
395
+ where,
396
+ what: "figure"
397
+ }));
398
+ findAll(el, (e) => e.name === "img").forEach((img) => images.push({
399
+ img,
400
+ ...linkAround(el, img) ? { link: linkAround(el, img) } : {}
401
+ }));
402
+ const figcaption = findDeep(el, (e) => e.name === "figcaption");
403
+ if (figcaption) push([captionText(figcaption, [])]);
404
+ } else if (CONTAINERS.has(name)) paragraphsOf(el.children, where, images).forEach(push);
405
+ else drop(bodyProblemText("drop_inner_element", {
406
+ where,
407
+ name
408
+ }));
409
+ });
410
+ return out;
411
+ };
412
+ /** リストを頭の印つきの段落に(入れ子は字下げ、項目の中の改行の後も印の幅だけ字下げ) */
413
+ const listParagraphs = (list, indent) => list.items.flatMap((item, i) => {
414
+ const continued = item.content.map((x) => typeof x === "string" ? x.replaceAll("\n", `\n${indent} `) : "text" in x && !("component" in x) ? {
415
+ ...x,
416
+ text: x.text.replaceAll("\n", `\n${indent} `)
417
+ } : x);
418
+ return [normalizeRich([`${indent}${list.ordered ? `${i + 1}. ` : "・"}`, ...continued]), ...item.children ? listParagraphs(item.children, `${indent} `) : []];
419
+ });
420
+ /** 埋め込みの元の URL(Iframely は url の引数か中のリンク) */
421
+ const embedUrlOf = (el) => {
422
+ if (el.name === "a" && el.attribs.href) return el.attribs.href;
423
+ const src = el.attribs.src ?? el.attribs["data-iframely-url"];
424
+ const parsed = src ? URL.parse(src) : null;
425
+ if (parsed?.hostname.endsWith("iframe.ly")) return parsed.searchParams.get("url") ?? void 0;
426
+ return src;
427
+ };
428
+ const embedBlock = (url, el) => {
429
+ if (!url || !URL.parse(url)) {
430
+ drop(bodyProblemText("drop_embed_no_url"));
431
+ return null;
432
+ }
433
+ const target = embedOfUrl(url);
434
+ const width = intOf(el.attribs.width);
435
+ const height = intOf(el.attribs.height);
436
+ if (target) return {
437
+ type: "component",
438
+ name: "embed",
439
+ props: {
440
+ ...target,
441
+ url,
442
+ ...width ? { width } : {},
443
+ ...height ? { height } : {}
444
+ }
445
+ };
446
+ if (!url.startsWith("https://")) {
447
+ drop(bodyProblemText("drop_embed_not_https"));
448
+ return null;
449
+ }
450
+ return {
451
+ type: "component",
452
+ name: "link-card",
453
+ props: {
454
+ href: url,
455
+ title: url
456
+ }
457
+ };
458
+ };
459
+ const findDeep = (el, pred) => {
460
+ for (const c of el.children) {
461
+ if (!isElement(c)) continue;
462
+ if (pred(c)) return c;
463
+ const found = findDeep(c, pred);
464
+ if (found) return found;
465
+ }
466
+ };
467
+ const findAll = (el, pred) => el.children.filter(isElement).flatMap((c) => [...pred(c) ? [c] : [], ...findAll(c, pred)]);
468
+ /** 画像を包むリンク(root の中で img を含む a、数えるのは linkOf の1回だけ) */
469
+ const linkCache = /* @__PURE__ */ new Map();
470
+ const linkAround = (root, img) => {
471
+ const anchor = findDeep(root, (e) => e.name === "a" && findDeep(e, (x) => x === img) !== void 0);
472
+ if (!anchor) return void 0;
473
+ if (!linkCache.has(anchor)) linkCache.set(anchor, linkOf(anchor));
474
+ return linkCache.get(anchor);
475
+ };
476
+ const imageOf = (img, extra = {}) => {
477
+ const src = img.attribs.src?.trim();
478
+ if (!src) {
479
+ drop(bodyProblemText("drop_image_no_src"));
480
+ return null;
481
+ }
482
+ const alt = img.attribs.alt?.trim();
483
+ const width = intOf(img.attribs.width);
484
+ const height = intOf(img.attribs.height);
485
+ return {
486
+ type: "image",
487
+ media: options.image(src, {
488
+ ...alt ? { alt } : {},
489
+ ...width ? { width } : {},
490
+ ...height ? { height } : {}
491
+ }),
492
+ ...alt ? { alt } : {},
493
+ ...extra
494
+ };
495
+ };
496
+ /** リスト(項目の中の入れ子のリストはすべてつなぐ、番号の有無は先頭の入れ子に揃える、中の画像は images へ) */
497
+ const listOf = (el, images) => {
498
+ const items = el.children.filter(isElement).flatMap((li) => {
499
+ if (li.name !== "li") {
500
+ drop(bodyProblemText("drop_list_element", { name: li.name }));
501
+ return [];
502
+ }
503
+ const nestedLists = li.children.filter((c) => isElement(c) && (c.name === "ul" || c.name === "ol"));
504
+ if (nestedLists.length > 1) drop(bodyProblemText("drop_extra_nested_list"));
505
+ const inner = li.children.filter((c) => !nestedLists.includes(c));
506
+ const paragraphs = inner.filter((c) => isElement(c) && c.name === "p");
507
+ const content = paragraphs.length ? normalizeRich(paragraphs.flatMap((p, i) => [...i ? ["\n"] : [], ...rich(p.children, images)])) : rich(inner, images);
508
+ const nested = nestedLists.length ? {
509
+ ...listOf(nestedLists[0], images),
510
+ items: nestedLists.flatMap((l) => listOf(l, images).items)
511
+ } : void 0;
512
+ return [{
513
+ content,
514
+ ...nested ? { children: nested } : {}
515
+ }];
516
+ });
517
+ return {
518
+ type: "list",
519
+ ...el.name === "ol" ? { ordered: true } : {},
520
+ items
521
+ };
522
+ };
523
+ const tableOf = (el, images) => {
524
+ const rows = [];
525
+ const collect = (parent) => {
526
+ for (const c of parent.children) {
527
+ if (!isElement(c)) continue;
528
+ if (c.name === "tr") rows.push(c);
529
+ else if (c.name === "thead" || c.name === "tbody" || c.name === "tfoot") collect(c);
530
+ else if (c.name !== "caption" && c.name !== "colgroup") drop(bodyProblemText("drop_table_element", { name: c.name }));
531
+ }
532
+ };
533
+ collect(el);
534
+ const captionEl = el.children.find((c) => isElement(c) && c.name === "caption");
535
+ const caption = captionEl ? captionText(captionEl, images) : "";
536
+ const grid = rows.map((tr) => tr.children.filter(isElement).flatMap((td) => {
537
+ if (td.name !== "td" && td.name !== "th") return [];
538
+ audit(td, ["text-align"]);
539
+ const colspan = intOf(td.attribs.colspan);
540
+ const rowspan = intOf(td.attribs.rowspan);
541
+ const align = alignOf(td);
542
+ return [{
543
+ content: paragraphsOf(td.children, "cell", images),
544
+ ...td.name === "th" ? { header: true } : {},
545
+ ...colspan && colspan > 1 ? { colspan } : {},
546
+ ...rowspan && rowspan > 1 ? { rowspan } : {},
547
+ ...align ? { align } : {}
548
+ }];
549
+ }));
550
+ const simpleCell = (c) => c.content.length <= 1 && !c.colspan && !c.rowspan && !c.align;
551
+ const only = (c) => c.content[0] ?? [];
552
+ const [first, ...rest] = grid;
553
+ if (grid.every((r) => r.every(simpleCell)) && rest.every((r) => r.every((c) => !c.header)) && (first?.every((c) => c.header) || first?.every((c) => !c.header)) && first) {
554
+ const headed = first.every((c) => c.header);
555
+ const plainRows = (headed ? rest : grid).map((r) => r.map(only));
556
+ return {
557
+ type: "table",
558
+ ...headed ? { head: first.map(only) } : {},
559
+ rows: plainRows,
560
+ ...caption ? { caption } : {}
561
+ };
562
+ }
563
+ return {
564
+ type: "table",
565
+ rows: grid.map((r) => r.map((c) => c)),
566
+ ...caption ? { caption } : {}
567
+ };
568
+ };
569
+ /** ブロックの列(段落の外の文字と装飾はまとめて段落に、その中の画像は段落の後へ) */
570
+ const blocks = (nodes) => {
571
+ const out = [];
572
+ segment(nodes.filter((n) => isElement(n) || n.type === "text"), (el) => {
573
+ const name = el.name.toLowerCase();
574
+ return DANGEROUS.has(name) || BLOCK_TAGS.has(name) || name === "a" && el.attribs["data-embed-type"] === "file" && Boolean(options.file);
575
+ }, (run) => out.push(...paragraphBlocks(run, void 0)), (el) => {
576
+ const name = el.name.toLowerCase();
577
+ if (DANGEROUS.has(name)) drop(bodyProblemText("drop_element", { name }));
578
+ else if (name === "a") out.push(fileOf(el));
579
+ else out.push(...blockOf(el));
580
+ });
581
+ return out;
582
+ };
583
+ /** 文字の連なりを段落に(中の画像は段落の後のブロックに) */
584
+ const paragraphBlocks = (run, align) => {
585
+ const images = [];
586
+ const r = rich(run, images);
587
+ return [...hasText(r) ? [{
588
+ type: "paragraph",
589
+ content: r,
590
+ ...align ? { align } : {}
591
+ }] : [], ...hoistedBlocks(images, "paragraph")];
592
+ };
593
+ /** 段落: 直下の画像とファイルのリンクで段落を分け、画像を含む要素(リンク・装飾)の後でも分ける(中の画像はその後へ) */
594
+ const paragraphOf = (el, align) => {
595
+ const parts = [];
596
+ let run = [];
597
+ const flush = () => {
598
+ if (run.length) parts.push(...paragraphBlocks(run, align));
599
+ run = [];
600
+ };
601
+ for (const c of el.children) if (isElement(c) && c.name === "img") {
602
+ flush();
603
+ const image = imageOf(c);
604
+ if (image) parts.push(image);
605
+ } else if (isElement(c) && c.name === "a" && c.attribs["data-embed-type"] === "file" && options.file) {
606
+ flush();
607
+ parts.push(fileOf(c));
608
+ } else {
609
+ run.push(c);
610
+ if (isElement(c) && findDeep(c, (e) => e.name === "img")) flush();
611
+ }
612
+ flush();
613
+ return parts;
614
+ };
615
+ const fileOf = (a) => {
616
+ const name = textOf(a.children).trim();
617
+ return {
618
+ type: "file",
619
+ media: options.file(a.attribs.href ?? "", {
620
+ ...a.attribs["data-mime-type"] ? { mime: a.attribs["data-mime-type"] } : {},
621
+ ...name ? { name } : {}
622
+ }),
623
+ ...name ? { label: name } : {}
624
+ };
625
+ };
626
+ const blockOf = (el) => {
627
+ const name = el.name.toLowerCase();
628
+ const images = [];
629
+ switch (name) {
630
+ case "p":
631
+ audit(el, ["text-align"]);
632
+ return paragraphOf(el, alignOf(el));
633
+ case "h1":
634
+ case "h2":
635
+ case "h3":
636
+ case "h4":
637
+ case "h5":
638
+ case "h6": {
639
+ audit(el, ["text-align"]);
640
+ const content = rich(el.children, images);
641
+ const id = el.attribs.id && /^[\w-]+$/.test(el.attribs.id) ? el.attribs.id : void 0;
642
+ const align = alignOf(el);
643
+ return [...hasText(content) ? [{
644
+ type: "heading",
645
+ level: Number(name[1]),
646
+ content,
647
+ ...id ? { id } : {},
648
+ ...align ? { align } : {}
649
+ }] : [], ...hoistedBlocks(images, "heading")];
650
+ }
651
+ case "ul":
652
+ case "ol":
653
+ audit(el);
654
+ return [listOf(el, images), ...hoistedBlocks(images, "list")];
655
+ case "blockquote":
656
+ audit(el);
657
+ return [{
658
+ type: "quote",
659
+ content: paragraphsOf(el.children, "quote", images)
660
+ }, ...hoistedBlocks(images, "quote")];
661
+ case "pre": {
662
+ audit(el);
663
+ const code = el.children.find((c) => isElement(c) && c.name === "code");
664
+ const lang = code ? classesOf(code).find((c) => c.startsWith("language-"))?.slice(9) : void 0;
665
+ const filename = el.parent?.attribs?.["data-filename"];
666
+ return [{
667
+ type: "code",
668
+ code: textOf(code ? code.children : el.children).replace(/\n$/, ""),
669
+ ...lang ? { lang } : {},
670
+ ...filename ? { filename } : {}
671
+ }];
672
+ }
673
+ case "figure": {
674
+ const decl = declarations(el).get("text-align")?.toLowerCase();
675
+ audit(el, ["text-align"]);
676
+ const align = decl === "left" || decl === "center" || decl === "right" ? decl : void 0;
677
+ const frame = findDeep(el, (e) => e.name === "iframe" || e.attribs["data-iframely-url"] !== void 0);
678
+ if (frame) return [embedBlock(embedUrlOf(frame), frame)].filter((b) => b !== null);
679
+ const figcaption = findDeep(el, (e) => e.name === "figcaption");
680
+ const [img, ...others] = findAll(el, (e) => e.name === "img" && !(figcaption && findDeep(figcaption, (x) => x === e)));
681
+ if (!img) return blocks(el.children);
682
+ const link = linkAround(el, img);
683
+ const caption = figcaption ? captionText(figcaption, images) : "";
684
+ const image = imageOf(img, {
685
+ ...caption ? { caption } : {},
686
+ ...link ? { link } : {},
687
+ ...align ? { align } : {}
688
+ });
689
+ const rest = others.map((o) => ({
690
+ img: o,
691
+ ...linkAround(el, o) ? { link: linkAround(el, o) } : {}
692
+ }));
693
+ return [
694
+ ...image ? [image] : [],
695
+ ...hoistedBlocks(rest, "figure"),
696
+ ...hoistedBlocks(images, "caption")
697
+ ];
698
+ }
699
+ case "img": {
700
+ const image = imageOf(el);
701
+ return image ? [image] : [];
702
+ }
703
+ case "iframe": return [embedBlock(embedUrlOf(el), el)].filter((b) => b !== null);
704
+ case "table":
705
+ audit(el);
706
+ return [tableOf(el, images), ...hoistedBlocks(images, "cell")];
707
+ case "hr": return [{ type: "divider" }];
708
+ default: {
709
+ const iframely = el.attribs.class?.includes("iframely") ? findDeep(el, (e) => e.attribs["data-iframely-url"] !== void 0 || e.name === "iframe") : void 0;
710
+ if (iframely) return [embedBlock(embedUrlOf(iframely), iframely)].filter((b) => b !== null);
711
+ if (el.attribs["data-filename"] === void 0) audit(el);
712
+ return blocks(el.children);
713
+ }
714
+ }
715
+ };
716
+ return {
717
+ body: normalizeBody(blocks(parseDocument(html, { decodeEntities: true }).children)),
718
+ dropped: [...counts].map(([what, count]) => ({
719
+ what,
720
+ count
721
+ }))
722
+ };
723
+ }
724
+ // ../cms-contract/src/app.ts
725
+ /** サーバーの側の宣言(コレクションの確認だけ) */
726
+ const defineAppServer = (manifest) => manifest;
727
+ // ../cms-contract/src/appProblems.ts
728
+ /** 問題の表から、問題を作る関数(差し込みの不足は型の誤り) */
729
+ function appProblems(tables) {
730
+ const of = (code, params) => ({
731
+ code,
732
+ ...Object.keys(params).length > 0 && { params },
733
+ message: formatMessage(tables.en[code], params, "en")
734
+ });
735
+ return {
736
+ problem: (code, ...params) => of(code, params[0] ?? {}),
737
+ /** zod の message のように文字列しか運べない所の既定の形(fromText で問題に戻す) */
738
+ text: (code, ...params) => problemText({
739
+ code,
740
+ params: params[0] ?? {}
741
+ }),
742
+ /** 既定の形の文の問題(表の外の文は message だけ) */
743
+ fromText: (text) => {
744
+ const p = problemOf(text);
745
+ return p && Object.hasOwn(tables.en, p.code) ? of(p.code, p.params) : { message: text };
746
+ }
747
+ };
748
+ }
749
+ // ../cms-contract/src/import.ts
750
+ /** 1回のリクエストで取り込む件数の上限(画像の取得の時間と、本文の大きさの上限 JSON_BODY_MAX のため) */
751
+ const IMPORT_ITEMS_MAX = 50;
752
+ /** 参照の項目の値で、同じ取り出し元の元の ID を指す形の印({ "$origin": "<元の ID>" }) */
753
+ const ORIGIN_REF = "$origin";
754
+ /** 顧客フィールドの値で、人の元の ID を指す形の印({ "$customer": "<元の ID>" }) */
755
+ const CUSTOMER_REF = "$customer";
756
+ /** 元の ID を指す値の元の ID(印 marker の1つのキーで値が文字列、ほかは null) */
757
+ function originIdOf(value, marker) {
758
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
759
+ const records = Object.entries(value);
760
+ return records.length === 1 && records[0][0] === marker && typeof records[0][1] === "string" ? records[0][1] : null;
761
+ }
762
+ const isOriginRef = (v) => originIdOf(v, ORIGIN_REF) !== null;
763
+ /** 元の ID を指す参照({ "$origin" })を除いた値(まとまりの中と配列の要素も。循環の1段目の項目) */
764
+ function withoutOrigins(value) {
765
+ if (Array.isArray(value)) return value.filter((v) => !isOriginRef(v)).map(withoutOrigins);
766
+ if (typeof value !== "object" || value === null) return value;
767
+ return Object.fromEntries(Object.entries(value).flatMap(([k, v]) => isOriginRef(v) ? [] : [[k, withoutOrigins(v)]]));
768
+ }
769
+ /** 入力の形のレコードを取り込みの1件に(at は報告の位置、phase はコレクションどうしの循環のフェーズで、1段目は参照を除く) */
770
+ const importItemOfRow = (line, at, phase) => ({
771
+ collection: line.collection,
772
+ ...line.new_id !== void 0 && { id: line.new_id },
773
+ fields: phase === 1 ? withoutOrigins(line.fields) : line.fields,
774
+ source: at,
775
+ origin_id: line.id,
776
+ ...phase !== void 0 && { phase },
777
+ ...line.created_at !== void 0 && { created_at: line.created_at }
778
+ });
779
+ /** 取り込む ID(前後の空白を除き小文字に、changed は大文字を含んでいた) */
780
+ function importIdOf(raw) {
781
+ const trimmed = raw.trim();
782
+ const id = trimmed.toLowerCase();
783
+ return {
784
+ id,
785
+ changed: id !== trimmed
786
+ };
787
+ }
788
+ /** 関係が循環する API のエンドポイント(関係をたどって自分に戻る、自分への関係を含む。参照なしと参照ありの2フェーズで取り込む) */
789
+ function cyclicCollections(apis) {
790
+ const reaches = (from, goal, seen) => Object.values(from.relations).some((target) => {
791
+ if (target === goal) return true;
792
+ if (seen.has(target)) return false;
793
+ seen.add(target);
794
+ const next = apis.find((x) => x.endpoint === target);
795
+ return next ? reaches(next, goal, seen) : false;
796
+ });
797
+ return new Set(apis.filter((a) => reaches(a, a.endpoint, /* @__PURE__ */ new Set())).map((a) => a.endpoint));
798
+ }
799
+ // src/app.ts
800
+ /** 相関の ID のヘッダー(CMS の応答とログと同じ名前) */
801
+ const REQUEST_ID_HEADER = "x-request-id";
802
+ /** 受け取る相関の ID の形(ほかは新しい UUID) */
803
+ const REQUEST_ID_FORM = /^[A-Za-z0-9._:-]{1,128}$/;
804
+ /** 検査の結果の形にそろえる(code の既定は invalid、severity の既定は error) */
805
+ const appIssue = (i) => ({
806
+ path: i.path,
807
+ code: i.code ?? "invalid",
808
+ message: i.message,
809
+ ...i.params && { params: i.params },
810
+ severity: i.severity ?? "error"
811
+ });
812
+ /** エラーの応答(CMS の API と同じ形、相関の ID は要求の x-request-id。extra は issues などの足し) */
813
+ function appFailure(request, code, params, extra = {}) {
814
+ const given = request.headers.get(REQUEST_ID_HEADER);
815
+ const requestId = given && REQUEST_ID_FORM.test(given) ? given : crypto.randomUUID();
816
+ return Response.json({
817
+ ...errorBodyFor(code, requestId, params),
818
+ ...extra,
819
+ requestId
820
+ }, {
821
+ status: ERROR_STATUS[code],
822
+ headers: { [REQUEST_ID_HEADER]: requestId }
823
+ });
824
+ }
825
+ export { AUDIENCES, CUSTOMER_REF, CUSTOMER_STATUSES, IMPORT_ITEMS_MAX, JSON_BODY_MAX, ORIGIN_REF, appFailure, appIssue, appProblems, bodyFromHtml, cyclicCollections, defineAppServer, importIdOf, importItemOfRow, isPublishable };