explico 0.4.0 → 0.5.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 (143) hide show
  1. package/CHANGELOG.md +273 -1
  2. package/README.md +103 -8
  3. package/dist/compiler/astro.d.ts +9 -1
  4. package/dist/compiler/astro.js +182 -3
  5. package/dist/compiler/cli.d.ts +13 -0
  6. package/dist/compiler/cli.js +28 -4
  7. package/dist/compiler/collections.d.ts +3 -2
  8. package/dist/compiler/collections.js +28 -1
  9. package/dist/compiler/course-config.d.ts +58 -1
  10. package/dist/compiler/course-config.js +28 -1
  11. package/dist/compiler/exports/standalone-html.d.ts +34 -0
  12. package/dist/compiler/exports/standalone-html.js +803 -0
  13. package/dist/compiler/manifest.d.ts +90 -6
  14. package/dist/compiler/manifest.js +150 -17
  15. package/dist/compiler/resolution-report.d.ts +4 -0
  16. package/dist/reference/base-abbreviations.d.mts +30 -0
  17. package/dist/reference/base-abbreviations.mjs +60 -0
  18. package/dist/reference/base-operators.d.mts +15 -0
  19. package/dist/reference/base-operators.mjs +40 -0
  20. package/dist/reference/base-roman-labels.d.mts +35 -0
  21. package/dist/reference/base-roman-labels.mjs +43 -0
  22. package/dist/reference/consistency.d.ts +8 -2
  23. package/dist/reference/consistency.js +50 -3
  24. package/dist/reference/diagrams-validate.d.ts +21 -0
  25. package/dist/reference/diagrams-validate.js +13 -0
  26. package/dist/reference/equations.d.mts +10 -15
  27. package/dist/reference/equations.mjs +67 -14
  28. package/dist/reference/gate-abbreviations.d.ts +17 -0
  29. package/dist/reference/gate-abbreviations.js +64 -0
  30. package/dist/reference/gate-math.d.ts +28 -5
  31. package/dist/reference/gate-math.js +5 -2
  32. package/dist/reference/gate-prose-notation.d.ts +60 -0
  33. package/dist/reference/gate-prose-notation.js +125 -0
  34. package/dist/reference/keyed-float-validate.d.ts +5 -5
  35. package/dist/reference/keyed-jsx-floats.d.mts +7 -12
  36. package/dist/reference/keyed-jsx-floats.mjs +50 -25
  37. package/dist/reference/knowledge-checks-validate.d.ts +35 -0
  38. package/dist/reference/knowledge-checks-validate.js +78 -0
  39. package/dist/reference/knowledge-checks.d.mts +25 -0
  40. package/dist/reference/knowledge-checks.mjs +45 -0
  41. package/dist/reference/lab-math-scope.d.ts +9 -1
  42. package/dist/reference/lab-math-scope.js +32 -4
  43. package/dist/reference/math-glyphs.d.mts +30 -0
  44. package/dist/reference/math-glyphs.mjs +367 -0
  45. package/dist/reference/mdx-attributes.d.mts +13 -0
  46. package/dist/reference/mdx-attributes.mjs +38 -0
  47. package/dist/reference/mixed-content.d.ts +17 -0
  48. package/dist/reference/mixed-content.js +29 -0
  49. package/dist/reference/page-notation.d.ts +52 -0
  50. package/dist/reference/page-notation.js +171 -0
  51. package/dist/reference/raw-floats-validate.d.ts +18 -0
  52. package/dist/reference/raw-floats-validate.js +53 -0
  53. package/dist/reference/raw-floats.d.mts +18 -0
  54. package/dist/reference/raw-floats.mjs +129 -0
  55. package/dist/reference/rehype-heading-anchors.d.mts +1 -1
  56. package/dist/reference/rehype-heading-anchors.mjs +107 -1
  57. package/dist/reference/rehype-table-header-labels.d.mts +26 -0
  58. package/dist/reference/rehype-table-header-labels.mjs +152 -0
  59. package/dist/reference/remark-abbreviations.d.mts +7 -0
  60. package/dist/reference/remark-abbreviations.mjs +174 -0
  61. package/dist/reference/remark-memo.d.mts +16 -0
  62. package/dist/reference/remark-memo.mjs +339 -0
  63. package/dist/reference/remark-notation.d.mts +57 -9
  64. package/dist/reference/remark-notation.mjs +354 -42
  65. package/dist/reference/remark-short-note.d.mts +4 -0
  66. package/dist/reference/remark-short-note.mjs +237 -0
  67. package/dist/reference/types.d.ts +16 -1
  68. package/dist/reference/units-vocab.js +4 -0
  69. package/docs/VERSIONING.md +33 -4
  70. package/docs/authoring/README.md +8 -0
  71. package/docs/authoring/agents.md +17 -2
  72. package/docs/authoring/components.md +135 -0
  73. package/docs/authoring/migrating.md +92 -7
  74. package/docs/authoring/notation.md +560 -1
  75. package/docs/authoring/structure.md +120 -8
  76. package/docs/authoring/visualizations.md +36 -3
  77. package/package.json +5 -2
  78. package/src/compiler/astro.ts +208 -3
  79. package/src/compiler/cli.ts +29 -2
  80. package/src/compiler/collections.ts +45 -4
  81. package/src/compiler/course-config.ts +92 -0
  82. package/src/compiler/exports/standalone-html.ts +963 -0
  83. package/src/compiler/manifest.ts +268 -22
  84. package/src/components/HoverPanel.astro +45 -4
  85. package/src/components/abbreviation/AbbreviationGlossary.astro +70 -0
  86. package/src/components/abbreviation/AbbreviationLayer.astro +127 -0
  87. package/src/components/assessments/AssessmentRunner.tsx +52 -17
  88. package/src/components/assessments/AssessmentSet.astro +98 -14
  89. package/src/components/assessments/RenderedContent.tsx +55 -0
  90. package/src/components/assessments/assessment-randomization.ts +16 -0
  91. package/src/components/diagram/Diagram.astro +444 -83
  92. package/src/components/disclosure/hover-preview.ts +202 -0
  93. package/src/components/disclosure/persisted-disclosure.ts +66 -0
  94. package/src/components/examples/CompactExamples.astro +2 -0
  95. package/src/components/figure/Figure.astro +279 -10
  96. package/src/components/memo/Memo.astro +453 -0
  97. package/src/components/memo/ShortNote.astro +223 -0
  98. package/src/components/notation/NotationFormula.astro +18 -1
  99. package/src/components/notation/NotationLayer.astro +105 -8
  100. package/src/components/notation/render-notation-math.ts +39 -2
  101. package/src/components/notation/safe-math-to-react.ts +50 -0
  102. package/src/components/reference/FloatRefLayer.astro +233 -15
  103. package/src/components/starlight/CollapsibleSections.astro +461 -0
  104. package/src/components/starlight/DiagramEnhancer.astro +385 -0
  105. package/src/components/starlight/HeadingAnchorEnhancer.astro +5 -17
  106. package/src/components/starlight/KeyedFloatEnhancer.astro +81 -3
  107. package/src/components/starlight/LayoutHeader.astro +50 -0
  108. package/src/components/starlight/LayoutSocialIcons.astro +41 -2
  109. package/src/components/starlight/LessonExportActions.astro +436 -0
  110. package/src/components/starlight/LessonFooter.astro +27 -2
  111. package/src/components/starlight/section-parts.ts +31 -0
  112. package/src/components/table/Table.astro +293 -7
  113. package/src/components/table/TableOverflowExpander.astro +489 -0
  114. package/src/content-config.ts +20 -0
  115. package/src/reference/base-abbreviations.mjs +60 -0
  116. package/src/reference/base-operators.mjs +40 -0
  117. package/src/reference/base-roman-labels.mjs +43 -0
  118. package/src/reference/consistency.ts +73 -2
  119. package/src/reference/diagrams-validate.ts +40 -0
  120. package/src/reference/equations.mjs +67 -14
  121. package/src/reference/gate-abbreviations.ts +128 -0
  122. package/src/reference/gate-math.ts +27 -7
  123. package/src/reference/gate-prose-notation.ts +224 -0
  124. package/src/reference/keyed-float-validate.ts +5 -5
  125. package/src/reference/keyed-jsx-floats.mjs +50 -25
  126. package/src/reference/knowledge-checks-validate.ts +130 -0
  127. package/src/reference/knowledge-checks.mjs +45 -0
  128. package/src/reference/lab-math-scope.ts +45 -3
  129. package/src/reference/math-glyphs.mjs +367 -0
  130. package/src/reference/mdx-attributes.mjs +38 -0
  131. package/src/reference/mixed-content.ts +69 -0
  132. package/src/reference/page-notation.ts +252 -0
  133. package/src/reference/raw-floats-validate.ts +103 -0
  134. package/src/reference/raw-floats.mjs +129 -0
  135. package/src/reference/rehype-heading-anchors.mjs +107 -1
  136. package/src/reference/rehype-table-header-labels.mjs +152 -0
  137. package/src/reference/remark-abbreviations.mjs +174 -0
  138. package/src/reference/remark-memo.mjs +339 -0
  139. package/src/reference/remark-notation.mjs +354 -42
  140. package/src/reference/remark-short-note.mjs +237 -0
  141. package/src/reference/types.ts +16 -0
  142. package/src/reference/units-vocab.ts +4 -0
  143. package/src/styles/global.css +996 -50
