@dooc-dev/dooc 0.0.4 → 0.0.6

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 (231) hide show
  1. package/dist/cli.js +157 -58
  2. package/dist/code-worker.global.js +152 -0
  3. package/dist/code.global.js +101 -101
  4. package/dist/compiler/asset-references.d.ts +4 -0
  5. package/dist/compiler/asset-references.js +117 -0
  6. package/dist/compiler/bundle-urls.d.ts +1 -1
  7. package/dist/compiler/bundle-urls.js +17 -5
  8. package/dist/compiler/check.js +10 -5
  9. package/dist/compiler/code-languages.js +7 -2
  10. package/dist/compiler/compile.d.ts +5 -0
  11. package/dist/compiler/compile.js +117 -12
  12. package/dist/compiler/config.js +2 -1
  13. package/dist/compiler/dxf.d.ts +1 -0
  14. package/dist/compiler/dxf.js +81 -0
  15. package/dist/compiler/export.d.ts +4 -0
  16. package/dist/compiler/export.js +147 -0
  17. package/dist/compiler/figures.js +43 -10
  18. package/dist/compiler/files.js +82 -41
  19. package/dist/compiler/fonts.js +4 -1
  20. package/dist/compiler/icons.d.ts +1 -1
  21. package/dist/compiler/icons.js +11 -2
  22. package/dist/compiler/ignored-files.d.ts +5 -0
  23. package/dist/compiler/ignored-files.js +98 -0
  24. package/dist/compiler/locales.js +13 -8
  25. package/dist/compiler/ops.d.ts +6 -0
  26. package/dist/compiler/ops.js +99 -8
  27. package/dist/compiler/subformat-schema.d.ts +14 -0
  28. package/dist/compiler/subformat-schema.js +92 -0
  29. package/dist/compiler/subformats.d.ts +22 -0
  30. package/dist/compiler/subformats.js +339 -0
  31. package/dist/compiler/textfile.js +3 -0
  32. package/dist/compiler/theme-variants.js +5 -2
  33. package/dist/core/code-file.js +7 -5
  34. package/dist/core/code-grammars.d.ts +5 -0
  35. package/dist/core/code-grammars.js +34 -0
  36. package/dist/core/content-metadata.d.ts +16 -6
  37. package/dist/core/content-metadata.js +283 -17
  38. package/dist/core/content-types.d.ts +4 -2
  39. package/dist/core/escape-html.d.ts +1 -0
  40. package/dist/core/escape-html.js +1 -0
  41. package/dist/core/figure-frame.js +20 -2
  42. package/dist/core/file-embed.js +7 -3
  43. package/dist/core/files.d.ts +2 -1
  44. package/dist/core/files.js +6 -8
  45. package/dist/core/flow-base-css.generated.js +1 -1
  46. package/dist/core/html.d.ts +4 -1
  47. package/dist/core/html.js +11 -10
  48. package/dist/core/icons.d.ts +3 -0
  49. package/dist/core/icons.js +3 -0
  50. package/dist/core/json-tools.d.ts +1 -0
  51. package/dist/core/json-tools.js +12 -0
  52. package/dist/core/language.js +5 -2
  53. package/dist/core/markdown.js +3 -1
  54. package/dist/core/previewers.d.ts +8 -0
  55. package/dist/core/previewers.js +2 -0
  56. package/dist/core/routing.d.ts +5 -2
  57. package/dist/core/routing.js +17 -5
  58. package/dist/core/schema-fields.d.ts +13 -0
  59. package/dist/core/schema-fields.js +175 -0
  60. package/dist/core/schema-graph-mount.js +26 -3
  61. package/dist/core/schema-refs.d.ts +8 -0
  62. package/dist/core/schema-refs.js +82 -0
  63. package/dist/core/subformat-record.d.ts +3 -0
  64. package/dist/core/subformat-record.js +131 -0
  65. package/dist/core/subformats.d.ts +43 -0
  66. package/dist/core/subformats.js +81 -0
  67. package/dist/core/toc-ui.js +26 -24
  68. package/dist/edit.global.js +326 -118
  69. package/dist/flow.global.js +3 -3
  70. package/dist/graph/abnfDiagram-S7VZAUFO-ASBULSKE.js +1 -0
  71. package/dist/graph/architecture-WOLXFQ4H-V57EDXXY.js +1 -0
  72. package/dist/graph/architectureDiagram-YGSQEFNW-GIMP347N.js +36 -0
  73. package/dist/graph/blockDiagram-APKKGZFW-RDL2T4JW.js +140 -0
  74. package/dist/graph/bpmnIcons-APHXDYXF-TWOSQNUQ.js +1 -0
  75. package/dist/graph/c4Diagram-KJ2TOQU5-75LIUC4F.js +38 -0
  76. package/dist/graph/chunk-26TUJSYJ.js +1 -0
  77. package/dist/graph/chunk-3LU66YMI.js +1 -0
  78. package/dist/graph/chunk-3RNHDNP5.js +1 -0
  79. package/dist/graph/chunk-3Y2PDVDE.js +1 -0
  80. package/dist/graph/chunk-3YJHMOOA.js +3 -0
  81. package/dist/graph/chunk-4KILFWYV.js +339 -0
  82. package/dist/graph/chunk-4QDE4SUK.js +1 -0
  83. package/dist/graph/chunk-5OO6DATX.js +1 -0
  84. package/dist/graph/chunk-5UUEAHTN.js +1 -0
  85. package/dist/graph/chunk-6T55RVVR.js +1 -0
  86. package/dist/graph/chunk-ARAJTKOH.js +2 -0
  87. package/dist/graph/chunk-BBD5CEDC.js +72 -0
  88. package/dist/graph/chunk-BVXT562I.js +1 -0
  89. package/dist/graph/chunk-BZAMCJ6B.js +32 -0
  90. package/dist/graph/chunk-C6J4A356.js +15 -0
  91. package/dist/graph/chunk-EG5UVNOC.js +213 -0
  92. package/dist/graph/chunk-GF4TOPUK.js +1 -0
  93. package/dist/graph/chunk-GYT4IYBZ.js +1 -0
  94. package/dist/graph/chunk-HJG2HUYJ.js +88 -0
  95. package/dist/graph/chunk-HOPPTA6H.js +62 -0
  96. package/dist/graph/chunk-I3DMZIHV.js +1 -0
  97. package/dist/graph/chunk-IC23EEF6.js +1 -0
  98. package/dist/graph/chunk-ISO77HML.js +1 -0
  99. package/dist/graph/chunk-JD7ZBJD2.js +1 -0
  100. package/dist/graph/chunk-JRSH5GNS.js +1 -0
  101. package/dist/graph/chunk-JSHWCPFJ.js +1 -0
  102. package/dist/graph/chunk-K2KLZU7M.js +10 -0
  103. package/dist/graph/chunk-K4I76XBX.js +14 -0
  104. package/dist/graph/chunk-KT4VPQD5.js +135 -0
  105. package/dist/graph/chunk-KU6GTEMS.js +1 -0
  106. package/dist/graph/chunk-LT6LXKCR.js +136 -0
  107. package/dist/graph/chunk-MPR575IN.js +1 -0
  108. package/dist/graph/chunk-N3OHKGRZ.js +1 -0
  109. package/dist/graph/chunk-O656ZI37.js +1 -0
  110. package/dist/graph/chunk-OFZLK5G4.js +1 -0
  111. package/dist/graph/chunk-PDH25ALM.js +2 -0
  112. package/dist/graph/chunk-PW2RGZ3O.js +1 -0
  113. package/dist/graph/chunk-QCJUF4YA.js +1 -0
  114. package/dist/graph/chunk-QEEA3KYY.js +2 -0
  115. package/dist/graph/chunk-QYAXYFNI.js +1 -0
  116. package/dist/graph/chunk-QYTM6GY3.js +1 -0
  117. package/dist/graph/chunk-R63JUMUK.js +1 -0
  118. package/dist/graph/chunk-SCEY3ZPT.js +1 -0
  119. package/dist/graph/chunk-VM2H7G2R.js +1 -0
  120. package/dist/graph/chunk-XQS73SBQ.js +1 -0
  121. package/dist/graph/chunk-YCRLJT2S.js +1 -0
  122. package/dist/graph/chunk-Z4FIZ7CR.js +1 -0
  123. package/dist/graph/chunk-ZMCB7VP2.js +1 -0
  124. package/dist/graph/chunk-ZSZXN4BT.js +2 -0
  125. package/dist/graph/classDiagram-v2-L25IHNXF-JL7LVDZH.js +217 -0
  126. package/dist/graph/cose-bilkent-JH36ORCC-PNM3EYPB.js +1 -0
  127. package/dist/graph/cynefin-EF2NZ3EQ-PSXP3DXL.js +1 -0
  128. package/dist/graph/cynefinDiagram-L36ESFBY-QVY54CXO.js +62 -0
  129. package/dist/graph/dagre-SPEB5RXH-HPGQXBZJ.js +4 -0
  130. package/dist/graph/diagram-3B7GL73J-DWH5WS2P.js +200 -0
  131. package/dist/graph/diagram-DNEMPZML-TEGH2U5I.js +24 -0
  132. package/dist/graph/diagram-L3MUGCDN-X4MREUWR.js +41 -0
  133. package/dist/graph/diagram-LA4NIPWU-NXT746HG.js +24 -0
  134. package/dist/graph/diagram-MHCOC4E2-JGHXSAGS.js +3 -0
  135. package/dist/graph/diagram-XPFHEONP-ZHZPBLT4.js +30 -0
  136. package/dist/graph/ebnfDiagram-UZSYDL2S-SMF6KJ43.js +1 -0
  137. package/dist/graph/elk-XV6XMXQE-AZ4HLCWI.js +27 -0
  138. package/dist/graph/erDiagram-EYBAPDEH-B3JNJ357.js +99 -0
  139. package/dist/graph/eventmodeling-K75KTNOO-WG6DDYMC.js +1 -0
  140. package/dist/graph/flowDiagram-MALGAFMO-2R7EYAQI.js +1 -0
  141. package/dist/graph/ganttDiagram-23REXITY-YXBYWFOJ.js +292 -0
  142. package/dist/graph/gitGraph-VSP46ZUC-VBFWJBBT.js +1 -0
  143. package/dist/graph/gitGraphDiagram-STJD7YCW-4FZDCFGV.js +106 -0
  144. package/dist/graph/info-OHQRW6UA-ALVDUVYV.js +1 -0
  145. package/dist/graph/infoDiagram-KBXO4MBG-7EHX2GQG.js +2 -0
  146. package/dist/graph/ishikawaDiagram-M35NCXHK-FRZD2PWQ.js +70 -0
  147. package/dist/graph/journeyDiagram-OPLOXDXO-7Z66JCPR.js +139 -0
  148. package/dist/graph/kanban-definition-BZKZHSQY-NN5AIKTE.js +89 -0
  149. package/dist/graph/katex-DXDNVO6V.js +257 -0
  150. package/dist/graph/mindmap-definition-I2QAS2VK-HDPCABN2.js +98 -0
  151. package/dist/graph/packet-JDAUHWVQ-5MQIBWAH.js +1 -0
  152. package/dist/graph/pegDiagram-FD4XPPHP-H2BFNCKT.js +1 -0
  153. package/dist/graph/pie-XZMESJXO-T5VRKHVT.js +1 -0
  154. package/dist/graph/pieDiagram-J4AKIIQI-O3NJSVD3.js +39 -0
  155. package/dist/graph/quadrantDiagram-XTYBLID2-TMTZJI3P.js +7 -0
  156. package/dist/graph/radar-ABXABTNO-C6FXIBYW.js +1 -0
  157. package/dist/graph/railroad-I3PHUGI6-ZSO5JXB3.js +1 -0
  158. package/dist/graph/railroad-abnf-LNEFI6M7-CI4GQRHB.js +1 -0
  159. package/dist/graph/railroad-ebnf-DWYD2UWJ-MUENNCGK.js +1 -0
  160. package/dist/graph/railroad-peg-JR7BVNR7-N3X5GGIX.js +1 -0
  161. package/dist/graph/railroadDiagram-MRQ6NCDL-JHWS23UO.js +1 -0
  162. package/dist/graph/requirementDiagram-Y7LWUY3S-7DVLPO4B.js +84 -0
  163. package/dist/graph/sankeyDiagram-WGJFYUXA-Z5L4R5TR.js +40 -0
  164. package/dist/graph/schema-graph-global.js +639 -0
  165. package/dist/graph/sequenceDiagram-5NDRBG5M-RCTU25NT.js +169 -0
  166. package/dist/graph/sizeCapture-INFHLROL-RROY32P5.js +1 -0
  167. package/dist/graph/stateDiagram-v2-FJ4FJNEA-BXUSPWDK.js +291 -0
  168. package/dist/graph/swimlanes-UMQ2AFYF-LDD74Q2S.js +1 -0
  169. package/dist/graph/swimlanesDiagram-OMPLPG4O-ZYULLJSU.js +8 -0
  170. package/dist/graph/timeline-definition-SEUHKTQE-ZQSMIVAS.js +120 -0
  171. package/dist/graph/treeView-D4JQ5SDB-42S7U73N.js +1 -0
  172. package/dist/graph/treemap-SAJKECNS-RUBYQ7XF.js +1 -0
  173. package/dist/graph/usecaseDiagram-AFA6YXN4-57RDRYXF.js +370 -0
  174. package/dist/graph/vennDiagram-NNMJR3RB-5C3LVAV6.js +34 -0
  175. package/dist/graph/wardley-7MLQ67FV-NV5HXTTC.js +1 -0
  176. package/dist/graph/wardleyDiagram-S7YFUSEH-6D5BKXHM.js +78 -0
  177. package/dist/graph/xychartDiagram-GGNKY7IX-6ET3ZM67.js +7 -0
  178. package/dist/mcp.js +54 -12
  179. package/dist/plugins/index.d.ts +1 -0
  180. package/dist/plugins/transport.d.ts +22 -0
  181. package/dist/plugins/transport.js +1 -0
  182. package/dist/plugins.global.js +95 -0
  183. package/dist/subformats.global.js +21 -0
  184. package/dist/ui.global.js +80 -60
  185. package/dist/viewer/build-plugins.d.ts +7 -0
  186. package/dist/viewer/build-plugins.js +70 -0
  187. package/dist/viewer/build.d.ts +11 -0
  188. package/dist/viewer/build.js +52 -11
  189. package/dist/viewer/code-file.js +7 -2
  190. package/dist/viewer/editor-tools.d.ts +7 -7
  191. package/dist/viewer/editor-tools.js +36 -5
  192. package/dist/viewer/figure-preview.d.ts +4 -0
  193. package/dist/viewer/figure-preview.js +54 -0
  194. package/dist/viewer/font-picker.js +5 -1
  195. package/dist/viewer/git-diff.d.ts +3 -0
  196. package/dist/viewer/git-diff.js +47 -0
  197. package/dist/viewer/hmr.js +8 -4
  198. package/dist/viewer/i18n.js +7 -5
  199. package/dist/viewer/language-picker.d.ts +2 -2
  200. package/dist/viewer/language-picker.js +26 -9
  201. package/dist/viewer/lazy-preview.d.ts +2 -0
  202. package/dist/viewer/lazy-preview.js +43 -0
  203. package/dist/viewer/locales/eng.json +95 -10
  204. package/dist/viewer/locales/jpn.json +96 -11
  205. package/dist/viewer/locales/zho.json +96 -11
  206. package/dist/viewer/nav-focus.js +8 -1
  207. package/dist/viewer/nav-update.d.ts +2 -0
  208. package/dist/viewer/nav-update.js +117 -0
  209. package/dist/viewer/plugin-blocks.js +1 -1
  210. package/dist/viewer/plugin-style.d.ts +1 -0
  211. package/dist/viewer/plugin-style.js +82 -0
  212. package/dist/viewer/plugins.js +1 -1
  213. package/dist/viewer/resize.d.ts +0 -2
  214. package/dist/viewer/resize.js +2 -98
  215. package/dist/viewer/resource-library.d.ts +11 -9
  216. package/dist/viewer/resource-library.js +181 -31
  217. package/dist/viewer/search.d.ts +4 -2
  218. package/dist/viewer/search.js +252 -53
  219. package/dist/viewer/serve.d.ts +11 -5
  220. package/dist/viewer/serve.js +913 -295
  221. package/dist/viewer/site-icon.js +13 -6
  222. package/dist/viewer/site-title.js +15 -2
  223. package/dist/viewer/textfile.d.ts +5 -0
  224. package/dist/viewer/textfile.js +34 -2
  225. package/dist/viewer/theme-picker.d.ts +2 -0
  226. package/dist/viewer/theme-picker.js +24 -8
  227. package/dist/viewer/workspace.d.ts +2 -0
  228. package/dist/viewer/workspace.js +46 -1
  229. package/package.json +27 -10
  230. package/dist/compiler/bundle.d.ts +0 -2
  231. package/dist/compiler/bundle.js +0 -100
