@superbuilders/incept-renderer 0.1.8 → 0.1.12

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.
@@ -1,10 +1,10 @@
1
- import { createHash } from 'crypto';
2
1
  import * as errors from '@superbuilders/errors';
3
- import * as logger2 from '@superbuilders/slog';
4
2
  import { XMLParser } from 'fast-xml-parser';
5
3
  import { z } from 'zod';
4
+ import { createHash } from 'crypto';
5
+ import * as logger2 from '@superbuilders/slog';
6
6
 
7
- // src/actions/internal/display.ts
7
+ // src/parser.ts
8
8
 
9
9
  // src/html/sanitize.ts
10
10
  var DEFAULT_CONFIG = {
@@ -16,6 +16,13 @@ var DEFAULT_CONFIG = {
16
16
  "div",
17
17
  "br",
18
18
  "hr",
19
+ // Headings
20
+ "h1",
21
+ "h2",
22
+ "h3",
23
+ "h4",
24
+ "h5",
25
+ "h6",
19
26
  // Formatting
20
27
  "b",
21
28
  "i",
@@ -65,6 +72,9 @@ var DEFAULT_CONFIG = {
65
72
  "figcaption",
66
73
  "blockquote",
67
74
  "cite",
75
+ // Interactive
76
+ "details",
77
+ "summary",
68
78
  // Links
69
79
  "a",
70
80
  // Forms (for future interactive elements)
@@ -575,6 +585,16 @@ var AssessmentItemSchema = z.object({
575
585
  itemBody: ItemBodySchema,
576
586
  responseProcessing: ResponseProcessingSchema
577
587
  });
588
+ var AssessmentStimulusSchema = z.object({
589
+ /** Unique identifier for the stimulus */
590
+ identifier: z.string().min(1),
591
+ /** Human-readable title */
592
+ title: z.string().default(""),
593
+ /** Language code (e.g., "en", "es") */
594
+ xmlLang: z.string().default("en"),
595
+ /** The HTML content of the stimulus body */
596
+ bodyHtml: z.string()
597
+ });
578
598
 
579
599
  // src/parser.ts
580
600
  function createXmlParser() {
@@ -1200,8 +1220,47 @@ function parseAssessmentItemXml(xml) {
1200
1220
  }
1201
1221
  return validation.data;
1202
1222
  }
1203
-
1204
- // src/actions/internal/display.ts
1223
+ function parseAssessmentStimulusXml(xml) {
1224
+ if (!xml || typeof xml !== "string") {
1225
+ throw errors.new("xml input must be a non-empty string");
1226
+ }
1227
+ const parser = createXmlParser();
1228
+ const parseResult = errors.trySync(() => {
1229
+ return parser.parse(xml, true);
1230
+ });
1231
+ if (parseResult.error) {
1232
+ throw errors.wrap(parseResult.error, "xml parse");
1233
+ }
1234
+ const raw = parseResult.data;
1235
+ if (!Array.isArray(raw)) {
1236
+ throw errors.new("expected xml parser to output an array for preserveOrder");
1237
+ }
1238
+ const normalizedTree = raw.map(normalizeNode).filter((n) => typeof n !== "string");
1239
+ const rootNode = normalizedTree.find(
1240
+ (n) => n.tagName === "qti-assessment-stimulus" || n.tagName.endsWith("assessment-stimulus")
1241
+ );
1242
+ if (!rootNode) {
1243
+ throw errors.new("qti assessment stimulus not found in xml document");
1244
+ }
1245
+ const rootChildren = rootNode.children.filter((c) => typeof c !== "string");
1246
+ const stimulusBodyNode = rootChildren.find((n) => n.tagName === "qti-stimulus-body");
1247
+ if (!stimulusBodyNode) {
1248
+ throw errors.new("qti-stimulus-body not found in stimulus document");
1249
+ }
1250
+ const bodyHtml = getInnerHtml(stimulusBodyNode);
1251
+ const normalizedStimulus = {
1252
+ identifier: coerceString(rootNode.attrs.identifier),
1253
+ title: coerceString(rootNode.attrs.title),
1254
+ xmlLang: coerceString(rootNode.attrs["xml:lang"]) || coerceString(rootNode.attrs["xml-lang"]) || "en",
1255
+ bodyHtml
1256
+ };
1257
+ const validation = AssessmentStimulusSchema.safeParse(normalizedStimulus);
1258
+ if (!validation.success) {
1259
+ const errorDetails = validation.error.issues.map((err) => `${err.path.join(".")}: ${err.message}`).join("; ");
1260
+ throw errors.new(`qti stimulus validation: ${errorDetails}`);
1261
+ }
1262
+ return validation.data;
1263
+ }
1205
1264
  function shuffleArray(items) {
1206
1265
  const arr = items.slice();
1207
1266
  for (let i = arr.length - 1; i > 0; i--) {
@@ -1688,6 +1747,6 @@ async function validateResponsesSecure(qtiXml, responses) {
1688
1747
  return validateResponsesFromXml(qtiXml, responses);
1689
1748
  }
1690
1749
 
1691
- export { buildDisplayModel, buildDisplayModelFromXml, validateResponsesFromXml, validateResponsesSecure };
1750
+ export { buildDisplayModel, buildDisplayModelFromXml, parseAssessmentStimulusXml, validateResponsesFromXml, validateResponsesSecure };
1692
1751
  //# sourceMappingURL=index.js.map
1693
1752
  //# sourceMappingURL=index.js.map