@@ -0,0 +1,224 @@
1
+ /**
2
+ * The plain-text-notation gate (rule N9), run from the CLI alongside the
3
+ * completeness gate (`gate-math.ts`). Where that gate asks "does every glyph
4
+ * *inside* `$…$` resolve", this one asks the complementary question: "is
5
+ * anything that reads as math sitting *outside* `$…$` in the first place" —
6
+ * a unit abbreviation, a division of two variable-like symbols, an escaped
7
+ * absolute-value bar, a hand-typed sub/superscript. Both gates share one
8
+ * `MathGateDiagnostic` shape and one output array (`manifest.diagnostics.math`)
9
+ * so the CLI needs no separate wiring.
10
+ *
11
+ * Two entry points, one set of checks (`findingsIn`): `gateProseNotation`
12
+ * scans `lesson.body` with fenced/inline code and every `$…$` / `$$…$$` span
13
+ * already blanked out (same `stripCode` + math-span erasure `gate-math.ts`
14
+ * uses), so it only ever sees prose and table cells. `gateProseNotationInAssessments`
15
+ * scans an assessment item's `prompt` / `explanation` the same way — a plain
16
+ * JSON string field, not Markdown, but one that renders through the same
17
+ * KaTeX pipeline as a lesson body since `AssessmentRunner` picked it up
18
+ * (`mixed-content.ts`), so the same rule applies.
19
+ */
20
+ import type { MathGateDiagnostic } from './gate-math';
21
+ import type { NotationLessonInput } from './types';
22
+
23
+ interface AssessmentItemLike {
24
+ readonly id?: string;
25
+ readonly prompt?: string;
26
+ readonly explanation?: string;
27
+ }
28
+ interface AssessmentLike {
29
+ readonly id: string;
30
+ readonly items?: readonly AssessmentItemLike[];
31
+ }
32
+
33
+ const CODE_FENCE = /```[\s\S]*?```/g;
34
+ const INLINE_CODE = /(^|[^`])`[^`\n]*`/g;
35
+ const DISPLAY_MATH = /\$\$([\s\S]+?)\$\$/g;
36
+ const INLINE_MATH = /(?<![\\$])\$(?!\s)([^$\n]+?)(?<![\\\s])\$(?!\d)/g;
37
+ // MDX `import … from '…/…'` lines: the path's `/` is a module separator, not
38
+ // a division of two symbols.
39
+ const IMPORT_LINE = /^[ \t]*import\b[^\n]*$/gm;
40
+ // `[[key]]` / `[[slug/lesson#anchor]]` reference syntax: never prose math,
41
+ // and a cross-page slug legitimately contains a `/`.
42
+ const KEY_REFERENCE = /\[\[[^\]\n]*\]\]/g;
43
+ // A JSX tag, opening through its matching `>` — may span several lines when
44
+ // each prop is on its own line. Component prop strings (`caption="…"`,
45
+ // `title="…"`) are plain display text with no math rendering at all (Figure
46
+ // and Table hand their `caption` straight to the DOM as a string), so N9
47
+ // does not apply inside one — enforcing "use $…$" somewhere it can't render
48
+ // would just break the caption instead of fixing it.
49
+ const JSX_TAG = /<\/?[A-Za-z][^<>]*>/g;
50
+
51
+ /**
52
+ * Blank code, math, reference, and JSX-tag spans so offsets don't shift.
53
+ * Exported so `gate-abbreviations.ts` (N14) scans exactly the same "is this
54
+ * really prose" surface as N9, instead of drifting with a second copy of
55
+ * these regexes.
56
+ */
57
+ export function proseOnly(body: string): string {
58
+ const blank = (match: string) => match.replace(/[^\n]/g, ' ');
59
+ return body
60
+ .replace(CODE_FENCE, blank)
61
+ .replace(INLINE_CODE, (match, lead: string) => lead + blank(match.slice(lead.length)))
62
+ .replace(DISPLAY_MATH, blank)
63
+ .replace(INLINE_MATH, blank)
64
+ .replace(IMPORT_LINE, blank)
65
+ .replace(KEY_REFERENCE, blank)
66
+ .replace(JSX_TAG, blank);
67
+ }
68
+
69
+ /** Exported for the same reason as {@link proseOnly}. */
70
+ export function lineOf(text: string, index: number): number {
71
+ return text.slice(0, index).split('\n').length;
72
+ }
73
+
74
+ // Symbolic/abbreviated unit tokens that must be typeset in `$…$`, longest
75
+ // first so the alternation can't match a prefix of a longer unit.
76
+ const UNIT = String.raw`m/s²|m/s|km/h|rad/s|m²/s²|J/kg|m²|s⁻¹|kg|N|J|W|Hz|deg|°|rad|m|s`;
77
+
78
+ const NUMERAL_UNIT = new RegExp(
79
+ String.raw`(?<![\w.])-?\d+(?:\.\d+)?\s?(?:${UNIT})(?![A-Za-z])`,
80
+ 'g',
81
+ );
82
+ const BARE_UNIT_PARENS = new RegExp(String.raw`\((?:${UNIT})\)`, 'g');
83
+
84
+ // Idiomatic English slash constructs that are prose, not a division of two
85
+ // symbols — reviewed and extended in the same PR that adds a new one, not by
86
+ // a per-page suppression (there is no lint-ignore.yml entry for this gate;
87
+ // see N9's "everything, no exceptions" scope).
88
+ const SLASH_IDIOMS = new Set([
89
+ 'and/or',
90
+ 'his/her',
91
+ 'him/her',
92
+ 'he/she',
93
+ 'she/he',
94
+ 'on/off',
95
+ 'yes/no',
96
+ 'pass/fail',
97
+ 'input/output',
98
+ 'output/input',
99
+ 'read/write',
100
+ 'either/or',
101
+ 'client/server',
102
+ 'pre/post',
103
+ 'a/b',
104
+ ]);
105
+ const SLASH_TOKENS = /\b([A-Za-z]{1,6})\/([A-Za-z]{1,6})\b/g;
106
+
107
+ // A `_` or `^` sitting directly between two alphanumerics reads as a
108
+ // hand-typed sub/superscript that was never wrapped in `$…$`.
109
+ const FAKE_SCRIPT = /[A-Za-z0-9][_^][A-Za-z0-9{]/g;
110
+
111
+ interface Finding {
112
+ readonly index: number;
113
+ readonly token: string;
114
+ readonly message: string;
115
+ }
116
+
117
+ function findAll(text: string, pattern: RegExp, toFinding: (m: RegExpExecArray) => Finding): Finding[] {
118
+ const out: Finding[] = [];
119
+ const re = new RegExp(pattern.source, pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`);
120
+ let match: RegExpExecArray | null;
121
+ while ((match = re.exec(text))) {
122
+ out.push(toFinding(match));
123
+ if (match[0].length === 0) re.lastIndex++;
124
+ }
125
+ return out;
126
+ }
127
+
128
+ const FIX = 'Wrap it in $…$ (or $$…$$) instead — see docs/authoring/notation.md#n9.';
129
+
130
+ /** Every N9 finding in one already-prose-only string, in source order. */
131
+ function findingsIn(text: string): Finding[] {
132
+ return [
133
+ ...findAll(text, /\\\|/g, (m) => ({
134
+ index: m.index,
135
+ token: m[0],
136
+ message: `Escaped pipe ${JSON.stringify(m[0])} outside math. Use \\lvert…\\rvert for an absolute value or norm inside $…$, not an escaped table pipe. ${FIX}`,
137
+ })),
138
+ ...findAll(text, NUMERAL_UNIT, (m) => ({
139
+ index: m.index,
140
+ token: m[0].trim(),
141
+ message: `Numeral with a plain-text unit ${JSON.stringify(m[0].trim())} outside math. ${FIX}`,
142
+ })),
143
+ ...findAll(text, BARE_UNIT_PARENS, (m) => ({
144
+ index: m.index,
145
+ token: m[0],
146
+ message: `Plain-text unit ${JSON.stringify(m[0])} outside math. ${FIX}`,
147
+ })),
148
+ ...findAll(text, FAKE_SCRIPT, (m) => ({
149
+ index: m.index,
150
+ token: m[0],
151
+ message: `${JSON.stringify(m[0])} looks like a hand-typed sub/superscript outside math. ${FIX}`,
152
+ })),
153
+ ...findAll(text, SLASH_TOKENS, (m) => {
154
+ if (SLASH_IDIOMS.has(m[0].toLowerCase())) return null as unknown as Finding;
155
+ return {
156
+ index: m.index,
157
+ token: m[0],
158
+ message: `${JSON.stringify(m[0])} outside math reads as a division of two symbols/units, not English prose. If it really is prose, add the idiom to SLASH_IDIOMS in gate-prose-notation.ts (reviewed like any other rule change). ${FIX}`,
159
+ };
160
+ }).filter(Boolean),
161
+ ];
162
+ }
163
+
164
+ /**
165
+ * Scan every lesson body for plain-text math notation outside `$…$`. Returns
166
+ * one `MathGateDiagnostic` (context `'prose'`, always an error — N9 has no
167
+ * escape hatch) per finding.
168
+ */
169
+ export function gateProseNotation(
170
+ lessons: readonly NotationLessonInput[],
171
+ ): MathGateDiagnostic[] {
172
+ const diagnostics: MathGateDiagnostic[] = [];
173
+
174
+ for (const lesson of lessons) {
175
+ const text = proseOnly(lesson.body);
176
+ for (const finding of findingsIn(text)) {
177
+ diagnostics.push({
178
+ context: 'prose',
179
+ severity: 'error',
180
+ file: lesson.source.file,
181
+ line: lineOf(lesson.body, finding.index),
182
+ token: finding.token,
183
+ message: finding.message,
184
+ });
185
+ }
186
+ }
187
+
188
+ return diagnostics;
189
+ }
190
+
191
+ /**
192
+ * Scan every assessment `prompt` / `explanation` for the same plain-text
193
+ * notation N9 forbids in a lesson body — `AssessmentRunner` renders both
194
+ * through the same KaTeX pipeline now (`mixed-content.ts`), so text sitting
195
+ * outside `$…$` there is just as invisible to the reader's hover cards as it
196
+ * would be in a lesson. No line number: an assessment item is one JSON string
197
+ * field, not a multi-line source file, so `file` names the item instead —
198
+ * same `"<assessmentId> (<itemId>)"` shape `gate-math.ts` already uses for
199
+ * this context.
200
+ */
201
+ export function gateProseNotationInAssessments(
202
+ assessments: readonly AssessmentLike[],
203
+ ): MathGateDiagnostic[] {
204
+ const diagnostics: MathGateDiagnostic[] = [];
205
+
206
+ for (const assessment of assessments) {
207
+ for (const item of assessment.items ?? []) {
208
+ for (const field of [item.prompt, item.explanation]) {
209
+ if (typeof field !== 'string') continue;
210
+ for (const finding of findingsIn(proseOnly(field))) {
211
+ diagnostics.push({
212
+ context: 'prose',
213
+ severity: 'error',
214
+ file: `${assessment.id} (${item.id ?? '?'})`,
215
+ token: finding.token,
216
+ message: finding.message,
217
+ });
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ return diagnostics;
224
+ }
@@ -6,13 +6,13 @@
6
6
  * - `<prefix>-invalid-key` (error): an `id` must match the key grammar.
7
7
  * - `<prefix>-duplicate-key` (error): an `id` is unique per lesson.
8
8
  * - `<prefix>-ref-resolves` (error): every `[[<prefix>-key]]` /
9
- * `[[slug#<prefix>-key]]` points at a `<Table>`/`<Figure>` with that `id`.
9
+ * `[[slug#<prefix>-key]]` points at the matching keyed float with that `id`.
10
10
  * - `<prefix>-key-unused` (warning): a keyed float never referenced.
11
11
  *
12
- * `tables-validate.ts` and `figures-validate.ts` are thin wrappers over this
13
- * so callers import `validateTables` / `validateFigures` with their own
14
- * narrow diagnostic-code types, while `equations-validate.ts` stays
15
- * untouched — equations predate this and keep their own dedicated path.
12
+ * `tables-validate.ts`, `figures-validate.ts`, and `diagrams-validate.ts` are
13
+ * thin wrappers over this so callers import a kind-specific validator with
14
+ * narrow diagnostic-code types, while `equations-validate.ts` stays untouched
15
+ * — equations predate this and keep their own dedicated path.
16
16
  */
17
17
 
18
18
  import {
@@ -1,20 +1,20 @@
1
1
  /**
2
- * Keyed-float identity for a JSX-wrapped table or figure, shared between
3
- * `<Table>` and `<Figure>` (Step F-tables-figures).
2
+ * Keyed-float identity for a JSX-wrapped table, figure, or diagram, shared
3
+ * between `<Table>`, `<Figure>`, and `<Diagram>`.
4
4
  *
5
5
  * This mirrors `equations.mjs`, but the key comes from a plain `id` prop on
6
6
  * the wrapper component rather than a `\label{eq:key}` embedded in shared
7
- * LaTeX source — a `<Table>`/`<Figure>` is purpose-built, so there is no
7
+ * LaTeX source — a `<Table>`/`<Figure>`/`<Diagram>` is purpose-built, so there is no
8
8
  * ambiguity an embedded marker would need to resolve. An author writes the
9
9
  * bare key (`id="kinematics-summary"`); the rendered DOM id and the `[[…]]`
10
10
  * reference prefix are computed the same way `\label{eq:key}` becomes
11
- * `#eq-key` — `keyedFloatAnchorId` prepends `tab-`/`fig-`.
11
+ * `#eq-key` — `keyedFloatAnchorId` prepends `tab-`/`fig-`/`dia-`.
12
12
  *
13
- * Only top-level occurrences count, exactly like `collectDisplayEquations`:
14
- * a `<Table>` nested inside a `<CompactExample>` (or any other component) is
15
- * illustrative, not part of the lesson's official sequence, so both the
16
- * mdast walker and the raw-text scanner only look at the document's direct
17
- * top-level blocks.
13
+ * Only top-level occurrences count, exactly like `collectDisplayEquations`,
14
+ * except that `<Memo>` is transparent: a float in a memo remains a side-note
15
+ * in the lesson's reading order. A keyed float nested inside a
16
+ * `<CompactExample>` (or any other component) is illustrative, not part of the
17
+ * official sequence.
18
18
  *
19
19
  * `scanKeyedJsxFloats` (raw Markdown, for the manifest builder) and
20
20
  * `collectKeyedJsxFloats` (mdast, for `remark-notation.mjs`) MUST agree —
@@ -41,7 +41,7 @@ function floatNumber(section, index) {
41
41
  * scheme is `‹part›.‹offset + indexInLesson›` and `##` headings are ignored;
42
42
  * without it, the legacy `##`-section scheme. Mirrors `displayEquationNumber`
43
43
  * in `equations.mjs`. Each kind of float counts on its own — a `<Table>` never
44
- * advances the `<Figure>` sequence — so `offset` is per kind too.
44
+ * advances the `<Figure>` or `<Diagram>` sequence — so `offset` is per kind too.
45
45
  */
46
46
  function keyedFloatNumber({
47
47
  prefix,
@@ -91,7 +91,10 @@ function readIdAttribute(attributesSource) {
91
91
  */
92
92
  export function scanKeyedJsxFloats(markdown, { tagName, numbering }) {
93
93
  const { prefix, offset } = resolveNumbering(numbering);
94
- const text = stripJsxComponents(stripFencedCode(markdown), tagName);
94
+ const text = stripJsxComponents(stripFencedCode(markdown), tagName, [
95
+ 'Memo',
96
+ 'ShortNote',
97
+ ]);
95
98
  const tokens = [];
96
99
  const heading = /^[ \t]{0,3}##[ \t]+\S/gm;
97
100
  const opening = new RegExp(`<${tagName}\\b([^>]*)>`, 'g');
@@ -141,11 +144,12 @@ export function countKeyedJsxFloats(markdown, { tagName }) {
141
144
  }
142
145
 
143
146
  /**
144
- * The mdast equivalent of `scanKeyedJsxFloats`: walk the tree's *top-level*
145
- * children in document order, advancing the section counter on every
146
- * depth-2 heading and emitting a record for every `mdxJsxFlowElement` named
147
- * `tagName` — keyed or not, exactly like `collectDisplayEquations`. `numbering`
148
- * is threaded the same way as in `scanKeyedJsxFloats`.
147
+ * The mdast equivalent of `scanKeyedJsxFloats`: walk the tree's top-level
148
+ * children (and through `<Memo>` bodies) in document order, advancing the
149
+ * section counter on every top-level depth-2 heading and emitting a record for
150
+ * every `mdxJsxFlowElement` named `tagName` — keyed or not, exactly like
151
+ * `collectDisplayEquations`. `numbering` is threaded the same way as in
152
+ * `scanKeyedJsxFloats`.
149
153
  */
150
154
  export function collectKeyedJsxFloats(tree, { tagName, numbering }) {
151
155
  const { prefix, offset } = resolveNumbering(numbering);
@@ -154,14 +158,8 @@ export function collectKeyedJsxFloats(tree, { tagName, numbering }) {
154
158
  let indexInSection = 0;
155
159
  let indexInLesson = 0;
156
160
 
157
- const children = Array.isArray(tree?.children) ? tree.children : [];
158
- for (const node of children) {
159
- if (!node || typeof node !== 'object') continue;
160
- if (node.type === 'heading' && node.depth === 2) {
161
- section += 1;
162
- indexInSection = 0;
163
- continue;
164
- }
161
+ const collectFromDetail = (node) => {
162
+ if (!node || typeof node !== 'object') return;
165
163
  if (node.type === 'mdxJsxFlowElement' && node.name === tagName) {
166
164
  indexInSection += 1;
167
165
  indexInLesson += 1;
@@ -184,8 +182,35 @@ export function collectKeyedJsxFloats(tree, { tagName, numbering }) {
184
182
  indexInSection,
185
183
  node,
186
184
  });
185
+ return;
186
+ }
187
+ if (
188
+ node.type === 'mdxJsxFlowElement' &&
189
+ node.name !== 'Memo' &&
190
+ node.name !== 'ShortNote'
191
+ ) {
192
+ return;
193
+ }
194
+ if (!Array.isArray(node.children)) return;
195
+ for (const child of node.children) collectFromDetail(child);
196
+ };
197
+
198
+ const children = Array.isArray(tree?.children) ? tree.children : [];
199
+ for (const node of children) {
200
+ if (!node || typeof node !== 'object') continue;
201
+ if (node.type === 'heading' && node.depth === 2) {
202
+ section += 1;
203
+ indexInSection = 0;
204
+ continue;
205
+ }
206
+ if (node.type === 'mdxJsxFlowElement' && node.name === tagName) {
207
+ collectFromDetail(node);
208
+ } else if (
209
+ node.type === 'mdxJsxFlowElement' &&
210
+ (node.name === 'Memo' || node.name === 'ShortNote')
211
+ ) {
212
+ collectFromDetail(node);
187
213
  }
188
214
  }
189
215
  return floats;
190
216
  }
191
-
@@ -0,0 +1,130 @@
1
+ /** Build-time identity and reference validation for knowledge checks. */
2
+
3
+ import {
4
+ collectKnowledgeChecks,
5
+ KNOWLEDGE_CHECK_KEY_PATTERN,
6
+ parseKnowledgeCheckRef,
7
+ } from './knowledge-checks.mjs';
8
+
9
+ export interface KnowledgeCheckLessonInput {
10
+ readonly lessonId: string;
11
+ readonly slug: string;
12
+ readonly file: string;
13
+ readonly body: string;
14
+ readonly assessmentIds: readonly string[];
15
+ readonly numbering?: { readonly prefix: number; readonly offset: number };
16
+ }
17
+
18
+ export interface KnowledgeCheckLabelRecord {
19
+ readonly key: string;
20
+ readonly number: string;
21
+ readonly lessonId: string;
22
+ readonly slug: string;
23
+ readonly indexInLesson: number;
24
+ }
25
+
26
+ export interface KnowledgeCheckDiagnostic {
27
+ readonly code:
28
+ | 'check-invalid-key'
29
+ | 'check-duplicate-key'
30
+ | 'check-ref-resolves';
31
+ readonly severity: 'error';
32
+ readonly message: string;
33
+ readonly file?: string;
34
+ readonly lessonId?: string;
35
+ readonly key?: string;
36
+ }
37
+
38
+ export interface KnowledgeCheckValidation {
39
+ readonly labels: readonly KnowledgeCheckLabelRecord[];
40
+ readonly numbersBySlug: Record<string, Record<string, string>>;
41
+ readonly diagnostics: readonly KnowledgeCheckDiagnostic[];
42
+ }
43
+
44
+ const BRACKET_REF = /\[\[\s*([^\][]+?)\s*\]\]/g;
45
+
46
+ function stripCode(text: string): string {
47
+ return text
48
+ .replace(/```[\s\S]*?```/g, ' ')
49
+ .replace(/~~~[\s\S]*?~~~/g, ' ')
50
+ .replace(/`[^`\n]*`/g, ' ');
51
+ }
52
+
53
+ export function validateKnowledgeChecks(
54
+ lessons: readonly KnowledgeCheckLessonInput[],
55
+ ): KnowledgeCheckValidation {
56
+ const labels: KnowledgeCheckLabelRecord[] = [];
57
+ const diagnostics: KnowledgeCheckDiagnostic[] = [];
58
+ const numbersBySlug: Record<string, Record<string, string>> = {};
59
+
60
+ for (const lesson of [...lessons].sort((a, b) =>
61
+ a.lessonId.localeCompare(b.lessonId),
62
+ )) {
63
+ const seen = new Set<string>();
64
+ const table: Record<string, string> = {};
65
+ for (const check of collectKnowledgeChecks(
66
+ lesson.assessmentIds,
67
+ lesson.numbering,
68
+ )) {
69
+ if (!KNOWLEDGE_CHECK_KEY_PATTERN.test(check.key)) {
70
+ diagnostics.push({
71
+ code: 'check-invalid-key',
72
+ severity: 'error',
73
+ message: `${lesson.lessonId} has a knowledge-check id "${check.key}" that is not a valid content key.`,
74
+ file: lesson.file,
75
+ lessonId: lesson.lessonId,
76
+ key: check.key,
77
+ });
78
+ continue;
79
+ }
80
+ if (seen.has(check.key)) {
81
+ diagnostics.push({
82
+ code: 'check-duplicate-key',
83
+ severity: 'error',
84
+ message: `${lesson.lessonId} lists knowledge check "${check.key}" twice; an assessment id is unique per lesson.`,
85
+ file: lesson.file,
86
+ lessonId: lesson.lessonId,
87
+ key: check.key,
88
+ });
89
+ continue;
90
+ }
91
+ seen.add(check.key);
92
+ table[check.key] = check.number;
93
+ labels.push({
94
+ ...check,
95
+ lessonId: lesson.lessonId,
96
+ slug: lesson.slug,
97
+ });
98
+ }
99
+ numbersBySlug[lesson.slug] = table;
100
+ }
101
+
102
+ for (const lesson of lessons) {
103
+ for (const match of stripCode(lesson.body).matchAll(BRACKET_REF)) {
104
+ const ref = parseKnowledgeCheckRef((match[1] ?? '').trim());
105
+ if (!ref) continue;
106
+ const targetSlug = ref.lesson ?? lesson.slug;
107
+ if (numbersBySlug[targetSlug]?.[ref.key] !== undefined) continue;
108
+ diagnostics.push({
109
+ code: 'check-ref-resolves',
110
+ severity: 'error',
111
+ message: ref.lesson
112
+ ? `${lesson.lessonId} references [[${ref.lesson}#check-${ref.key}]] but that lesson has no knowledge check "${ref.key}"`
113
+ : `${lesson.lessonId} references [[check-${ref.key}]] but its .checks.yml does not list "${ref.key}"`,
114
+ file: lesson.file,
115
+ lessonId: lesson.lessonId,
116
+ key: ref.key,
117
+ });
118
+ }
119
+ }
120
+
121
+ diagnostics.sort(
122
+ (a, b) =>
123
+ a.code.localeCompare(b.code) ||
124
+ (a.lessonId ?? '').localeCompare(b.lessonId ?? '') ||
125
+ (a.key ?? '').localeCompare(b.key ?? '') ||
126
+ a.message.localeCompare(b.message),
127
+ );
128
+
129
+ return { labels, numbersBySlug, diagnostics };
130
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Stable identity and numbering helpers for lesson knowledge checks.
3
+ *
4
+ * A course owns check titles and questions in `content/assessments/*.json`,
5
+ * then places their ids in a lesson's colocated `.checks.yml`. That ordered
6
+ * list is the check sequence: numbers run independently across each part,
7
+ * exactly like the equation/table/figure/diagram streams.
8
+ */
9
+
10
+ /** Assessment ids use the content key grammar, including optional dots. */
11
+ export const KNOWLEDGE_CHECK_KEY_PATTERN =
12
+ /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
13
+
14
+ /** `[[check-id]]`, or `[[lesson/slug#check-id]]` across pages. */
15
+ export function parseKnowledgeCheckRef(raw) {
16
+ const match =
17
+ /^(?:([a-z0-9]+(?:-[a-z0-9]+)*(?:\/[a-z0-9]+(?:-[a-z0-9]+)*)*)#)?check-([a-z0-9]+(?:[.-][a-z0-9]+)*)$/.exec(
18
+ String(raw).trim(),
19
+ );
20
+ if (!match) return undefined;
21
+ return match[1] ? { lesson: match[1], key: match[2] } : { key: match[2] };
22
+ }
23
+
24
+ /** The stable DOM fragment for a knowledge check. */
25
+ export function knowledgeCheckAnchorId(key) {
26
+ return `check-${key}`;
27
+ }
28
+
29
+ /**
30
+ * Number an ordered assessment-id list. With manifest placement, the number
31
+ * is `part.offset + index`; without it, the list remains usefully page-local.
32
+ */
33
+ export function collectKnowledgeChecks(assessmentIds, numbering) {
34
+ const prefix = Number.isInteger(numbering?.prefix) && numbering.prefix > 0
35
+ ? numbering.prefix
36
+ : undefined;
37
+ const offset = Number.isInteger(numbering?.offset) && numbering.offset >= 0
38
+ ? numbering.offset
39
+ : 0;
40
+ return assessmentIds.map((key, index) => ({
41
+ key,
42
+ number: prefix == null ? String(index + 1) : `${prefix}.${offset + index + 1}`,
43
+ indexInLesson: index + 1,
44
+ }));
45
+ }
@@ -1,5 +1,9 @@
1
1
  import { loadManifest } from '../compiler/manifest';
2
2
  import type { LabMathScopeDefinition } from './render-lab-math';
3
+ import {
4
+ registerPageNotationDependencies,
5
+ routeIdFromLocals,
6
+ } from './page-notation';
3
7
 
4
8
  interface LessonNotationFrontmatter {
5
9
  readonly local?: ReadonlyArray<{
@@ -20,10 +24,16 @@ const EXPLAIN_KEY = /\\explain\s*\{([a-z0-9]+(?:[.-][a-z0-9]+)*)\}/g;
20
24
  *
21
25
  * Shared definitions are read from the one compiler manifest (Phase D5); this
22
26
  * module never re-reads the content tree.
27
+ *
28
+ * A nested component whose `body` contains notation absent from lesson MDX
29
+ * passes `{ locals: Astro.locals, context: 'Component formula name' }`. That
30
+ * request-local declaration lets `LessonFooter` ship just those extra cards and
31
+ * gives an unknown `\explain` key an actionable render-time diagnostic.
23
32
  */
24
33
  export async function labMathScope(
25
34
  notation: LessonNotationFrontmatter | undefined,
26
35
  body = '',
36
+ component?: { readonly locals: object; readonly context: string },
27
37
  ): Promise<LabMathScopeDefinition[]> {
28
38
  const local = notation?.local ?? [];
29
39
  const scope: LabMathScopeDefinition[] = local.map((definition) => ({
@@ -32,8 +42,12 @@ export async function labMathScope(
32
42
  }));
33
43
 
34
44
  const referenced = new Set<string>();
45
+ const explicitlyExplained = new Set<string>();
35
46
  for (const pattern of [TERM_KEY, EXPLAIN_KEY]) {
36
- for (const match of body.matchAll(pattern)) referenced.add(match[1]);
47
+ for (const match of body.matchAll(pattern)) {
48
+ referenced.add(match[1]);
49
+ if (pattern === EXPLAIN_KEY) explicitlyExplained.add(match[1]);
50
+ }
37
51
  }
38
52
 
39
53
  const localKeys = new Set(local.map((definition) => definition.key));
@@ -44,10 +58,38 @@ export async function labMathScope(
44
58
  .map((definition) => [definition.key, definition]),
45
59
  );
46
60
 
61
+ const resolvedKeys = new Set<string>();
47
62
  for (const key of referenced) {
48
- if (localKeys.has(key)) continue;
63
+ if (localKeys.has(key)) {
64
+ resolvedKeys.add(key);
65
+ continue;
66
+ }
49
67
  const shared = sharedByKey.get(key);
50
- if (shared) scope.push({ key: shared.key, notation: shared.notation });
68
+ if (shared) {
69
+ scope.push({ key: shared.key, notation: shared.notation });
70
+ resolvedKeys.add(key);
71
+ continue;
72
+ }
73
+ // `[[...]]` also carries equation/table/figure/diagram references. The
74
+ // manifest validates those namespaces; an explicit \explain is always a
75
+ // notation dependency and can be diagnosed here without ambiguity.
76
+ if (explicitlyExplained.has(key)) {
77
+ throw new Error(
78
+ `Notation dependency error${
79
+ component
80
+ ? ` in lesson "${routeIdFromLocals(component.locals) ?? 'unknown'}" (${component.context})`
81
+ : ''
82
+ }: unknown key "${key}". Define it in notation.local or the shared notation registry.`,
83
+ );
84
+ }
85
+ }
86
+
87
+ if (component) {
88
+ registerPageNotationDependencies(
89
+ component.locals,
90
+ resolvedKeys,
91
+ component.context,
92
+ );
51
93
  }
52
94
 
53
95
  return scope;