@supa-media/tex-contract 0.0.0-stage → 0.3.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 (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -2
  3. package/package.json +16 -4
  4. package/texContract/aboutLines.cjs +117 -0
  5. package/texContract/aboutLines.d.cts +14 -0
  6. package/texContract/aboutNoteLimits.cjs +41 -0
  7. package/texContract/aboutNoteLimits.d.cts +5 -0
  8. package/texContract/actionManual.cjs +104 -0
  9. package/texContract/activityHint.cjs +40 -0
  10. package/texContract/activityHint.d.cts +4 -0
  11. package/texContract/asks.cjs +16 -0
  12. package/texContract/asks.d.cts +6 -0
  13. package/texContract/asksToFile.cjs +20 -0
  14. package/texContract/asksToFile.d.cts +2 -0
  15. package/texContract/attention.cjs +138 -0
  16. package/texContract/attention.d.cts +20 -0
  17. package/texContract/browserHandback.cjs +13 -0
  18. package/texContract/browserHandback.d.cts +3 -0
  19. package/texContract/browserOwner.cjs +31 -0
  20. package/texContract/browserOwner.d.cts +2 -0
  21. package/texContract/bytes.cjs +12 -0
  22. package/texContract/capabilities.cjs +47 -0
  23. package/texContract/capabilities.d.cts +27 -0
  24. package/texContract/capabilities.generated.json +77 -0
  25. package/texContract/chatHistoryPath.cjs +38 -0
  26. package/texContract/chatHistoryPath.d.cts +5 -0
  27. package/texContract/chatLedger.cjs +91 -0
  28. package/texContract/chatLedger.d.cts +21 -0
  29. package/texContract/computerTool.cjs +102 -0
  30. package/texContract/computerTool.d.cts +16 -0
  31. package/texContract/consentYes.cjs +22 -0
  32. package/texContract/consentYes.d.cts +5 -0
  33. package/texContract/contextGateway.cjs +52 -0
  34. package/texContract/contextGateway.d.cts +19 -0
  35. package/texContract/deskEligible.cjs +20 -0
  36. package/texContract/deskEligible.d.cts +3 -0
  37. package/texContract/downloads.cjs +129 -0
  38. package/texContract/downloads.d.cts +15 -0
  39. package/texContract/egressLedger.cjs +114 -0
  40. package/texContract/egressLedger.d.cts +14 -0
  41. package/texContract/emailRead.cjs +38 -0
  42. package/texContract/emailRead.d.cts +5 -0
  43. package/texContract/eventWords.cjs +35 -0
  44. package/texContract/eventWords.d.cts +4 -0
  45. package/texContract/fileParts.cjs +43 -0
  46. package/texContract/fileParts.d.cts +6 -0
  47. package/texContract/fileTables.cjs +243 -0
  48. package/texContract/fileWords.cjs +169 -0
  49. package/texContract/folded.cjs +16 -0
  50. package/texContract/folded.d.cts +3 -0
  51. package/texContract/googleAiAccess.cjs +12 -0
  52. package/texContract/googleAiAccess.d.cts +2 -0
  53. package/texContract/graphWires.cjs +18 -0
  54. package/texContract/graphWires.d.cts +5 -0
  55. package/texContract/groupSession.cjs +28 -0
  56. package/texContract/groupSession.d.cts +7 -0
  57. package/texContract/guestNames.cjs +136 -0
  58. package/texContract/guestNames.d.cts +35 -0
  59. package/texContract/heard.cjs +19 -0
  60. package/texContract/heard.d.cts +4 -0
  61. package/texContract/heldOffer.cjs +57 -0
  62. package/texContract/heldOffer.d.cts +8 -0
  63. package/texContract/helperCallers.cjs +29 -0
  64. package/texContract/helperCallers.d.cts +4 -0
  65. package/texContract/identity.cjs +179 -0
  66. package/texContract/identity.d.cts +8 -0
  67. package/texContract/jobLogEntries.cjs +93 -0
  68. package/texContract/jobLogEntries.d.cts +12 -0
  69. package/texContract/judgedActions.cjs +20 -0
  70. package/texContract/judgedActions.d.cts +4 -0
  71. package/texContract/labSandboxGate.cjs +63 -0
  72. package/texContract/labSandboxGate.d.cts +9 -0
  73. package/texContract/ledgerReadChars.cjs +4 -0
  74. package/texContract/ledgerReadChars.d.cts +3 -0
  75. package/texContract/mailCheck.cjs +90 -0
  76. package/texContract/mailCheck.d.cts +12 -0
  77. package/texContract/mailEntries.cjs +64 -0
  78. package/texContract/mailEntries.d.cts +13 -0
  79. package/texContract/mailHealth.cjs +60 -0
  80. package/texContract/mailHealth.d.cts +10 -0
  81. package/texContract/notePath.cjs +99 -0
  82. package/texContract/notePath.d.cts +4 -0
  83. package/texContract/noteRunFacts.cjs +136 -0
  84. package/texContract/noteRunFacts.d.cts +32 -0
  85. package/texContract/objectEtag.cjs +84 -0
  86. package/texContract/objectEtag.d.cts +4 -0
  87. package/texContract/objectPlace.cjs +32 -0
  88. package/texContract/objectPlace.d.cts +5 -0
  89. package/texContract/pageJobs.cjs +226 -0
  90. package/texContract/pageJobs.d.cts +13 -0
  91. package/texContract/pdfLinks.cjs +135 -0
  92. package/texContract/pdfLinks.d.cts +4 -0
  93. package/texContract/personHub.cjs +38 -0
  94. package/texContract/personHub.d.cts +5 -0
  95. package/texContract/personNameText.cjs +18 -0
  96. package/texContract/personNameText.d.cts +3 -0
  97. package/texContract/pinnedContext.cjs +12 -0
  98. package/texContract/pinnedContext.d.cts +3 -0
  99. package/texContract/quietHoursWindow.cjs +36 -0
  100. package/texContract/quietHoursWindow.d.cts +3 -0
  101. package/texContract/reactEmoji.cjs +79 -0
  102. package/texContract/reactEmoji.d.cts +2 -0
  103. package/texContract/scopeClamp.cjs +117 -0
  104. package/texContract/scopeClamp.d.cts +5 -0
  105. package/texContract/scopes.cjs +29 -0
  106. package/texContract/scopes.d.cts +7 -0
  107. package/texContract/searchSettings.cjs +54 -0
  108. package/texContract/searchSettings.d.cts +6 -0
  109. package/texContract/settingHabits.cjs +87 -0
  110. package/texContract/settingHabits.d.cts +20 -0
  111. package/texContract/settingsNotes.cjs +217 -0
  112. package/texContract/settingsNotes.d.cts +26 -0
  113. package/texContract/shownEmail.cjs +181 -0
  114. package/texContract/texClients.cjs +53 -0
  115. package/texContract/texClients.d.cts +12 -0
  116. package/texContract/texGraph.cjs +593 -0
  117. package/texContract/texGraph.d.cts +55 -0
  118. package/texContract/texKeys.cjs +100 -0
  119. package/texContract/texKeys.d.cts +13 -0
  120. package/texContract/texNumber.cjs +20 -0
  121. package/texContract/texNumber.d.cts +3 -0
  122. package/texContract/texOwnSources.cjs +9 -0
  123. package/texContract/texOwnSources.d.cts +3 -0
  124. package/texContract/texSettings.cjs +22 -0
  125. package/texContract/texSettings.d.cts +20 -0
  126. package/texContract/texSettings.generated.json +25 -0
  127. package/texContract/texStateFiles.cjs +22 -0
  128. package/texContract/texStateFiles.d.cts +9 -0
  129. package/texContract/texSteps.cjs +27 -0
  130. package/texContract/texSteps.d.cts +14 -0
  131. package/texContract/texSteps.generated.json +41 -0
  132. package/texContract/texStoreNames.cjs +26 -0
  133. package/texContract/texStoreNames.d.cts +12 -0
  134. package/texContract/texStoreWire.cjs +306 -0
  135. package/texContract/texStoreWire.d.cts +32 -0
  136. package/texContract/threadsFold.cjs +75 -0
  137. package/texContract/threadsFold.d.cts +22 -0
  138. package/texContract/toolActions.cjs +253 -0
  139. package/texContract/toolCatalog.cjs +49 -0
  140. package/texContract/toolCatalog.generated.json +90 -0
  141. package/texContract/toolContract.cjs +52 -0
  142. package/texContract/toolContract.d.cts +12 -0
  143. package/texContract/toolShapes.cjs +14 -0
  144. package/texContract/transcription.cjs +101 -0
  145. package/texContract/transcription.d.cts +3 -0
  146. package/texContract/treeJobs.cjs +31 -0
  147. package/texContract/treeJobs.d.cts +3 -0
  148. package/texContract/turnSession.cjs +90 -0
  149. package/texContract/turnSession.d.cts +19 -0
  150. package/texContract/turnWords.cjs +90 -0
  151. package/texContract/turnWords.d.cts +9 -0
  152. package/texContract/uploadImages.cjs +131 -0
  153. package/texContract/uploadImages.d.cts +13 -0
  154. package/texContract/v2Names.cjs +41 -0
  155. package/texContract/v2SearchFetch.cjs +316 -0
  156. package/texContract/v2Translate.cjs +123 -0
  157. package/texContract/vaultTools.cjs +112 -0
  158. package/texContract/vaultTools.d.cts +15 -0
  159. package/texContract/yesWords.cjs +91 -0
  160. package/texShared/argumentCoercion.cjs +93 -0
  161. package/texShared/cellComments.cjs +122 -0
  162. package/texShared/cellComments.d.cts +21 -0
  163. package/texShared/comments.cjs +586 -0
  164. package/texShared/comments.d.cts +115 -0
  165. package/texShared/contentTypes.cjs +85 -0
  166. package/texShared/fileKinds.cjs +123 -0
  167. package/texShared/flags.cjs +159 -0
  168. package/texShared/flags.d.cts +23 -0
  169. package/texShared/folderJobInfo.cjs +301 -0
  170. package/texShared/folderJobInfo.d.cts +40 -0
  171. package/texShared/folderRoles.cjs +512 -0
  172. package/texShared/folderRoles.d.cts +73 -0
  173. package/texShared/githubOidc.ts +98 -0
  174. package/texShared/groupRunQuestions.cjs +98 -0
  175. package/texShared/groupRunQuestions.d.cts +4 -0
  176. package/texShared/inboxFolders.cjs +49 -0
  177. package/texShared/inboxFolders.d.cts +5 -0
  178. package/texShared/ink/drawing.cjs +474 -0
  179. package/texShared/ink/texFigure.cjs +252 -0
  180. package/texShared/ink/texFigure.d.cts +36 -0
  181. package/texShared/ink/texPoses.cjs +180 -0
  182. package/texShared/ink/texPoses.d.cts +20 -0
  183. package/texShared/jobPartEnd.cjs +133 -0
  184. package/texShared/jobPartEnd.d.cts +13 -0
  185. package/texShared/jobResume.cjs +31 -0
  186. package/texShared/jobResume.d.cts +5 -0
  187. package/texShared/jobSplit.cjs +90 -0
  188. package/texShared/jobSplit.d.cts +6 -0
  189. package/texShared/jobStart.cjs +56 -0
  190. package/texShared/jobStart.d.cts +9 -0
  191. package/texShared/mail.cjs +400 -0
  192. package/texShared/mail.d.cts +56 -0
  193. package/texShared/messageAnchors.cjs +38 -0
  194. package/texShared/models/aiKeys.cjs +137 -0
  195. package/texShared/models/aiKeys.d.cts +42 -0
  196. package/texShared/models/aiKeys.json +56 -0
  197. package/texShared/models/creditRest.cjs +269 -0
  198. package/texShared/models/promptBudget.cjs +295 -0
  199. package/texShared/models/promptSections.cjs +29 -0
  200. package/texShared/noteAppLink.cjs +23 -0
  201. package/texShared/notePreview.cjs +219 -0
  202. package/texShared/notePreview.d.cts +21 -0
  203. package/texShared/pages/blocks.cjs +215 -0
  204. package/texShared/pages/cover.cjs +164 -0
  205. package/texShared/pages/css.cjs +181 -0
  206. package/texShared/pages/cssBlocks.cjs +264 -0
  207. package/texShared/pages/draw.cjs +212 -0
  208. package/texShared/pages/drawRich.cjs +232 -0
  209. package/texShared/pages/html.cjs +168 -0
  210. package/texShared/pages/inline.cjs +251 -0
  211. package/texShared/pages/layout.cjs +76 -0
  212. package/texShared/pages/parse.cjs +418 -0
  213. package/texShared/pages/render.cjs +200 -0
  214. package/texShared/pages/skill.cjs +147 -0
  215. package/texShared/pages/skill.d.cts +5 -0
  216. package/texShared/pages/templates.cjs +336 -0
  217. package/texShared/r2Store.cjs +163 -0
  218. package/texShared/remoteImage.cjs +177 -0
  219. package/texShared/routines.cjs +656 -0
  220. package/texShared/routines.d.cts +86 -0
  221. package/texShared/servedListing.cjs +250 -0
  222. package/texShared/servedListing.d.cts +17 -0
  223. package/texShared/sheetCsv.cjs +566 -0
  224. package/texShared/sheetCsv.d.cts +91 -0
  225. package/texShared/storageLayout.cjs +88 -0
  226. package/texShared/storeKeys.cjs +144 -0
  227. package/texShared/toolArguments.cjs +323 -0
@@ -0,0 +1,76 @@
1
+ /**
2
+ * A page's own layout: HTML and CSS that place the page's sections, for work
3
+ * that needs to look designed (owner, 2026-11-10: pages "similar to instinct",
4
+ * "extensible enough for custom Ui using html css"; the Instinct roast:
5
+ * editorial type, numbered findings, exhibits you tap to enlarge).
6
+ *
7
+ * The words stay in the note as Markdown. A ```` ```layout ```` fence holds
8
+ * the HTML, and each `data-from` in it names what the runtime puts there:
9
+ * `cover` (the title, the opening line and the cover), a `##` section by its
10
+ * heading, or `rest` (every section the layout doesn't name). A section the
11
+ * layout doesn't place and there is no `rest` for is added at the end, so a
12
+ * layout never hides a word, and editing, comments, search and export all
13
+ * keep working on the Markdown. Delete the fence and the page reads as it did.
14
+ *
15
+ * The layout is drawn only inside the sealed page frame, whose policy loads
16
+ * nothing and runs only the runtime's own script; this file refuses at write
17
+ * what the frame would strip anyway (`problems`), so a writer learns what is
18
+ * not allowed instead of seeing it vanish, and the frame strips it again
19
+ * (`apps/mobile/page-frame/layout.js`). Decision:
20
+ * `docs/decisions/app-and-console/pages.md`, "A page can have a layout of its own".
21
+ */
22
+
23
+ "use strict";
24
+
25
+ /** The longest layout, in characters: a layout places words, it doesn't hold them. */
26
+ const MAX_LAYOUT_CHARS = 40_000;
27
+
28
+ /** Elements a layout may not hold: anything that runs, loads, embeds, submits or redirects. */
29
+ const BANNED_TAGS = ["script", "iframe", "frame", "frameset", "object", "embed", "applet", "base", "meta", "link", "form", "input", "button", "textarea", "select", "option", "portal", "template", "svg", "math", "noscript"];
30
+
31
+ const CHECKS = [
32
+ [new RegExp(`<\\s*(?:${BANNED_TAGS.join("|")})\\b`, "i"), (m) => `a layout can't hold ${m[0].replace(/[<\s]/g, "")} elements: it places the page's words and pictures, nothing that runs, loads or sends.`],
33
+ [/\son[a-z]+\s*=/i, () => "a layout can't hold event handlers (onclick= and the like): nothing in it runs."],
34
+ [/(?:javascript|vbscript|data)\s*:(?!image\/(?:png|jpeg|gif|webp);base64,)/i, () => "a layout can't link to javascript: or data: addresses."],
35
+ [/@import\b/i, () => "a layout's CSS can't @import: it loads nothing."],
36
+ [/url\(\s*['"]?(?!data:image\/)/i, () => "a layout's CSS can't load url(...) from anywhere: use the page's own photos in its sections."],
37
+ [/\b(?:src|srcset|href|action|formaction|poster|background)\s*=/i, () => "a layout can't name its own addresses (src=, href=): pictures and links come from the page's words."],
38
+ ];
39
+
40
+ /** What `data-from` names in a layout, in order, each once. */
41
+ function layoutSlots(html) {
42
+ const seen = new Set();
43
+ for (const match of String(html ?? "").matchAll(/\bdata-from\s*=\s*(?:"([^"]*)"|'([^']*)')/gi)) {
44
+ const name = (match[1] ?? match[2] ?? "").trim();
45
+ if (name !== "") seen.add(name);
46
+ }
47
+ return [...seen];
48
+ }
49
+
50
+ /** Section headings as a layout names them: case and spacing don't matter. */
51
+ function slotKey(name) {
52
+ return String(name ?? "").trim().replace(/\s+/g, " ").toLowerCase();
53
+ }
54
+
55
+ /**
56
+ * Why a layout can't be used, as page problems, or none. `sections` are the
57
+ * page's `##` headings as text, so a slot that names no section is caught.
58
+ */
59
+ function layoutProblems(html, sections = []) {
60
+ const text = String(html ?? "");
61
+ const problems = [];
62
+ if (text.length > MAX_LAYOUT_CHARS) problems.push({ message: `a layout is at most ${MAX_LAYOUT_CHARS.toLocaleString("en-US")} characters; this one is ${text.length.toLocaleString("en-US")}.` });
63
+ for (const [pattern, say] of CHECKS) {
64
+ const found = pattern.exec(text);
65
+ if (found) problems.push({ message: `This layout can't be drawn: ${say(found)}` });
66
+ }
67
+ const slots = layoutSlots(text);
68
+ if (slots.length === 0) problems.push({ message: 'This layout can\'t be drawn: it places nothing. Name what goes where with data-from="cover", data-from="<a ## heading>" or data-from="rest".' });
69
+ const known = new Set(["cover", "rest", ...sections.map(slotKey)]);
70
+ for (const slot of slots) {
71
+ if (!known.has(slotKey(slot))) problems.push({ message: `This layout's data-from="${slot.slice(0, 80)}" names no ## section of this page.` });
72
+ }
73
+ return problems;
74
+ }
75
+
76
+ module.exports = { MAX_LAYOUT_CHARS, BANNED_TAGS, layoutProblems, layoutSlots, slotKey };
@@ -0,0 +1,418 @@
1
+ /**
2
+ * A page note (`<name>.page.md`), read into the parts a page is drawn from.
3
+ *
4
+ * The format is Markdown with three kinds of fenced block on top
5
+ * (`docs/decisions/app-and-console/pages.md`):
6
+ *
7
+ * - **Words** are ordinary Markdown: headings, paragraphs, lists, tasks,
8
+ * quotes, tables, rules. `##` headings are the page's sections.
9
+ * - **Blocks** are fences whose info string names a block kind
10
+ * (`blocks.cjs`): ```` ```compare Quotes per head ````, then one item per
11
+ * line (`- Casa Lola: $29 | Family style`) and settings as `key: value`
12
+ * lines. A block is data the page runtime draws; opened anywhere else it
13
+ * reads as the list it is.
14
+ * - **Code**: a ```` ```css ```` fence styles the page; a ```` ```box ````
15
+ * fence is HTML (with its own style and script) drawn in a sealed box of
16
+ * its own. Neither ever holds the page's words.
17
+ *
18
+ * Every node keeps where it came from in the source, and every word in it is a
19
+ * run from `inline.cjs` with its own `[from, to)`, so the page can be edited and
20
+ * commented on as the note. The `comments` block is left out: the threads are
21
+ * the comment layer's (`comments.cjs`), never page content.
22
+ *
23
+ * Nothing here throws on a note it cannot make sense of: a fence that never
24
+ * closes runs to the end, a block line it cannot read is reported as a
25
+ * problem on that block and drawn as text.
26
+ */
27
+
28
+ const { inlineRuns } = require("./inline.cjs");
29
+ const { BLOCK_KINDS, checkBlock } = require("./blocks.cjs");
30
+ const { readScene } = require("../ink/drawing.cjs");
31
+ const { layoutProblems } = require("./layout.cjs");
32
+
33
+ const PAGE_SUFFIX = ".page.md";
34
+
35
+ /** Whether a note is a page, decided by its name like a drawing or a code note is. */
36
+ function isPagePath(path) {
37
+ return typeof path === "string" && path.toLowerCase().endsWith(PAGE_SUFFIX) && path.length > PAGE_SUFFIX.length;
38
+ }
39
+
40
+ /** The note's lines, each with its offsets: `{ text, from, to }`, `to` before the newline. */
41
+ function linesOf(source, start) {
42
+ const lines = [];
43
+ let from = start;
44
+ while (from <= source.length) {
45
+ const nl = source.indexOf("\n", from);
46
+ const to = nl === -1 ? source.length : nl;
47
+ lines.push({ text: source.slice(from, to).replace(/\r$/, ""), from, to });
48
+ if (nl === -1) break;
49
+ from = nl + 1;
50
+ }
51
+ return lines;
52
+ }
53
+
54
+ /** Front matter at the very top, as plain `key: value` pairs (nested YAML is left alone), and where the body starts. */
55
+ function frontMatter(source) {
56
+ const open = /^---[ \t]*\r?\n/.exec(source);
57
+ if (!open) return { fields: {}, end: 0 };
58
+ const close = /\n---[ \t]*(?:\r?\n|$)/.exec(source.slice(open[0].length - 1));
59
+ if (!close) return { fields: {}, end: 0 };
60
+ const bodyStart = open[0].length - 1 + close.index + close[0].length;
61
+ const fields = {};
62
+ for (const line of source.slice(open[0].length, open[0].length - 1 + close.index).split("\n")) {
63
+ const pair = /^([A-Za-z][\w-]{0,40}):[ \t]*(.*?)\s*$/.exec(line);
64
+ if (!pair) continue;
65
+ fields[pair[1].toLowerCase()] = pair[2].replace(/^(["'])(.*)\1$/, "$2");
66
+ }
67
+ return { fields, end: bodyStart };
68
+ }
69
+
70
+ const FENCE = /^( {0,3})(`{3,}|~{3,})[ \t]*([^`]*?)\s*$/;
71
+ const HEADING = /^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/;
72
+ const RULE = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
73
+ const LIST = /^([ \t]*)([-*+]|\d{1,3}[.)])[ \t]+(.*)$/;
74
+ const TASK = /^\[([ xX])\][ \t]+/;
75
+ const QUOTE = /^ {0,3}>[ \t]?(.*)$/;
76
+ const ALERT = /^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*/i;
77
+ const TABLE_SEP = /^ {0,3}\|?[ \t]*:?-{2,}:?[ \t]*(\|[ \t]*:?-{2,}:?[ \t]*)*\|?[ \t]*$/;
78
+
79
+ /** Read a whole page note. */
80
+ function parsePage(source) {
81
+ const text = typeof source === "string" ? source : "";
82
+ const front = frontMatter(text);
83
+ const lines = linesOf(text, front.end);
84
+ const nodes = [];
85
+ let i = 0;
86
+
87
+ const runsOf = (from, to) => inlineRuns(text, from, to);
88
+ const offsetIn = (line, part) => line.from + line.text.indexOf(part);
89
+
90
+ while (i < lines.length) {
91
+ const line = lines[i];
92
+ if (line.text.trim() === "") {
93
+ i++;
94
+ continue;
95
+ }
96
+
97
+ const fence = FENCE.exec(line.text);
98
+ if (fence) {
99
+ const marker = fence[2];
100
+ const info = fence[3];
101
+ let j = i + 1;
102
+ while (j < lines.length) {
103
+ const close = /^ {0,3}(`{3,}|~{3,})[ \t]*$/.exec(lines[j].text);
104
+ if (close && close[1][0] === marker[0] && close[1].length >= marker.length) break;
105
+ j++;
106
+ }
107
+ const closed = j < lines.length;
108
+ const bodyFrom = i + 1 < lines.length ? lines[i + 1].from : line.to;
109
+ const bodyTo = closed ? (j > i + 1 ? lines[j - 1].to : bodyFrom) : text.length;
110
+ const node = fenced(text, info, lines.slice(i + 1, j), {
111
+ from: line.from,
112
+ to: closed ? lines[j].to : text.length,
113
+ bodyFrom,
114
+ bodyTo,
115
+ closeFrom: closed ? lines[j].from : text.length,
116
+ closed,
117
+ });
118
+ if (node) nodes.push(node);
119
+ i = closed ? j + 1 : lines.length;
120
+ continue;
121
+ }
122
+
123
+ const heading = HEADING.exec(line.text);
124
+ if (heading) {
125
+ const textFrom = offsetIn(line, heading[2]);
126
+ const textTo = textFrom + heading[2].length;
127
+ nodes.push({ type: "heading", level: heading[1].length, runs: runsOf(textFrom, textTo), from: line.from, to: line.to });
128
+ i++;
129
+ continue;
130
+ }
131
+
132
+ if (RULE.test(line.text)) {
133
+ nodes.push({ type: "rule", from: line.from, to: line.to });
134
+ i++;
135
+ continue;
136
+ }
137
+
138
+ if (LIST.test(line.text)) {
139
+ const items = [];
140
+ let ordered = null;
141
+ while (i < lines.length && LIST.test(lines[i].text)) {
142
+ const [, indent, bullet, rest] = LIST.exec(lines[i].text);
143
+ if (ordered === null) ordered = /\d/.test(bullet);
144
+ const contentFrom = lines[i].to - rest.length;
145
+ const task = TASK.exec(rest);
146
+ const wordsFrom = task ? contentFrom + task[0].length : contentFrom;
147
+ // Lines indented under an item, that are not items themselves, continue it.
148
+ let end = lines[i].to;
149
+ let k = i + 1;
150
+ while (k < lines.length && /^[ \t]{2,}\S/.test(lines[k].text) && !LIST.test(lines[k].text)) end = lines[k++].to;
151
+ items.push({
152
+ depth: Math.min(3, Math.floor(indent.replace(/\t/g, " ").length / 2)),
153
+ task: task ? { checked: task[1] !== " ", at: contentFrom + 1 } : null,
154
+ runs: runsOf(wordsFrom, end),
155
+ from: lines[i].from,
156
+ to: end,
157
+ });
158
+ i = k;
159
+ }
160
+ nodes.push({ type: "list", ordered: ordered === true, items, from: items[0].from, to: items[items.length - 1].to });
161
+ continue;
162
+ }
163
+
164
+ if (QUOTE.test(line.text)) {
165
+ const parts = [];
166
+ const start = line.from;
167
+ let alert = null;
168
+ while (i < lines.length && QUOTE.test(lines[i].text)) {
169
+ const inner = QUOTE.exec(lines[i].text)[1];
170
+ let innerFrom = lines[i].to - inner.length;
171
+ if (parts.length === 0 && alert === null) {
172
+ const tag = ALERT.exec(inner);
173
+ if (tag) {
174
+ alert = tag[1].toLowerCase();
175
+ innerFrom += tag[0].length;
176
+ }
177
+ }
178
+ if (innerFrom < lines[i].to) parts.push([innerFrom, lines[i].to]);
179
+ i++;
180
+ }
181
+ const runs = [];
182
+ for (const [from, to] of parts) {
183
+ if (runs.length > 0) runs.push({ text: " ", from, to: from, editable: false, anchors: [] });
184
+ runs.push(...runsOf(from, to));
185
+ }
186
+ nodes.push({ type: "quote", alert, runs, from: start, to: lines[i - 1].to });
187
+ continue;
188
+ }
189
+
190
+ if (line.text.includes("|") && i + 1 < lines.length && TABLE_SEP.test(lines[i + 1].text)) {
191
+ const head = cells(text, line);
192
+ const rows = [];
193
+ let j = i + 2;
194
+ while (j < lines.length && lines[j].text.includes("|") && lines[j].text.trim() !== "") rows.push(cells(text, lines[j++]));
195
+ nodes.push({
196
+ type: "table",
197
+ head: head.map(([from, to]) => runsOf(from, to)),
198
+ rows: rows.map((row) => row.map(([from, to]) => runsOf(from, to))),
199
+ from: line.from,
200
+ to: lines[j - 1].to,
201
+ });
202
+ i = j;
203
+ continue;
204
+ }
205
+
206
+ // A paragraph: lines up to a blank one or the start of anything else.
207
+ const start = i;
208
+ while (
209
+ i < lines.length &&
210
+ lines[i].text.trim() !== "" &&
211
+ (i === start || !(FENCE.test(lines[i].text) || HEADING.test(lines[i].text) || LIST.test(lines[i].text) || QUOTE.test(lines[i].text)))
212
+ ) {
213
+ i++;
214
+ }
215
+ const from = lines[start].from + (lines[start].text.length - lines[start].text.trimStart().length);
216
+ nodes.push({ type: "paragraph", runs: runsOf(from, lines[i - 1].to), from: lines[start].from, to: lines[i - 1].to });
217
+ }
218
+
219
+ // A layout names sections by their `##` headings, so it is checked once the whole page is read (`layout.cjs`).
220
+ const sectionNames = nodes.filter((node) => node.type === "heading" && node.level === 2).map((node) => node.runs.map((run) => run.text ?? "").join("").trim());
221
+ nodes
222
+ .filter((node) => node.type === "layout")
223
+ .forEach((node, k) => {
224
+ node.problems = k > 0 ? [{ message: "A page has one layout; this is a second ```layout fence." }] : layoutProblems(node.html, sectionNames);
225
+ });
226
+
227
+ const firstHeading = nodes.find((node) => node.type === "heading" && node.level === 1);
228
+ return {
229
+ fields: front.fields,
230
+ bodyStart: front.end,
231
+ title: front.fields.title || (firstHeading ? firstHeading.runs.map((run) => run.text ?? "").join("").trim() : ""),
232
+ nodes,
233
+ problems: nodes.flatMap((node) => (node.problems ?? []).map((problem) => ({ ...problem, from: node.from }))),
234
+ };
235
+ }
236
+
237
+ /** A table row's cells, as source ranges with the pipes and padding left out. Escaped `\|` stays in its cell. */
238
+ function cells(source, line) {
239
+ const out = [];
240
+ let start = line.from;
241
+ const end = line.to;
242
+ let k = start;
243
+ while (k < end && /\s/.test(source[k])) k++;
244
+ if (source[k] === "|") start = k + 1;
245
+ for (k = start; k <= end; k++) {
246
+ if (source[k] === "\\") {
247
+ k++;
248
+ continue;
249
+ }
250
+ if (k === end || source[k] === "|") {
251
+ let a = start;
252
+ let b = k;
253
+ while (a < b && /\s/.test(source[a])) a++;
254
+ while (b > a && /\s/.test(source[b - 1])) b--;
255
+ if (!(k === end && a === b && out.length > 0)) out.push([a, b]);
256
+ start = k + 1;
257
+ }
258
+ }
259
+ return out;
260
+ }
261
+
262
+ /**
263
+ * A fenced block: a page block, a box, the page's CSS, or a plain code block.
264
+ * The comments block is dropped; it is the comment layer's.
265
+ */
266
+ function fenced(source, info, bodyLines, where) {
267
+ const word = (/^([A-Za-z][\w-]*)/.exec(info) ?? [, ""])[1].toLowerCase();
268
+ const rest = info.slice(word.length).trim();
269
+ const body = source.slice(where.bodyFrom, where.bodyTo);
270
+ if (word === "comments") return null;
271
+ if (word === "css") return { type: "css", text: body, ...where };
272
+ if (word === "layout") return { type: "layout", html: body, problems: [], ...where };
273
+ if (word === "box") {
274
+ const height = /(?:^|\s)height=(\d{2,4})(?:\s|$)/.exec(rest);
275
+ return { type: "box", html: body, height: height ? Math.min(2000, Math.max(80, Number(height[1]))) : null, ...where };
276
+ }
277
+ if (word === "drawing") {
278
+ // Tex's ink inside the page (`ink/drawing.cjs`): shapes only, read here so a scene that can't be drawn is a problem the write is refused for.
279
+ const read = readScene(body);
280
+ return {
281
+ type: "drawing",
282
+ caption: rest,
283
+ scene: read.error ? null : read,
284
+ problems: read.error ? [{ message: `This drawing can't be drawn: ${read.error}` }] : [],
285
+ ...where,
286
+ };
287
+ }
288
+ if (!Object.hasOwn(BLOCK_KINDS, word)) return { type: "code", lang: word, text: body, ...where };
289
+
290
+ const items = [];
291
+ const settings = {};
292
+ const problems = [];
293
+ const words = [];
294
+ for (const line of bodyLines) {
295
+ if (line.text.trim() === "") continue;
296
+ const item = /^[ \t]*(?:[-*+]|\d{1,3}[.)])[ \t]+(.*)$/.exec(line.text);
297
+ if (item) {
298
+ items.push(blockItem(source, line.to - item[1].length, line.to, line));
299
+ continue;
300
+ }
301
+ // Indented `key: value` lines under an item are that item's fields (`photo:`, `seats:`, a fact).
302
+ const last = items[items.length - 1];
303
+ if (last && /^[ \t]{2,}\S/.test(line.text)) {
304
+ const fields = itemFields(source, line);
305
+ if (fields) {
306
+ last.fields.push(...fields);
307
+ last.to = line.to;
308
+ continue;
309
+ }
310
+ }
311
+ const setting = /^[ \t]*([a-z][a-z _-]{0,24}):[ \t]*(.*?)[ \t]*$/i.exec(line.text);
312
+ if (setting) {
313
+ const valueFrom = line.to - (line.text.length - line.text.lastIndexOf(setting[2]));
314
+ const key = setting[1].trim().toLowerCase().replace(/[ -]/g, "_");
315
+ settings[key] = { value: setting[2], from: setting[2] === "" ? line.to : valueFrom, to: setting[2] === "" ? line.to : valueFrom + setting[2].length, lineFrom: line.from, lineTo: line.to };
316
+ continue;
317
+ }
318
+ // A kind that takes words (a vote's reason) keeps plain lines written before its items.
319
+ if (BLOCK_KINDS[word].words && items.length === 0) {
320
+ const from = line.from + (line.text.length - line.text.trimStart().length);
321
+ words.push(...(words.length ? [{ text: " ", from, to: from, editable: false, anchors: [] }] : []), ...inlineRuns(source, from, line.to));
322
+ continue;
323
+ }
324
+ problems.push({ message: `"${line.text.trim().slice(0, 40)}" is not an item ("- ..."), a setting ("key: value") or, indented under an item, one of its fields.` });
325
+ }
326
+ const block = { type: "block", kind: word, title: rest, items, settings, words, problems, ...where };
327
+ for (const message of checkBlock(block)) problems.push({ message });
328
+ return block;
329
+ }
330
+
331
+ /**
332
+ * One `- ` line of a block: its `|`-separated parts, and the first part split
333
+ * at its first `: ` into a label and a value when it has one. Each part keeps
334
+ * its range and its runs, so every word in it can be edited.
335
+ */
336
+ function blockItem(source, from, to, line) {
337
+ const parts = [];
338
+ let start = from;
339
+ for (let k = from; k <= to; k++) {
340
+ if (source[k] === "\\") {
341
+ k++;
342
+ continue;
343
+ }
344
+ if (k === to || source[k] === "|") {
345
+ let a = start;
346
+ let b = k;
347
+ while (a < b && /\s/.test(source[a])) a++;
348
+ while (b > a && /\s/.test(source[b - 1])) b--;
349
+ parts.push({ from: a, to: b });
350
+ start = k + 1;
351
+ }
352
+ }
353
+ const first = parts[0];
354
+ let label = null;
355
+ const colon = labelEnd(source, first.from, first.to);
356
+ if (colon !== -1) {
357
+ label = { from: first.from, to: colon };
358
+ let v = colon + 1;
359
+ while (v < first.to && /\s/.test(source[v])) v++;
360
+ parts[0] = { from: v, to: first.to };
361
+ }
362
+ const read = (part) => ({ ...part, text: source.slice(part.from, part.to), runs: inlineRuns(source, part.from, part.to) });
363
+ return { label: label ? read(label) : null, parts: parts.map(read), fields: [], from: line.from, to: line.to };
364
+ }
365
+
366
+ /** A field's name as written ("On the L") and as looked up ("on_the_l"). */
367
+ const FIELD = /^([A-Za-z0-9][^:;|]{0,30}?):[ \t]+(\S.*)$/;
368
+
369
+ /**
370
+ * One indented line of fields: `seats: 300-500`, or several facts on one line
371
+ * split at `; ` (`kitchen: yes; on the L: no`). Null when the line is not
372
+ * fields at all, so the caller can say so.
373
+ */
374
+ function itemFields(source, line) {
375
+ const start = line.from + (line.text.length - line.text.trimStart().length);
376
+ const text = source.slice(start, line.to).replace(/\s+$/, "");
377
+ const segments = [];
378
+ const splitter = /;[ \t]+(?=[A-Za-z0-9][^:;|]{0,30}?:[ \t])/g;
379
+ let at = 0;
380
+ for (const m of text.matchAll(splitter)) {
381
+ segments.push([at, m.index]);
382
+ at = m.index + m[0].length;
383
+ }
384
+ segments.push([at, text.length]);
385
+ const fields = [];
386
+ for (const [a, b] of segments) {
387
+ const m = FIELD.exec(text.slice(a, b));
388
+ if (!m) return null;
389
+ const keyFrom = start + a;
390
+ const valueFrom = keyFrom + m[0].length - m[2].length;
391
+ const valueTo = start + b;
392
+ fields.push({
393
+ name: m[1].trim(),
394
+ key: m[1].trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_|_$/g, ""),
395
+ value: { from: valueFrom, to: valueTo, text: source.slice(valueFrom, valueTo), runs: inlineRuns(source, valueFrom, valueTo) },
396
+ line: { from: line.from, to: line.to, alone: segments.length === 1 },
397
+ });
398
+ }
399
+ return fields;
400
+ }
401
+
402
+ /** Where a label ends: the first unescaped `:` followed by a space, outside a link or code. */
403
+ function labelEnd(source, from, to) {
404
+ let depth = 0;
405
+ for (let k = from; k < to; k++) {
406
+ const ch = source[k];
407
+ if (ch === "\\") {
408
+ k++;
409
+ continue;
410
+ }
411
+ if (ch === "[" || ch === "(" || ch === "`") depth += ch === "`" ? (depth % 2 === 0 ? 1 : -1) : 1;
412
+ if ((ch === "]" || ch === ")") && depth > 0) depth--;
413
+ if (ch === ":" && depth === 0 && /\s/.test(source[k + 1] ?? "") && k > from) return k;
414
+ }
415
+ return -1;
416
+ }
417
+
418
+ module.exports = { PAGE_SUFFIX, isPagePath, parsePage, frontMatter };