@@ -0,0 +1,339 @@
1
+ import { existsSync } from "node:fs";
2
+ import { pathToFileURL } from "node:url";
3
+ import { isDeepStrictEqual } from "node:util";
4
+ import { stringify as stringifyToml } from "smol-toml";
5
+ import { Document, isSeq, parseDocument, stringify as stringifyYaml, visit } from "yaml";
6
+ import { flattenTree, MANIFEST_FILE, subformatPageId } from "../core/content-metadata.js";
7
+ import { fileExtension } from "../core/files.js";
8
+ import { schemaResolver } from "../core/schema-refs.js";
9
+ import { parseSubformatRecord, setSubformatValue } from "../core/subformat-record.js";
10
+ import { parseSubformats, SUBFORMAT_SCHEMA_TEMPLATE, } from "../core/subformats.js";
11
+ import { filesSnapshot, managedFile } from "./files.js";
12
+ import { parseManifestText, readManifest } from "./manifest.js";
13
+ import { compileSubformatSchema } from "./subformat-schema.js";
14
+ import { readTextfile, textfilePath, writeTextfile } from "./textfile.js";
15
+ export { compileSubformatSchema } from "./subformat-schema.js";
16
+ export const loadSubformat = (root, config, domains = []) => {
17
+ const content = readTextfile(root, config.schema);
18
+ const { schema, validate, context } = compileSubformatSchema(content, {
19
+ root,
20
+ file: config.schema,
21
+ domains,
22
+ });
23
+ const resolve = schemaResolver(schema, context);
24
+ // shortcut: controls use top-level properties and refs; extend extraction for composed schemas.
25
+ const fields = Object.entries(resolve(schema).properties ?? {}).flatMap(([name, definition]) => {
26
+ if (!definition || typeof definition !== "object")
27
+ return [];
28
+ const property = resolve(definition);
29
+ const values = property.enum;
30
+ return [
31
+ {
32
+ name,
33
+ title: typeof property.title === "string" ? property.title : name,
34
+ ...(["string", "number", "integer", "boolean"].includes(property.type)
35
+ ? { type: property.type }
36
+ : {}),
37
+ ...(Array.isArray(values) &&
38
+ values.every((value) => value === null ||
39
+ ["string", "number", "boolean"].includes(typeof value))
40
+ ? { enum: values }
41
+ : {}),
42
+ ...(["date", "date-time"].includes(property.format)
43
+ ? { format: property.format }
44
+ : {}),
45
+ ...(property.readOnly === true ? { readOnly: true } : {}),
46
+ },
47
+ ];
48
+ });
49
+ const object = resolve(schema);
50
+ const initial = object.default ??
51
+ object.examples?.[0] ??
52
+ Object.fromEntries(Object.entries(object.properties ?? {}).flatMap(([name, definition]) => {
53
+ if (!definition || typeof definition !== "object")
54
+ return [];
55
+ const property = resolve(definition);
56
+ if (!object.required?.includes(name) && property.default === undefined)
57
+ return [];
58
+ const prescribed = ["default", "const"].find((key) => Object.hasOwn(property, key));
59
+ const value = prescribed
60
+ ? property[prescribed]
61
+ : Array.isArray(property.enum)
62
+ ? property.enum[0]
63
+ : property.type === "null"
64
+ ? null
65
+ : property.type === "boolean"
66
+ ? false
67
+ : ["number", "integer"].includes(property.type)
68
+ ? (property.minimum ?? 0)
69
+ : property.type === "object"
70
+ ? {}
71
+ : property.type === "array"
72
+ ? []
73
+ : property.format === "date"
74
+ ? new Date().toISOString().slice(0, 10)
75
+ : property.format === "date-time"
76
+ ? new Date().toISOString()
77
+ : "";
78
+ return [[name, value]];
79
+ }));
80
+ let template = "";
81
+ try {
82
+ template =
83
+ config.format === "json"
84
+ ? `${JSON.stringify(initial, null, 2)}\n`
85
+ : config.format === "yaml"
86
+ ? stringifyYaml(initial)
87
+ : stringifyToml(initial);
88
+ }
89
+ catch {
90
+ // A schema example may contain values the configured format cannot represent.
91
+ }
92
+ return { ...config, fields, template, validate };
93
+ };
94
+ export const validateSubformatRecord = (format, data) => {
95
+ if (!format.validate(data))
96
+ throw new Error((format.validate.errors ?? [])
97
+ .map((error) => `${error.instancePath || "/"} ${error.message}`)
98
+ .join("; "));
99
+ };
100
+ export const isSubformatSchemaFile = (root, file, domains = []) => {
101
+ const configs = readManifest(root).subformats ?? [];
102
+ if (/\.schema\.json$/i.test(file) ||
103
+ configs.some((config) => config.schema === file.replace(/^(\.\/)+/, "")))
104
+ return true;
105
+ const uri = pathToFileURL(textfilePath(root, file)).href;
106
+ return configs.some((config) => {
107
+ try {
108
+ return Object.hasOwn(compileSubformatSchema(readTextfile(root, config.schema), {
109
+ root,
110
+ file: config.schema,
111
+ domains,
112
+ }).context.schemas, uri);
113
+ }
114
+ catch {
115
+ // A broken referring schema must not prevent opening a file for repair.
116
+ return false;
117
+ }
118
+ });
119
+ };
120
+ export const validateSubformatFile = (root, file, content, domains = []) => {
121
+ const configs = readManifest(root).subformats;
122
+ if (isSubformatSchemaFile(root, file, domains))
123
+ compileSubformatSchema(content, { root, file, domains });
124
+ const config = configs?.find((format) => format.ext === fileExtension(file));
125
+ if (!config)
126
+ return;
127
+ if (!filesSnapshot(root).files.some((file) => file.path === config.schema))
128
+ throw new Error("Subformat schema is not available in the workspace");
129
+ try {
130
+ validateSubformatRecord(loadSubformat(root, config, domains), parseSubformatRecord(content, config.format));
131
+ }
132
+ catch (error) {
133
+ throw new Error(`${file} (${config.format}): ${error instanceof Error ? error.message : String(error)}`);
134
+ }
135
+ };
136
+ export const subformatResponse = (root, input, domains = []) => {
137
+ try {
138
+ if (input?.action === "validate") {
139
+ if (typeof input.content !== "string")
140
+ throw new Error("Schema content is required");
141
+ if (input.file !== undefined && typeof input.file !== "string")
142
+ throw new Error("Schema file must be a path");
143
+ compileSubformatSchema(input.content, {
144
+ root,
145
+ file: typeof input.file === "string" && input.file
146
+ ? input.file
147
+ : "subformat.schema.json",
148
+ domains,
149
+ });
150
+ return { status: 200, body: {} };
151
+ }
152
+ if (input?.action === "layouts") {
153
+ if (typeof input.key !== "string" ||
154
+ !["card_title", "group_by", "sort_by", "sort_order"].includes(input.key) ||
155
+ typeof input.value !== "string" ||
156
+ (input.base !== null && typeof input.base !== "string"))
157
+ throw new Error("A layout key, value and previous value are required");
158
+ const raw = readTextfile(root, MANIFEST_FILE);
159
+ const manifest = parseManifestText(raw);
160
+ if (manifest.errors.length)
161
+ throw new Error(manifest.errors.join("\n"));
162
+ const index = manifest.subformats?.findIndex((format) => format.ext === input.ext) ?? -1;
163
+ const config = manifest.subformats?.[index];
164
+ if (!config)
165
+ throw new Error("Unknown subformat");
166
+ const key = input.key;
167
+ if ((config.layouts?.[key] ?? null) !== input.base)
168
+ return {
169
+ status: 409,
170
+ body: { error: "Default changed externally. Reload and try again." },
171
+ };
172
+ parseSubformats([{ ...config, layouts: { ...config.layouts, [key]: input.value } }]);
173
+ if (key !== "sort_order" && input.value) {
174
+ const field = loadSubformat(root, config, domains).fields.find((field) => field.name === input.value);
175
+ if (!field ||
176
+ (key === "group_by" && !field.enum?.length) ||
177
+ (key === "sort_by" && !field.enum && !field.format && !field.type))
178
+ throw new Error("Default must reference a compatible schema field");
179
+ }
180
+ const document = parseDocument(raw);
181
+ const section = manifest.format === "nested" ? "json_subformats" : "subformats";
182
+ document.setIn([section, index, "layouts", key], input.value);
183
+ if (!writeTextfile(root, MANIFEST_FILE, document.toString(), raw))
184
+ return { status: 409, body: { error: "Manifest changed externally. Try again." } };
185
+ return { status: 200, body: {} };
186
+ }
187
+ if (input?.action === "delete") {
188
+ if (typeof input.ext !== "string")
189
+ throw new Error("A subformat extension is required");
190
+ const raw = readTextfile(root, MANIFEST_FILE);
191
+ const manifest = parseManifestText(raw);
192
+ if (manifest.errors.length)
193
+ throw new Error(manifest.errors.join("\n"));
194
+ const index = manifest.subformats?.findIndex((format) => format.ext === input.ext) ?? -1;
195
+ const config = manifest.subformats?.[index];
196
+ if (!config)
197
+ throw new Error("Unknown subformat");
198
+ if (!isDeepStrictEqual(config, parseSubformats([input.base])[0]))
199
+ return {
200
+ status: 409,
201
+ body: { error: "Subformat changed externally. Reload and try again." },
202
+ };
203
+ const id = subformatPageId(input.ext);
204
+ if (flattenTree(manifest.tree).some((node) => node.parent === id))
205
+ throw new Error("Move or remove this subformat page's child pages before deleting it.");
206
+ const document = parseDocument(raw);
207
+ const nested = manifest.format === "nested";
208
+ document.deleteIn([nested ? "json_subformats" : "subformats", index]);
209
+ const pages = document.get(nested ? "center_contents" : "docs", true);
210
+ if (isSeq(pages))
211
+ visit(pages, {
212
+ Map: (_key, node) => {
213
+ if (node.get("subformat") === input.ext)
214
+ return visit.REMOVE;
215
+ },
216
+ });
217
+ if (!nested && document.hasIn(["files", "icons", id]))
218
+ document.deleteIn(["files", "icons", id]);
219
+ const content = document.toString();
220
+ const next = parseManifestText(content);
221
+ if (next.errors.length)
222
+ throw new Error(next.errors.join("\n"));
223
+ if (!writeTextfile(root, MANIFEST_FILE, content, raw))
224
+ return { status: 409, body: { error: "Manifest changed externally. Try again." } };
225
+ return { status: 200, body: {} };
226
+ }
227
+ if (input?.action === "register") {
228
+ const raw = readTextfile(root, MANIFEST_FILE);
229
+ const manifest = parseManifestText(raw);
230
+ if (manifest.errors.length)
231
+ throw new Error(manifest.errors.join("\n"));
232
+ const schema = input.schema === undefined || input.schema === ""
233
+ ? `${input.ext}.schema.json`
234
+ : input.schema;
235
+ const configs = parseSubformats([
236
+ ...(manifest.subformats ?? []),
237
+ { ext: input.ext, format: input.format, schema },
238
+ ]);
239
+ const config = configs[configs.length - 1];
240
+ if (!managedFile(config.schema))
241
+ throw new Error("Schema must be a local project file");
242
+ if (input.content !== undefined && typeof input.content !== "string")
243
+ throw new Error("Schema content must be text");
244
+ const initial = input.content ?? SUBFORMAT_SCHEMA_TEMPLATE;
245
+ compileSubformatSchema(initial, { root, file: config.schema, domains });
246
+ if (!existsSync(textfilePath(root, config.schema))) {
247
+ if (!writeTextfile(root, config.schema, initial, null))
248
+ return {
249
+ status: 409,
250
+ body: { error: "Schema changed externally. Try again." },
251
+ };
252
+ }
253
+ else if (input.content !== undefined &&
254
+ readTextfile(root, config.schema) !== initial) {
255
+ return {
256
+ status: 409,
257
+ body: { error: "Schema file already exists. Choose another path." },
258
+ };
259
+ }
260
+ if (!filesSnapshot(root).files.some((file) => file.path === config.schema))
261
+ throw new Error("Subformat schema is not available in the workspace");
262
+ const loaded = loadSubformat(root, config, domains);
263
+ const parsed = parseDocument(raw);
264
+ const document = isSeq(parsed.contents) || parsed.contents == null
265
+ ? new Document({ docs: parsed.toJS() ?? [] })
266
+ : parsed;
267
+ if (document !== parsed)
268
+ document.commentBefore = parsed.commentBefore;
269
+ const key = manifest.format === "nested" ? "json_subformats" : "subformats";
270
+ if (document.has(key))
271
+ document.addIn([key], config);
272
+ else
273
+ document.set(key, [config]);
274
+ if (!writeTextfile(root, MANIFEST_FILE, document.toString(), raw))
275
+ return { status: 409, body: { error: "Manifest changed externally. Try again." } };
276
+ return {
277
+ status: 200,
278
+ body: {
279
+ collection: {
280
+ ...config,
281
+ fields: loaded.fields,
282
+ template: loaded.template,
283
+ defaultFolder: manifest.defaultSubformatFolder,
284
+ records: [],
285
+ change: "added",
286
+ },
287
+ },
288
+ };
289
+ }
290
+ if (input?.action === "create") {
291
+ if (typeof input.file !== "string" || typeof input.content !== "string")
292
+ throw new Error("A record file and content are required");
293
+ const config = readManifest(root).subformats?.find((format) => format.ext === fileExtension(input.file));
294
+ if (!config)
295
+ throw new Error("Choose a configured subformat extension");
296
+ validateSubformatFile(root, input.file, input.content, domains);
297
+ const data = parseSubformatRecord(input.content, config.format);
298
+ if (!writeTextfile(root, input.file, input.content, null))
299
+ return { status: 409, body: { error: "File already exists" } };
300
+ return { status: 200, body: { content: input.content, data } };
301
+ }
302
+ if (typeof input?.file !== "string" ||
303
+ typeof input.base !== "string" ||
304
+ typeof input.field !== "string")
305
+ throw new Error("A record file, original content and enum field are required");
306
+ const files = filesSnapshot(root).files.map((file) => file.path);
307
+ if (!files.includes(input.file))
308
+ throw new Error("Record file is not available in the workspace");
309
+ const config = readManifest(root).subformats?.find((format) => format.ext === fileExtension(input.file));
310
+ if (!config || !files.includes(config.schema))
311
+ throw new Error("Subformat schema is not available in the workspace");
312
+ const format = loadSubformat(root, config, domains);
313
+ const field = format.fields.find((field) => field.name === input.field);
314
+ if (field?.readOnly || !field?.enum?.some((value) => value === input.value))
315
+ throw new Error("Choose a writable enum value defined by the schema");
316
+ const current = readTextfile(root, input.file);
317
+ if (current !== input.base)
318
+ return {
319
+ status: 409,
320
+ body: { error: "File changed externally. Reload it before saving." },
321
+ };
322
+ const content = setSubformatValue(current, format.format, [input.field], input.value);
323
+ const roundTrip = parseSubformatRecord(content, format.format);
324
+ validateSubformatRecord(format, roundTrip);
325
+ if (!writeTextfile(root, input.file, content, current))
326
+ return {
327
+ status: 409,
328
+ body: { error: "File changed externally. Reload it before saving." },
329
+ };
330
+ return { status: 200, body: { content, data: roundTrip } };
331
+ }
332
+ catch (error) {
333
+ return {
334
+ status: 400,
335
+ body: { error: error instanceof Error ? error.message : String(error) },
336
+ };
337
+ }
338
+ };
339
+ export { parseSubformatRecord } from "../core/subformat-record.js";
@@ -3,6 +3,7 @@ import { linkSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync,
3
3
  import { open, stat } from "node:fs/promises";
4
4
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
5
  import { isRemoteTextfile, parseTextfileRange } from "../core/textfile.js";
6
+ import { assertNotIgnored } from "./ignored-files.js";
6
7
  import { readRemoteTextfile, readRemoteTextfileAsync } from "./textfile-remote.js";
7
8
  export { parseTextfileRange } from "../core/textfile.js";
8
9
  /** Select whole lines, preserving their original separators and excluding the final separator. */
@@ -56,6 +57,7 @@ export const readTextfile = (root, file, refresh = false, domains = []) => {
56
57
  }
57
58
  else {
58
59
  const path = textfilePath(root, file);
60
+ assertNotIgnored(root, file);
59
61
  if (!statSync(path).isFile())
60
62
  throw new Error("expected a regular UTF-8 text file");
61
63
  data = readFileSync(path);
@@ -73,6 +75,7 @@ export const readTextfileAsync = async (root, file, refresh = false, domains = [
73
75
  return decodeTextfile(await readRemoteTextfileAsync(file, domains, refresh));
74
76
  }
75
77
  const path = textfilePath(root, file);
78
+ assertNotIgnored(root, file);
76
79
  if (!(await stat(path)).isFile())
77
80
  throw new Error("expected a regular UTF-8 text file");
78
81
  const handle = await open(path, "r");
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readdirSync } from "node:fs";
2
2
  import { localFile } from "../core/files.js";
3
3
  import { themeVariantFile } from "../core/theme-variants.js";
4
+ import { ignoredFiles } from "./ignored-files.js";
4
5
  import { readManifest } from "./manifest.js";
5
6
  import { textfilePath } from "./textfile.js";
6
7
  export const themeNames = (root) => {
@@ -16,7 +17,9 @@ export const siteThemes = (root, manifest = readManifest(root)) => manifest.them
16
17
  export const themeVariants = (root, file, themes = siteThemes(root)) => {
17
18
  if (!localFile(file) || file.startsWith("themes/"))
18
19
  return {};
19
- return Object.fromEntries(themes
20
+ const variants = themes
20
21
  .map((theme) => [theme, themeVariantFile(file, theme)])
21
- .filter(([, path]) => existsSync(textfilePath(root, path))));
22
+ .filter(([, path]) => existsSync(textfilePath(root, path)));
23
+ const ignored = ignoredFiles(root, variants.map(([, path]) => path));
24
+ return Object.fromEntries(variants.filter(([, path]) => !ignored.has(path)));
22
25
  };
@@ -17,11 +17,13 @@ export const codeFileName = (body, position, documentId) => {
17
17
  markdown: "md",
18
18
  "c++": "cpp",
19
19
  };
20
- const extension = Object.hasOwn(aliases, language)
21
- ? aliases[language]
22
- : /^[a-z0-9_-]+$/.test(language)
23
- ? language
24
- : "txt";
20
+ const extension = /^\.[a-z0-9][a-z0-9_-]*$/.test(language)
21
+ ? language.slice(1)
22
+ : Object.hasOwn(aliases, language)
23
+ ? aliases[language]
24
+ : /^[a-z0-9_-]+$/.test(language)
25
+ ? language
26
+ : "txt";
25
27
  const directory = language === "termth"
26
28
  ? "terms"
27
29
  : language === "graphth" || language === "schemath"
@@ -0,0 +1,5 @@
1
+ import type { LanguageRegistration } from "shiki/core";
2
+ import type { Subformat } from "./subformats.js";
3
+ /** Include referenced scopes as well as declared embedded languages for custom grammars. */
4
+ export declare const selectCodeGrammars: (all: LanguageRegistration[], languages: Iterable<string>) => LanguageRegistration[];
5
+ export declare const subformatCodeGrammars: (grammars: LanguageRegistration[], subformats?: readonly Subformat[]) => LanguageRegistration[];
@@ -0,0 +1,34 @@
1
+ /** Include referenced scopes as well as declared embedded languages for custom grammars. */
2
+ export const selectCodeGrammars = (all, languages) => {
3
+ const selected = new Set();
4
+ const add = (entry) => {
5
+ if (selected.has(entry))
6
+ return;
7
+ selected.add(entry);
8
+ const names = new Set(entry.embeddedLangs ?? []);
9
+ for (const match of JSON.stringify(entry).matchAll(/"include":"([^"#$][^"]*)"/g))
10
+ names.add((match[1] ?? "").split("#")[0] ?? "");
11
+ for (const dependency of all)
12
+ if (names.has(dependency.name) || names.has(dependency.scopeName))
13
+ add(dependency);
14
+ };
15
+ for (const language of languages) {
16
+ const grammar = all.find((entry) => entry.name === language || entry.aliases?.includes(language));
17
+ if (grammar)
18
+ add(grammar);
19
+ }
20
+ return [...selected];
21
+ };
22
+ export const subformatCodeGrammars = (grammars, subformats = []) => {
23
+ const registered = new Set(grammars.flatMap((grammar) => [grammar.name, ...(grammar.aliases ?? [])]));
24
+ return grammars.map((grammar) => ({
25
+ ...grammar,
26
+ aliases: [
27
+ ...(grammar.aliases ?? []),
28
+ ...subformats
29
+ .filter(({ format }) => format === grammar.name)
30
+ .flatMap(({ ext }) => [`.${ext}`, ext])
31
+ .filter((alias) => !registered.has(alias)),
32
+ ],
33
+ }));
34
+ };
@@ -1,23 +1,34 @@
1
1
  import { type FileLayout } from "./files.js";
2
+ import { type Subformat } from "./subformats.js";
2
3
  export declare const MANIFEST_FILE = "dooc.yaml";
3
4
  export interface DocNode {
4
5
  id: string;
6
+ file?: string;
7
+ subformat?: string;
8
+ title?: string;
5
9
  children: DocNode[];
6
10
  }
7
11
  export interface ParsedManifest {
12
+ format?: "nested";
13
+ titles?: Record<string, string>;
14
+ themeIcons?: Record<string, string>;
8
15
  tree: DocNode[];
9
16
  title?: string;
10
17
  icon?: string;
11
18
  defaultLang?: string;
19
+ licence?: string;
12
20
  files?: FileLayout;
13
21
  /** Content languages offered by the site, including defaultLang; normalized to ISO 639-3. */
14
22
  languages?: string[];
15
23
  /** Color themes offered by the site, with authored variants for embedded files. */
16
24
  themes?: ("light" | "dark")[];
25
+ subformats?: Subformat[];
26
+ defaultSubformatFolder?: string;
17
27
  errors: string[];
18
28
  }
19
29
  export declare const DOC_ID_RE: RegExp;
20
30
  export declare const isDocId: (s: string) => boolean;
31
+ export declare const subformatPageId: (ext: string) => string;
21
32
  /** Doc file name directly under content/ (`<id>.md`) → id. undefined if the shape differs. */
22
33
  export declare const docIdOfFile: (name: string) => string | undefined;
23
34
  /**
@@ -26,16 +37,15 @@ export declare const docIdOfFile: (name: string) => string | undefined;
26
37
  * Validates id shape and duplicates.
27
38
  */
28
39
  export declare const parseManifest: (value: unknown) => ParsedManifest;
29
- /** Tree → dooc.yaml text (serialized in the hand-written shape, keeping git diffs clean). */
30
- export declare const serializeManifest: (tree: readonly DocNode[], { title, icon, defaultLang, files, languages, themes, }?: Omit<ParsedManifest, "tree" | "errors">) => string;
40
+ /** Serialize the authored format; legacy manifests remain readable during migration. */
41
+ export declare const serializeManifest: (tree: readonly DocNode[], { format, titles, themeIcons, title, icon, defaultLang, licence, files, languages, themes, subformats, defaultSubformatFolder, }?: Omit<ParsedManifest, "tree" | "errors">) => string;
31
42
  /** Flatten the tree into display order (pre-order). parent omitted = top level. */
32
- export declare const flattenTree: (tree: readonly DocNode[]) => {
33
- id: string;
43
+ export declare const flattenTree: (tree: readonly DocNode[]) => (Omit<DocNode, "children"> & {
34
44
  parent?: string;
35
45
  depth: number;
36
- }[];
46
+ })[];
37
47
  /** Append a new doc to the end of parent's children. Duplicate id / missing parent → undefined. */
38
- export declare const insertDoc: (tree: readonly DocNode[], id: string, parent?: string) => DocNode[] | undefined;
48
+ export declare const insertDoc: (tree: readonly DocNode[], id: string, parent?: string, page?: Pick<DocNode, "subformat" | "title">) => DocNode[] | undefined;
39
49
  /**
40
50
  * Move a doc (with its children) to position index under parent. It can't be moved under
41
51
  * itself or its own descendants (that would create a cycle). An invalid move returns undefined