grimoire3d 0.0.0 → 0.1.0-dev.9361940

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 (153) hide show
  1. package/README.md +23 -123
  2. package/cli/cli.js +350 -0
  3. package/cli/content.js +403 -0
  4. package/cli/cover-options.d.ts +15 -0
  5. package/cli/cover-options.js +105 -0
  6. package/cli/head-file.js +133 -0
  7. package/cli/library-footer.d.ts +10 -0
  8. package/cli/library-footer.js +21 -0
  9. package/cli/markdown.js +139 -0
  10. package/cli/markup.d.ts +3 -0
  11. package/cli/markup.js +60 -0
  12. package/cli/routes.d.ts +6 -0
  13. package/cli/routes.js +19 -0
  14. package/cli/static-pages.js +481 -0
  15. package/cli/video-url.d.ts +5 -0
  16. package/cli/video-url.js +51 -0
  17. package/dist/BookViewer-GXuBR_bC.js +339 -0
  18. package/dist/components/BookModel.d.ts +20 -0
  19. package/dist/components/BookViewer.d.ts +17 -0
  20. package/dist/components/index.d.ts +2 -0
  21. package/dist/components/index.js +1 -0
  22. package/dist/grimoire3d.css +3 -0
  23. package/dist/index.d.ts +8 -0
  24. package/dist/index.js +1 -0
  25. package/dist/main.d.ts +1 -0
  26. package/dist/site/assets/BookReader-DudAXdp3.js +2 -0
  27. package/dist/site/assets/BookScene-DiAiTP9V.js +4294 -0
  28. package/dist/site/assets/MermaidDiagram-2mwgnVgM.js +44 -0
  29. package/dist/site/assets/PageMarkdown-ENP_TuEU.js +37 -0
  30. package/dist/site/assets/abnfDiagram-S7VZAUFO-CPn8kyCz.js +1 -0
  31. package/dist/site/assets/alcove-cyX4Tv3E.avif +0 -0
  32. package/dist/site/assets/architecture-WOLXFQ4H-Czsdswwl.js +1 -0
  33. package/dist/site/assets/architectureDiagram-YGSQEFNW-C_9i0B1c.js +36 -0
  34. package/dist/site/assets/blockDiagram-APKKGZFW-B4TEuCLK.js +140 -0
  35. package/dist/site/assets/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
  36. package/dist/site/assets/c4Diagram-KJ2TOQU5-CH_Uv_Fz.js +38 -0
  37. package/dist/site/assets/catalog-07-UR91A.avif +0 -0
  38. package/dist/site/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
  39. package/dist/site/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
  40. package/dist/site/assets/chunk-6MUBBAK2-C4kh7Z3T.js +2 -0
  41. package/dist/site/assets/chunk-AXFCVDMN-B_taAhLl.js +62 -0
  42. package/dist/site/assets/chunk-BG4553SI-Cn6LMB1q.js +129 -0
  43. package/dist/site/assets/chunk-DQASFODM-BsDpj_0k.js +10 -0
  44. package/dist/site/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
  45. package/dist/site/assets/chunk-HFQBQIA3-DLIuoeT3.js +1 -0
  46. package/dist/site/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
  47. package/dist/site/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
  48. package/dist/site/assets/chunk-KQLB2F66-AFzkv6d3.js +2 -0
  49. package/dist/site/assets/chunk-KS47KZWR-DjywFwRy.js +1 -0
  50. package/dist/site/assets/chunk-LEVMRMFU-BrDnuOnu.js +1 -0
  51. package/dist/site/assets/chunk-M57EVK23-BikTvdAr.js +1 -0
  52. package/dist/site/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
  53. package/dist/site/assets/chunk-OY52VR5R-Nk28J3qR.js +88 -0
  54. package/dist/site/assets/chunk-QB3YPZ4I-OgBmaa_y.js +213 -0
  55. package/dist/site/assets/chunk-R3KSYZAL-CdwBI4dZ.js +1 -0
  56. package/dist/site/assets/chunk-STOXU5M4-BZAF-H6I.js +72 -0
  57. package/dist/site/assets/chunk-TU62E6Q5-C6WlEf5k.js +1 -0
  58. package/dist/site/assets/chunk-X4PVCEUW-BYepnSMY.js +1 -0
  59. package/dist/site/assets/chunk-XXDRQBXY-DN9ONrwM.js +1 -0
  60. package/dist/site/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
  61. package/dist/site/assets/classDiagram-v2-L25IHNXF-ChtKa3P5.js +217 -0
  62. package/dist/site/assets/cose-bilkent-JH36ORCC-kNFKiV_g.js +1 -0
  63. package/dist/site/assets/cover-blue-Bh8Xp4T5.avif +0 -0
  64. package/dist/site/assets/cover-blue-mobile-YcuYIg0I.avif +0 -0
  65. package/dist/site/assets/cover-green-By13JjUx.avif +0 -0
  66. package/dist/site/assets/cover-green-mobile-BNTgNt7v.avif +0 -0
  67. package/dist/site/assets/cover-red-mobile-DHOCz3wI.avif +0 -0
  68. package/dist/site/assets/cover-red-qvZEk3PI.avif +0 -0
  69. package/dist/site/assets/cynefin-EF2NZ3EQ-lzZSPZ0f.js +1 -0
  70. package/dist/site/assets/cynefinDiagram-L36ESFBY-sq_xQVi7.js +62 -0
  71. package/dist/site/assets/cytoscape.esm-nTrd5EVq.js +321 -0
  72. package/dist/site/assets/d3-scale-CElPUUud.js +1 -0
  73. package/dist/site/assets/d3-selection-DIUqvUVB.js +1 -0
  74. package/dist/site/assets/dagre-SPEB5RXH-DqoO0sGc.js +4 -0
  75. package/dist/site/assets/diagram-3B7GL73J-M26Lh06i.js +200 -0
  76. package/dist/site/assets/diagram-DNEMPZML-ZZQRAEJq.js +24 -0
  77. package/dist/site/assets/diagram-L3MUGCDN-CjsMyH3f.js +41 -0
  78. package/dist/site/assets/diagram-LA4NIPWU-B1tD-62P.js +24 -0
  79. package/dist/site/assets/diagram-MHCOC4E2-B0jKQ9HQ.js +3 -0
  80. package/dist/site/assets/diagram-XPFHEONP-BelYHEVT.js +30 -0
  81. package/dist/site/assets/dist-ppXrr7du.js +1 -0
  82. package/dist/site/assets/ebnfDiagram-UZSYDL2S-BlM2_kcX.js +1 -0
  83. package/dist/site/assets/elk-XV6XMXQE-DGFBMQyB.js +27 -0
  84. package/dist/site/assets/erDiagram-EYBAPDEH-BYeh4LuC.js +99 -0
  85. package/dist/site/assets/es-YYQGP5MR.js +2 -0
  86. package/dist/site/assets/eventmodeling-K75KTNOO-CJHo0aIb.js +1 -0
  87. package/dist/site/assets/favicon-C9YRygge.png +0 -0
  88. package/dist/site/assets/flowDiagram-MALGAFMO-4xnU_Dv7.js +1 -0
  89. package/dist/site/assets/ganttDiagram-23REXITY-BlApGIuJ.js +292 -0
  90. package/dist/site/assets/gitGraph-VSP46ZUC-Dwcg1pSB.js +1 -0
  91. package/dist/site/assets/gitGraphDiagram-STJD7YCW-BJXE5oWq.js +106 -0
  92. package/dist/site/assets/graphlib-DS17s2tU.js +1 -0
  93. package/dist/site/assets/index-D4HJC4TW.js +2 -0
  94. package/dist/site/assets/index-fT8cj2JB.css +1 -0
  95. package/dist/site/assets/info-OHQRW6UA-BEW1vxOL.js +1 -0
  96. package/dist/site/assets/infoDiagram-KBXO4MBG-DseLOSIt.js +2 -0
  97. package/dist/site/assets/ishikawaDiagram-M35NCXHK-D3nXJVhq.js +70 -0
  98. package/dist/site/assets/journeyDiagram-OPLOXDXO-Dwmf_2yx.js +139 -0
  99. package/dist/site/assets/jsxRuntime-WpZ1bTm4.js +1 -0
  100. package/dist/site/assets/kanban-definition-BZKZHSQY-D8hRo5NB.js +89 -0
  101. package/dist/site/assets/katex-MEuSR36T.js +257 -0
  102. package/dist/site/assets/mermaid-parser.core-CRzqu-zz.js +7 -0
  103. package/dist/site/assets/mindmap-definition-I2QAS2VK-C_KsVBJE.js +98 -0
  104. package/dist/site/assets/mobile-cabinet-BUr9vtlo.avif +0 -0
  105. package/dist/site/assets/mobile-cabinet-sm-pauJUfRH.avif +0 -0
  106. package/dist/site/assets/mobile-paper-DF0CuLes.avif +0 -0
  107. package/dist/site/assets/mobile-paper-sm-77eJHX6h.avif +0 -0
  108. package/dist/site/assets/open-nWGD2hyb.glb +0 -0
  109. package/dist/site/assets/packet-JDAUHWVQ-vLUg6kma.js +1 -0
  110. package/dist/site/assets/pegDiagram-FD4XPPHP-CsAJzLE2.js +1 -0
  111. package/dist/site/assets/pie-XZMESJXO-C5OiZ5I_.js +1 -0
  112. package/dist/site/assets/pieDiagram-J4AKIIQI-C0Z-TuM1.js +39 -0
  113. package/dist/site/assets/quadrantDiagram-XTYBLID2-BfwNOGbp.js +7 -0
  114. package/dist/site/assets/radar-ABXABTNO-DlOAhNu8.js +1 -0
  115. package/dist/site/assets/railroad-I3PHUGI6-BUpxKd8T.js +1 -0
  116. package/dist/site/assets/railroad-abnf-LNEFI6M7-BsS9Vk34.js +1 -0
  117. package/dist/site/assets/railroad-ebnf-DWYD2UWJ-Cx8TyVDx.js +1 -0
  118. package/dist/site/assets/railroad-peg-JR7BVNR7-C2s9CJ5A.js +1 -0
  119. package/dist/site/assets/railroadDiagram-MRQ6NCDL-nXlyo8yi.js +1 -0
  120. package/dist/site/assets/requirementDiagram-Y7LWUY3S-C_jfLPMO.js +84 -0
  121. package/dist/site/assets/resting-amber-BSjsErIg.avif +0 -0
  122. package/dist/site/assets/resting-black-DDQ4XMJI.avif +0 -0
  123. package/dist/site/assets/resting-blue-DDOT8IQ8.avif +0 -0
  124. package/dist/site/assets/resting-green-DkRrV8eX.avif +0 -0
  125. package/dist/site/assets/resting-ivory-CEqoG5yI.avif +0 -0
  126. package/dist/site/assets/resting-plum-B-8tkSmP.avif +0 -0
  127. package/dist/site/assets/resting-red-D0hLpMpz.avif +0 -0
  128. package/dist/site/assets/resting-slate-CH8Z_Rky.avif +0 -0
  129. package/dist/site/assets/resting-teal-CGt6jFmT.avif +0 -0
  130. package/dist/site/assets/resting-walnut-t3WgHgUO.avif +0 -0
  131. package/dist/site/assets/rough.esm-Dy-Kn_BL.js +1 -0
  132. package/dist/site/assets/sankeyDiagram-WGJFYUXA-DiLclJAZ.js +40 -0
  133. package/dist/site/assets/search-stage-B8cH1AE_.avif +0 -0
  134. package/dist/site/assets/search-stage-mobile-BZ58ew4F.avif +0 -0
  135. package/dist/site/assets/sequenceDiagram-5NDRBG5M-B9sM-5ZJ.js +169 -0
  136. package/dist/site/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
  137. package/dist/site/assets/stateDiagram-v2-FJ4FJNEA-Dkis5g_v.js +291 -0
  138. package/dist/site/assets/swimlanes-UMQ2AFYF-DB6kjOv7.js +1 -0
  139. package/dist/site/assets/swimlanesDiagram-OMPLPG4O-IMFdS7Zf.js +8 -0
  140. package/dist/site/assets/three-core-B1q6MRTI.js +5 -0
  141. package/dist/site/assets/timeline-definition-SEUHKTQE-2CMebGt0.js +120 -0
  142. package/dist/site/assets/treeView-D4JQ5SDB-BZ52mw7_.js +1 -0
  143. package/dist/site/assets/treemap-SAJKECNS-C-pobFuU.js +1 -0
  144. package/dist/site/assets/usecaseDiagram-AFA6YXN4-BACx6ppv.js +328 -0
  145. package/dist/site/assets/vennDiagram-NNMJR3RB-B8NLdumI.js +34 -0
  146. package/dist/site/assets/wardley-7MLQ67FV-BWJ-dzdI.js +1 -0
  147. package/dist/site/assets/wardleyDiagram-S7YFUSEH-WKBfzjEM.js +78 -0
  148. package/dist/site/assets/xychartDiagram-GGNKY7IX-BUciDj2U.js +7 -0
  149. package/dist/site/index.html +15 -0
  150. package/dist/types/index.d.ts +163 -0
  151. package/package.json +51 -28
  152. package/dist/grimoire3d.js +0 -504
  153. package/dist/grimoire3d.umd.cjs +0 -27
package/cli/content.js ADDED
@@ -0,0 +1,403 @@
1
+ import { readdir, readFile, realpath, stat } from "node:fs/promises";
2
+ import { extname, isAbsolute, join, posix, relative, sep } from "node:path";
3
+ import { parse } from "yaml";
4
+ import { coverOptions } from "./cover-options.js";
5
+ import { readMarkdown } from "./markdown.js";
6
+
7
+ const rootId = "_root";
8
+ const languages = new Set(["en", "ja"]);
9
+ const collator = new Intl.Collator("en", { numeric: true });
10
+
11
+ function sortNames(a, b) {
12
+ return collator.compare(a, b) || (a < b ? -1 : Number(a > b));
13
+ }
14
+
15
+ function titleFromPath(path) {
16
+ return path.replace(/^\d+[-_]+/, "").replace(/[-_]+/g, " ");
17
+ }
18
+
19
+ function visibleEntry(name) {
20
+ return !name.startsWith(".") && !name.startsWith("_") && name !== "assets";
21
+ }
22
+
23
+ function isMarkdown(name) {
24
+ return extname(name).toLowerCase() === ".md";
25
+ }
26
+
27
+ function pageId(path) {
28
+ return path.replace(/\.md$/i, "");
29
+ }
30
+
31
+ function encodePath(path) {
32
+ return path.split("/").map(encodeURIComponent).join("/");
33
+ }
34
+
35
+ async function readPages(root, directory, nested) {
36
+ const entries = (
37
+ await readdir(join(root, directory), { withFileTypes: true })
38
+ )
39
+ .filter((entry) => visibleEntry(entry.name))
40
+ .sort((a, b) => sortNames(a.name, b.name));
41
+ const pages = [];
42
+ for (const entry of entries) {
43
+ const path = posix.join(directory, entry.name);
44
+ if (entry.isDirectory() && nested) {
45
+ pages.push(...(await readPages(root, path, true)));
46
+ } else if (entry.isFile() && isMarkdown(entry.name)) {
47
+ const markdown = await readFile(join(root, path), "utf8");
48
+ pages.push({ path, ...readMarkdown(markdown) });
49
+ }
50
+ }
51
+ return pages;
52
+ }
53
+
54
+ function comparePages(a, b) {
55
+ // Treat index.md as the first page of its folder, without reordering the folders.
56
+ return sortNames(
57
+ a.path.replace(/(^|\/)index\.md$/i, "$1!index.md"),
58
+ b.path.replace(/(^|\/)index\.md$/i, "$1!index.md")
59
+ );
60
+ }
61
+
62
+ function describeBook(pages, folder, fallback) {
63
+ const index = pages.find(
64
+ (page) =>
65
+ page.path.toLowerCase() === posix.join(folder, "index.md").toLowerCase()
66
+ );
67
+ const id = folder || rootId;
68
+ const title = index?.metadata.title || index?.heading || fallback;
69
+ const cover = index?.metadata.cover;
70
+ if (cover && !Object.hasOwn(coverOptions, cover)) {
71
+ throw new Error(
72
+ `${index.path}: cover "${cover}" must be one of ${Object.keys(coverOptions).join(", ")}.`
73
+ );
74
+ }
75
+ const groups = new Map(
76
+ pages
77
+ .filter((page) => /\/index\.md$/i.test(page.path))
78
+ .map((page) => [posix.dirname(page.path), page.heading])
79
+ );
80
+ return {
81
+ id,
82
+ title,
83
+ cover,
84
+ chapters: pages.sort(comparePages).map((page) => {
85
+ const group = posix.dirname(page.path);
86
+ const inBook = folder ? page.path.slice(folder.length + 1) : page.path;
87
+ const file = posix.basename(pageId(page.path));
88
+ const groupTitle =
89
+ group !== (folder || ".")
90
+ ? groups.get(group) || titleFromPath(posix.basename(group))
91
+ : undefined;
92
+ return {
93
+ id: pageId(inBook),
94
+ path: page.path,
95
+ title:
96
+ page.metadata.title ||
97
+ page.heading ||
98
+ (file === "index" ? groupTitle || title : titleFromPath(file)),
99
+ group: groupTitle,
100
+ sheets: page.sheets,
101
+ metadata: page.metadata,
102
+ hasTitle: Boolean(page.heading),
103
+ excerpt: page.excerpt,
104
+ markdown: page.markdown,
105
+ headingIds: page.headingIds,
106
+ urls: page.urls,
107
+ };
108
+ }),
109
+ };
110
+ }
111
+
112
+ function inside(parent, path) {
113
+ const fromParent = relative(parent, path);
114
+ return (
115
+ fromParent !== ".." &&
116
+ !fromParent.startsWith(`..${sep}`) &&
117
+ !isAbsolute(fromParent)
118
+ );
119
+ }
120
+
121
+ export async function readSettings(cwd) {
122
+ let source;
123
+ try {
124
+ source = await readFile(join(cwd, "grimoire.yml"), "utf8");
125
+ } catch (error) {
126
+ if (error.code === "ENOENT") return { locale: "en" };
127
+ throw error;
128
+ }
129
+ let settings;
130
+ try {
131
+ settings = parse(source) ?? {};
132
+ } catch {
133
+ throw new Error(
134
+ "grimoire.yml contains invalid YAML / grimoire.yml の YAML を確認してください。"
135
+ );
136
+ }
137
+ const {
138
+ locale = "en",
139
+ title,
140
+ outDir,
141
+ description,
142
+ siteUrl,
143
+ socialImage,
144
+ author,
145
+ magicMode,
146
+ footer,
147
+ headFile,
148
+ } = settings;
149
+ if (
150
+ typeof settings !== "object" ||
151
+ Array.isArray(settings) ||
152
+ !languages.has(locale)
153
+ ) {
154
+ throw new Error(
155
+ "grimoire.yml: set locale to en or ja / locale は en または ja を指定してください。"
156
+ );
157
+ }
158
+ if (
159
+ (title !== undefined && typeof title !== "string") ||
160
+ (outDir !== undefined && (typeof outDir !== "string" || !outDir.trim()))
161
+ ) {
162
+ throw new Error(
163
+ "grimoire.yml: title and outDir must be text / title と outDir は文字列で指定してください。"
164
+ );
165
+ }
166
+ if (
167
+ [description, siteUrl, socialImage, author].some(
168
+ (value) =>
169
+ value !== undefined && (typeof value !== "string" || !value.trim())
170
+ ) ||
171
+ (magicMode !== undefined && typeof magicMode !== "boolean")
172
+ )
173
+ throw new Error(
174
+ "grimoire.yml: description, siteUrl, socialImage and author must be text, and magicMode must be true or false."
175
+ );
176
+
177
+ return {
178
+ locale,
179
+ title,
180
+ outDir,
181
+ description,
182
+ socialImage,
183
+ author,
184
+ magicMode,
185
+ footer: readFooter(footer),
186
+ headFile,
187
+ siteUrl: siteUrl && normalizeSiteUrl(siteUrl),
188
+ };
189
+ }
190
+
191
+ function absoluteWebUrl(value) {
192
+ try {
193
+ const url = new URL(value);
194
+ if (
195
+ ["http:", "https:"].includes(url.protocol) &&
196
+ !url.username &&
197
+ !url.password
198
+ )
199
+ return url;
200
+ } catch {}
201
+ return undefined;
202
+ }
203
+
204
+ function readFooter(footer) {
205
+ if (footer === undefined) return undefined;
206
+ if (typeof footer?.label !== "string" || !footer.label.trim()) {
207
+ throw new Error("grimoire.yml: footer.label must be non-empty text.");
208
+ }
209
+ const { label, url } = footer;
210
+ if (url === undefined) return { label: label.trim() };
211
+ const link = typeof url === "string" ? absoluteWebUrl(url) : undefined;
212
+ if (!link) {
213
+ throw new Error(
214
+ "grimoire.yml: footer.url must be an absolute HTTP(S) URL."
215
+ );
216
+ }
217
+ return { label: label.trim(), url: link.href };
218
+ }
219
+
220
+ function normalizeSiteUrl(value) {
221
+ const url = absoluteWebUrl(value);
222
+ if (!url || url.search || url.hash) {
223
+ throw new Error(
224
+ "grimoire.yml: siteUrl must be an absolute HTTP(S) URL without a query or fragment."
225
+ );
226
+ }
227
+ if (!url.pathname.endsWith("/")) url.pathname += "/";
228
+ return url.href;
229
+ }
230
+
231
+ async function loadLink(url, page, links, docs, assets, issues) {
232
+ if (/^(?:[a-z][a-z\d+.-]*:|\/)/i.test(url)) return undefined;
233
+ const match = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(url);
234
+ if (!match) return undefined;
235
+ const [, path, query = "", anchor = ""] = match;
236
+ let decoded;
237
+ try {
238
+ decoded = decodeURIComponent(path);
239
+ } catch {
240
+ issues.push(`${page.path}: ${url}`);
241
+ return "";
242
+ }
243
+ const target = path
244
+ ? posix.normalize(posix.join(posix.dirname(page.path), decoded))
245
+ : page.path;
246
+ const targetPage =
247
+ links.get(target) ?? links.get(posix.join(target, "index.md"));
248
+ if (targetPage) {
249
+ const fragment = anchor.slice(1);
250
+ if (!fragment) return targetPage;
251
+ try {
252
+ return `${targetPage}?anchor=${encodeURIComponent(decodeURIComponent(fragment))}`;
253
+ } catch {
254
+ return `${targetPage}?anchor=${encodeURIComponent(fragment)}`;
255
+ }
256
+ }
257
+ if (target.startsWith("../") || target === ".." || !path) {
258
+ issues.push(`${page.path}: ${url}`);
259
+ return "";
260
+ }
261
+ try {
262
+ const absolute = await realpath(join(docs, target));
263
+ if (!inside(docs, absolute) || !(await stat(absolute)).isFile()) {
264
+ issues.push(`${page.path}: ${url}`);
265
+ return "";
266
+ }
267
+ assets.set(target, absolute);
268
+ return `./content/${encodePath(target)}${query}${anchor}`;
269
+ } catch (error) {
270
+ if (!["ENOENT", "ENOTDIR"].includes(error.code)) throw error;
271
+ issues.push(`${page.path}: ${url}`);
272
+ return "";
273
+ }
274
+ }
275
+
276
+ export async function readLibrary(cwd, settings) {
277
+ const { locale, title } = settings;
278
+ const docs = await realpath(join(cwd, "docs")).catch((error) => {
279
+ if (error.code === "ENOENT") throw missingMarkdown(locale);
280
+ throw error;
281
+ });
282
+ const entries = (await readdir(docs, { withFileTypes: true }))
283
+ .filter((entry) => entry.isDirectory() && visibleEntry(entry.name))
284
+ .sort((a, b) => sortNames(a.name, b.name));
285
+ const books = [];
286
+ const rootPages = await readPages(docs, "", false);
287
+ if (rootPages.length)
288
+ books.push(describeBook(rootPages, "", title ?? "Grimoire3D"));
289
+
290
+ for (const { name } of entries) {
291
+ const pages = await readPages(docs, name, true);
292
+ if (pages.length)
293
+ books.push(describeBook(pages, name, titleFromPath(name)));
294
+ }
295
+ if (!books.length) throw missingMarkdown(locale);
296
+
297
+ const shelf = books.some((book) => book.id !== rootId);
298
+ const assets = new Map();
299
+ const links = new Map();
300
+ const issues = [];
301
+ for (const book of books) {
302
+ for (const page of book.chapters) {
303
+ links.set(
304
+ page.path,
305
+ `#/${encodeURIComponent(book.id)}/${encodeURIComponent(page.id)}`
306
+ );
307
+ }
308
+ }
309
+ async function socialAsset(value, path) {
310
+ if (!value) return undefined;
311
+ const remote = absoluteWebUrl(value);
312
+ if (remote) return remote.href;
313
+ if (/^(?:[a-z][a-z\d+.-]*:|\/|#)/i.test(value)) {
314
+ issues.push(`${path}: ${value}`);
315
+ return undefined;
316
+ }
317
+ const image = await loadLink(
318
+ value,
319
+ { path },
320
+ new Map(),
321
+ docs,
322
+ assets,
323
+ issues
324
+ );
325
+ return image || undefined;
326
+ }
327
+ const socialImage = await socialAsset(settings.socialImage, "index.md");
328
+ const publishedBooks = [];
329
+ const documents = [];
330
+ for (const { id, title, cover, chapters } of books) {
331
+ const pages = [];
332
+ for (const chapter of chapters) {
333
+ const {
334
+ id: chapterId,
335
+ title: chapterTitle,
336
+ group,
337
+ sheets,
338
+ urls,
339
+ } = chapter;
340
+ const paths = await Promise.all(
341
+ urls.map(async (url) => [
342
+ url,
343
+ await loadLink(url, chapter, links, docs, assets, issues),
344
+ ])
345
+ );
346
+ const chapterLinks = Object.fromEntries(
347
+ paths.filter(([, path]) => path !== undefined)
348
+ );
349
+ for (const sheet of sheets) {
350
+ pages.push({
351
+ id: chapterId,
352
+ title: chapterTitle,
353
+ group,
354
+ ...sheet,
355
+ links: chapterLinks,
356
+ });
357
+ }
358
+ documents.push({
359
+ book: id,
360
+ chapter: chapterId,
361
+ title: chapterTitle,
362
+ hasTitle: chapter.hasTitle,
363
+ description:
364
+ chapter.metadata.description ||
365
+ chapter.excerpt ||
366
+ settings.description,
367
+ socialImage:
368
+ (await socialAsset(chapter.metadata.socialImage, chapter.path)) ||
369
+ socialImage,
370
+ markdown: chapter.markdown,
371
+ headingIds: chapter.headingIds,
372
+ links: chapterLinks,
373
+ });
374
+ }
375
+ publishedBooks.push({ id, title, cover, pages });
376
+ }
377
+ return {
378
+ manifest: {
379
+ locale,
380
+ shelf,
381
+ title: title ?? "Grimoire3D",
382
+ description: settings.description,
383
+ siteUrl: settings.siteUrl,
384
+ socialImage,
385
+ author: settings.author,
386
+ magicMode: settings.magicMode,
387
+ footer: settings.footer,
388
+ books: books.map(({ id, title, cover }) => ({ id, title, cover })),
389
+ },
390
+ books: publishedBooks,
391
+ documents,
392
+ assets,
393
+ issues: [...new Set(issues)].sort(sortNames),
394
+ };
395
+ }
396
+
397
+ function missingMarkdown(locale) {
398
+ return new Error(
399
+ locale === "ja"
400
+ ? "docs/ に Markdown がありません。docs/index.md に「# 最初の本」と書いてください。"
401
+ : 'No Markdown in docs/. Start with "# My first book" in docs/index.md.'
402
+ );
403
+ }
@@ -0,0 +1,15 @@
1
+ export interface CoverOption {
2
+ base: "red" | "green" | "blue";
3
+ filter: string;
4
+ edge: string;
5
+ mobile: string;
6
+ hue: string;
7
+ dye: string;
8
+ light: string;
9
+ lettering?: string;
10
+ }
11
+ export const coverOptions: Readonly<Record<string, CoverOption>> & {
12
+ red: CoverOption;
13
+ };
14
+
15
+ export function selectCover(id: string, requested?: string): string;
@@ -0,0 +1,105 @@
1
+ export const coverOptions = {
2
+ red: {
3
+ base: "red",
4
+ filter: "brightness(1)",
5
+ edge: "#6d2022",
6
+ mobile: "none",
7
+ hue: "0deg",
8
+ dye: "",
9
+ light: ".22,.064,.060",
10
+ },
11
+ green: {
12
+ base: "green",
13
+ filter: "brightness(1)",
14
+ edge: "#424624",
15
+ mobile: "hue-rotate(66deg) saturate(.62)",
16
+ hue: "72deg",
17
+ dye: ".57,.83,.25",
18
+ light: ".105,.128,.049",
19
+ },
20
+ blue: {
21
+ base: "blue",
22
+ filter: "brightness(1)",
23
+ edge: "#34314c",
24
+ mobile: "hue-rotate(248deg) saturate(.62)",
25
+ hue: "235deg",
26
+ dye: ".37,.51,.85",
27
+ light: ".08,.102,.164",
28
+ },
29
+ plum: {
30
+ base: "red",
31
+ filter: "hue-rotate(-47deg) saturate(.79)",
32
+ edge: "#572c50",
33
+ mobile: "hue-rotate(-47deg) saturate(.79)",
34
+ hue: "313deg",
35
+ dye: ".71,.31,.67",
36
+ light: ".15,.069,.14",
37
+ },
38
+ amber: {
39
+ base: "red",
40
+ filter: "hue-rotate(34deg) saturate(.85) brightness(1.28)",
41
+ edge: "#8a551e",
42
+ mobile: "hue-rotate(34deg) brightness(1.28)",
43
+ hue: "34deg",
44
+ dye: "1.24,.68,.20",
45
+ light: ".27,.17,.055",
46
+ },
47
+ teal: {
48
+ base: "green",
49
+ filter: "hue-rotate(63deg) saturate(.86) brightness(1.18)",
50
+ edge: "#245452",
51
+ mobile: "hue-rotate(152deg) saturate(.65)",
52
+ hue: "150deg",
53
+ dye: ".26,.81,.75",
54
+ light: ".06,.16,.15",
55
+ },
56
+ black: {
57
+ base: "blue",
58
+ filter: "grayscale(1) brightness(.82)",
59
+ edge: "#252321",
60
+ mobile: "grayscale(1) brightness(.68)",
61
+ hue: "0deg",
62
+ dye: ".38,.37,.36",
63
+ light: ".072,.07,.068",
64
+ },
65
+ ivory: {
66
+ base: "red",
67
+ filter: "grayscale(1) invert(.86) sepia(.33) brightness(1.05)",
68
+ edge: "#d9c5a3",
69
+ mobile: "grayscale(1) invert(.86) sepia(.33)",
70
+ hue: "32deg",
71
+ dye: "3.2,2.7,2.02",
72
+ light: ".38,.315,.22",
73
+ lettering: "#392518",
74
+ },
75
+ walnut: {
76
+ base: "red",
77
+ filter: "hue-rotate(22deg) saturate(.58) brightness(.78)",
78
+ edge: "#50301c",
79
+ mobile: "hue-rotate(22deg) saturate(.58) brightness(.78)",
80
+ hue: "23deg",
81
+ dye: ".70,.43,.25",
82
+ light: ".15,.09,.048",
83
+ },
84
+ slate: {
85
+ base: "blue",
86
+ filter: "grayscale(.66) brightness(1.42)",
87
+ edge: "#5e6570",
88
+ mobile: "hue-rotate(213deg) saturate(.24) brightness(1.23)",
89
+ hue: "220deg",
90
+ dye: ".75,.82,.91",
91
+ light: ".17,.19,.21",
92
+ },
93
+ };
94
+
95
+ const fallbackCovers = ["red", "blue", "green"];
96
+
97
+ /** @param {string} id @param {string} [requested] */
98
+ export function selectCover(id, requested) {
99
+ if (requested && Object.hasOwn(coverOptions, requested)) return requested;
100
+ let hash = 2166136261;
101
+ for (const character of id.replace(/^\d+[-_]+/, "")) {
102
+ hash = Math.imul(hash ^ (character.codePointAt(0) ?? 0), 16777619);
103
+ }
104
+ return fallbackCovers[(hash >>> 10) % fallbackCovers.length];
105
+ }
@@ -0,0 +1,133 @@
1
+ import { readFile, realpath, stat } from "node:fs/promises";
2
+ import { isAbsolute, relative, resolve, sep } from "node:path";
3
+ import { parse } from "parse5";
4
+
5
+ const managedMeta = new Set([
6
+ "author",
7
+ "description",
8
+ "grimoire-preview",
9
+ "grimoire-static",
10
+ "og:description",
11
+ "og:image",
12
+ "og:locale",
13
+ "og:site_name",
14
+ "og:title",
15
+ "og:type",
16
+ "og:url",
17
+ "twitter:card",
18
+ "viewport",
19
+ ]);
20
+ const headElements = new Set(["link", "meta", "script", "style"]);
21
+ const headStart = "<!doctype html><html><head>\n";
22
+
23
+ function managedTag(node) {
24
+ const attrs = Object.fromEntries(
25
+ node.attrs.map(({ name, value }) => [name, value])
26
+ );
27
+ if (node.tagName === "title" || node.tagName === "base")
28
+ return `<${node.tagName}>`;
29
+ if (node.tagName === "link") {
30
+ const rel = attrs.rel?.toLowerCase().trim().split(/\s+/) ?? [];
31
+ if (rel.includes("canonical")) return '<link rel="canonical">';
32
+ if (rel.includes("icon")) return '<link rel="icon">';
33
+ }
34
+ if (node.tagName === "meta") {
35
+ if ("charset" in attrs) return "<meta charset>";
36
+ if (attrs["http-equiv"]?.trim().toLowerCase() === "content-type")
37
+ return '<meta http-equiv="content-type">';
38
+ for (const attr of ["name", "property"]) {
39
+ const key = attrs[attr]?.trim().toLowerCase();
40
+ if (managedMeta.has(key)) return `<meta ${attr}="${key}">`;
41
+ }
42
+ }
43
+ }
44
+
45
+ function checkHeadFragment(markup, label) {
46
+ const fileError = (line, message) =>
47
+ new Error(`${label}:${Math.max(1, line - 1)}: ${message}`);
48
+ const document = parse(`${headStart}${markup}</head><body></body></html>`, {
49
+ sourceCodeLocationInfo: true,
50
+ onParseError(error) {
51
+ throw fileError(error.startLine, `Invalid HTML (${error.code}).`);
52
+ },
53
+ });
54
+ const html = document.childNodes.find(({ nodeName }) => nodeName === "html");
55
+ const head = html.childNodes.find(({ nodeName }) => nodeName === "head");
56
+ if (
57
+ head.sourceCodeLocation?.endTag?.startOffset !==
58
+ headStart.length + markup.length
59
+ ) {
60
+ throw new Error(
61
+ `${label}: Expected an HTML head fragment. Use metadata, links, scripts, or styles inside the file.`
62
+ );
63
+ }
64
+
65
+ for (const node of head.childNodes) {
66
+ if (
67
+ node.nodeName === "#comment" ||
68
+ (node.nodeName === "#text" && !node.value.trim())
69
+ )
70
+ continue;
71
+
72
+ const { startLine } = node.sourceCodeLocation;
73
+ const conflict = node.attrs && managedTag(node);
74
+ if (conflict) {
75
+ throw fileError(
76
+ startLine,
77
+ `${conflict} is managed by Grimoire3D. Use the existing site and chapter settings.`
78
+ );
79
+ }
80
+ if (!headElements.has(node.tagName)) {
81
+ throw fileError(
82
+ startLine,
83
+ "Expected a meta, link, script, or style tag."
84
+ );
85
+ }
86
+ }
87
+ }
88
+
89
+ function insideProject(project, path) {
90
+ const location = relative(project, path);
91
+ return !isAbsolute(location) && location.split(sep)[0] !== "..";
92
+ }
93
+
94
+ export async function readHeadFile(cwd, setting) {
95
+ if (setting === undefined) return "";
96
+ if (
97
+ typeof setting !== "string" ||
98
+ !setting.trim() ||
99
+ isAbsolute(setting) ||
100
+ /^[a-z][a-z\d+.-]*:/i.test(setting)
101
+ ) {
102
+ throw new Error(
103
+ "grimoire.yml: headFile must be a relative path within the project."
104
+ );
105
+ }
106
+
107
+ const label = `grimoire.yml: headFile ${JSON.stringify(setting)}`;
108
+ const project = await realpath(cwd);
109
+ const requested = resolve(project, setting);
110
+ const outside = () => new Error(`${label}: Use a path within the project.`);
111
+ if (!insideProject(project, requested)) throw outside();
112
+
113
+ const unreadable = (cause) =>
114
+ new Error(
115
+ `${label}: Cannot read the HTML file. Check that it exists and is readable.`,
116
+ { cause }
117
+ );
118
+ const path = await realpath(requested).catch((error) => {
119
+ throw unreadable(error);
120
+ });
121
+ if (!insideProject(project, path)) throw outside();
122
+
123
+ let markup;
124
+ try {
125
+ if (!(await stat(path)).isFile()) throw new Error("Expected a file.");
126
+ markup = (await readFile(path, "utf8")).replace(/^\uFEFF/, "");
127
+ } catch (error) {
128
+ throw unreadable(error);
129
+ }
130
+
131
+ checkHeadFragment(markup, label);
132
+ return markup;
133
+ }
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "react";
2
+
3
+ export interface LibraryPublisher {
4
+ label: string;
5
+ url?: string;
6
+ }
7
+
8
+ export function LibraryFooter(props: {
9
+ publisher?: LibraryPublisher;
10
+ }): JSX.Element;
@@ -0,0 +1,21 @@
1
+ import { createElement as element } from "react";
2
+
3
+ export function LibraryFooter({ publisher }) {
4
+ const label = publisher
5
+ ? element(
6
+ publisher.url ? "a" : "span",
7
+ { className: "library-publisher", href: publisher.url },
8
+ publisher.label
9
+ )
10
+ : null;
11
+ return element(
12
+ "footer",
13
+ { className: "library-footer" },
14
+ label,
15
+ element(
16
+ "a",
17
+ { href: "https://github.com/kkm-horikawa/grimoire3d" },
18
+ "Grimoire3D"
19
+ )
20
+ );
21
+ }