@longsightgroup/qti3-migrator 0.9.5

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 (68) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +13 -0
  3. package/dist/diagnostics.d.ts +7 -0
  4. package/dist/diagnostics.d.ts.map +1 -0
  5. package/dist/diagnostics.js +7 -0
  6. package/dist/diagnostics.js.map +1 -0
  7. package/dist/index.d.ts +7 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +262 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/options.d.ts +3 -0
  12. package/dist/options.d.ts.map +1 -0
  13. package/dist/options.js +7 -0
  14. package/dist/options.js.map +1 -0
  15. package/dist/qti12-item.d.ts +7 -0
  16. package/dist/qti12-item.d.ts.map +1 -0
  17. package/dist/qti12-item.js +393 -0
  18. package/dist/qti12-item.js.map +1 -0
  19. package/dist/qti2-body.d.ts +11 -0
  20. package/dist/qti2-body.d.ts.map +1 -0
  21. package/dist/qti2-body.js +90 -0
  22. package/dist/qti2-body.js.map +1 -0
  23. package/dist/qti2-choices.d.ts +7 -0
  24. package/dist/qti2-choices.d.ts.map +1 -0
  25. package/dist/qti2-choices.js +74 -0
  26. package/dist/qti2-choices.js.map +1 -0
  27. package/dist/qti2-graphic.d.ts +10 -0
  28. package/dist/qti2-graphic.d.ts.map +1 -0
  29. package/dist/qti2-graphic.js +30 -0
  30. package/dist/qti2-graphic.js.map +1 -0
  31. package/dist/qti2-item.d.ts +8 -0
  32. package/dist/qti2-item.d.ts.map +1 -0
  33. package/dist/qti2-item.js +494 -0
  34. package/dist/qti2-item.js.map +1 -0
  35. package/dist/qti2-response.d.ts +9 -0
  36. package/dist/qti2-response.d.ts.map +1 -0
  37. package/dist/qti2-response.js +32 -0
  38. package/dist/qti2-response.js.map +1 -0
  39. package/dist/source.d.ts +30 -0
  40. package/dist/source.d.ts.map +1 -0
  41. package/dist/source.js +197 -0
  42. package/dist/source.js.map +1 -0
  43. package/dist/text.d.ts +5 -0
  44. package/dist/text.d.ts.map +1 -0
  45. package/dist/text.js +19 -0
  46. package/dist/text.js.map +1 -0
  47. package/dist/types.d.ts +61 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +2 -0
  50. package/dist/types.js.map +1 -0
  51. package/dist/xml.d.ts +16 -0
  52. package/dist/xml.d.ts.map +1 -0
  53. package/dist/xml.js +101 -0
  54. package/dist/xml.js.map +1 -0
  55. package/package.json +56 -0
  56. package/src/diagnostics.ts +21 -0
  57. package/src/index.ts +393 -0
  58. package/src/options.ts +8 -0
  59. package/src/qti12-item.ts +507 -0
  60. package/src/qti2-body.ts +138 -0
  61. package/src/qti2-choices.ts +96 -0
  62. package/src/qti2-graphic.ts +32 -0
  63. package/src/qti2-item.ts +650 -0
  64. package/src/qti2-response.ts +41 -0
  65. package/src/source.ts +251 -0
  66. package/src/text.ts +21 -0
  67. package/src/types.ts +73 -0
  68. package/src/xml.ts +119 -0
package/src/index.ts ADDED
@@ -0,0 +1,393 @@
1
+ import { parseQtiXml, validateAssessmentItem, type QtiDiagnostic } from "@longsightgroup/qti3-core";
2
+ import {
3
+ qti3TrustedXmlFragment,
4
+ writeQti3AssessmentItemResult,
5
+ type Qti3AuthoringItem,
6
+ } from "@longsightgroup/qti3-writer";
7
+ import { diagnostic, hasErrors } from "./diagnostics.js";
8
+ import { resolveOptions } from "./options.js";
9
+ import { migrateQti12Xml } from "./qti12-item.js";
10
+ import { itemTitleFromXml, migrateQti2ItemXml } from "./qti2-item.js";
11
+ import {
12
+ detectMigrationSource,
13
+ detectQtiMigrationSource,
14
+ parseLegacyManifest,
15
+ readMigrationSource,
16
+ type MigrationEntry,
17
+ type MigrationSource,
18
+ } from "./source.js";
19
+ import type {
20
+ QtiMigrationAsset,
21
+ QtiMigrationDetectionResult,
22
+ QtiMigrationDiagnostic,
23
+ QtiMigrationItemResult,
24
+ QtiMigrationOptions,
25
+ QtiMigrationPart,
26
+ QtiMigrationResult,
27
+ QtiMigrationSourceFormat,
28
+ QtiMigrationSourceInput,
29
+ } from "./types.js";
30
+
31
+ export { detectQtiMigrationSource };
32
+ export type {
33
+ QtiMigrationAsset,
34
+ QtiMigrationDetectionResult,
35
+ QtiMigrationDiagnostic,
36
+ QtiMigrationDiagnosticSeverity,
37
+ QtiMigrationItemResult,
38
+ QtiMigrationOptions,
39
+ QtiMigrationPart,
40
+ QtiMigrationRepairPolicy,
41
+ QtiMigrationResult,
42
+ QtiMigrationSourceFormat,
43
+ QtiMigrationSourceInput,
44
+ QtiMigrationUnsupportedPolicy,
45
+ } from "./types.js";
46
+
47
+ export async function migrateQtiToQti3(
48
+ input: QtiMigrationSourceInput,
49
+ options: QtiMigrationOptions = {},
50
+ ): Promise<QtiMigrationResult> {
51
+ const resolvedOptions = resolveOptions(options);
52
+ const source = readMigrationSource(input);
53
+ const detection = detectMigrationSource(source);
54
+ if (!detection.supported || !detection.sourceFormat) {
55
+ return {
56
+ title: input.filename ?? "Imported QTI",
57
+ sourceFormat: detection.sourceFormat,
58
+ parts: [],
59
+ items: [],
60
+ assets: [],
61
+ diagnostics: [
62
+ diagnostic(
63
+ "source_unsupported",
64
+ "error",
65
+ `Unsupported QTI migration source: ${detection.reason}.`,
66
+ {
67
+ sourceFormat: detection.sourceFormat,
68
+ },
69
+ ),
70
+ ],
71
+ };
72
+ }
73
+
74
+ const migrated = source.isPackage
75
+ ? migratePackageSource(source, detection, resolvedOptions)
76
+ : migrateItemSource(source, detection, resolvedOptions);
77
+ return Promise.resolve(migrated);
78
+ }
79
+
80
+ export function migrateQtiItemToQti3(
81
+ input: QtiMigrationSourceInput,
82
+ options: QtiMigrationOptions = {},
83
+ ): QtiMigrationItemResult {
84
+ const resolvedOptions = resolveOptions(options);
85
+ const source = readMigrationSource(input);
86
+ const detection = detectMigrationSource(source);
87
+ if (!detection.sourceFormat || !source.xml) {
88
+ return {
89
+ identifier: "ITEM",
90
+ title: input.filename ?? "Imported Item",
91
+ href: "ITEM.xml",
92
+ diagnostics: [
93
+ diagnostic(
94
+ "item_source_invalid",
95
+ "error",
96
+ "Input is not a supported QTI item XML source.",
97
+ {
98
+ sourceFormat: detection.sourceFormat,
99
+ },
100
+ ),
101
+ ],
102
+ };
103
+ }
104
+ const results = migrateItemXml(
105
+ source.xml,
106
+ input.filename ?? "item.xml",
107
+ detection.sourceFormat,
108
+ resolvedOptions,
109
+ );
110
+ const result = results[0];
111
+ if (!result) {
112
+ return {
113
+ identifier: "ITEM",
114
+ title: input.filename ?? "Imported Item",
115
+ href: "ITEM.xml",
116
+ diagnostics: [
117
+ diagnostic(
118
+ "item_migration_empty",
119
+ "error",
120
+ "Input did not produce a migratable QTI item.",
121
+ {
122
+ sourceFormat: detection.sourceFormat,
123
+ },
124
+ ),
125
+ ],
126
+ };
127
+ }
128
+ return finalizeItemResult(result, input.filename ?? `${result.identifier}.xml`, resolvedOptions);
129
+ }
130
+
131
+ function migratePackageSource(
132
+ source: MigrationSource,
133
+ detection: QtiMigrationDetectionResult,
134
+ options: ReturnType<typeof resolveOptions>,
135
+ ): QtiMigrationResult {
136
+ const sourceFormat = detection.sourceFormat!;
137
+ const manifestEntry = source.entries.find(
138
+ (entry) => entry.path.toLowerCase() === "imsmanifest.xml",
139
+ );
140
+ const diagnostics: QtiMigrationDiagnostic[] = [];
141
+ if (!manifestEntry?.text) {
142
+ diagnostics.push(
143
+ diagnostic("manifest_missing", "error", "QTI package is missing imsmanifest.xml.", {
144
+ sourceFormat,
145
+ path: "imsmanifest.xml",
146
+ }),
147
+ );
148
+ return {
149
+ title: source.filename ?? "Imported QTI Package",
150
+ sourceFormat,
151
+ parts: [],
152
+ items: [],
153
+ assets: [],
154
+ diagnostics,
155
+ };
156
+ }
157
+
158
+ const manifest = parseLegacyManifest(manifestEntry.text);
159
+ diagnostics.push(...manifest.diagnostics.map((entry) => ({ ...entry, sourceFormat })));
160
+ if (manifest.testHrefs.length) {
161
+ diagnostics.push(
162
+ diagnostic(
163
+ "assessment_test_structure_not_migrated",
164
+ "warning",
165
+ "QTI assessment-test structure is not migrated yet; items are returned in a flat review part.",
166
+ { sourceFormat, path: "imsmanifest.xml" },
167
+ ),
168
+ );
169
+ }
170
+ const entriesByPath = new Map(source.entries.map((entry) => [entry.path, entry]));
171
+ const itemHrefs = manifest.itemHrefs.length
172
+ ? manifest.itemHrefs
173
+ : source.entries
174
+ .filter(
175
+ (entry) => entry.path.toLowerCase().endsWith(".xml") && entry.path !== "imsmanifest.xml",
176
+ )
177
+ .map((entry) => entry.path);
178
+ const items: QtiMigrationItemResult[] = [];
179
+ for (const href of itemHrefs) {
180
+ const entry = entriesByPath.get(href) ?? entriesByPath.get(href.replace(/^\.\//, ""));
181
+ if (!entry?.text) {
182
+ items.push({
183
+ identifier: pathIdentifier(href),
184
+ title: href,
185
+ href,
186
+ diagnostics: [
187
+ diagnostic("package_item_missing", "error", `Package item ${href} was not found.`, {
188
+ path: href,
189
+ sourceFormat,
190
+ }),
191
+ ],
192
+ });
193
+ continue;
194
+ }
195
+ const migratedItems = migrateItemXml(entry.text, href, sourceFormat, options);
196
+ for (const item of migratedItems) {
197
+ items.push(finalizeItemResult(item, `${item.identifier}.xml`, options));
198
+ }
199
+ }
200
+ return {
201
+ title: manifest.title,
202
+ sourceFormat,
203
+ parts: [partFromItems("PART_1", manifest.title, items)],
204
+ items,
205
+ assets: packageAssets(source.entries),
206
+ diagnostics,
207
+ };
208
+ }
209
+
210
+ function migrateItemSource(
211
+ source: MigrationSource,
212
+ detection: QtiMigrationDetectionResult,
213
+ options: ReturnType<typeof resolveOptions>,
214
+ ): QtiMigrationResult {
215
+ const sourceFormat = detection.sourceFormat!;
216
+ const xml = source.xml ?? "";
217
+ const migratedItems = migrateItemXml(
218
+ xml,
219
+ source.filename ?? "item.xml",
220
+ sourceFormat,
221
+ options,
222
+ ).map((item) => finalizeItemResult(item, `${item.identifier}.xml`, options));
223
+ return {
224
+ title: migratedItems[0]?.title ?? source.filename ?? "Imported QTI Item",
225
+ sourceFormat,
226
+ parts: [partFromItems("PART_1", "Imported Items", migratedItems)],
227
+ items: migratedItems,
228
+ assets: [],
229
+ diagnostics: [],
230
+ };
231
+ }
232
+
233
+ function migrateItemXml(
234
+ xml: string,
235
+ path: string,
236
+ sourceFormat: QtiMigrationSourceFormat,
237
+ options: ReturnType<typeof resolveOptions>,
238
+ ): readonly PendingMigrationItem[] {
239
+ if (sourceFormat === "qti12") {
240
+ return migrateQti12Xml(xml, path, options).map((result, index) => ({
241
+ identifier: result.authoringItem?.identifier ?? `ITEM_${index + 1}`,
242
+ title: result.authoringItem?.title ?? itemTitleFromXmlSafe(xml),
243
+ authoringItem: result.authoringItem,
244
+ diagnostics: result.diagnostics,
245
+ }));
246
+ }
247
+ const result = migrateQti2ItemXml(xml, path, sourceFormat, options);
248
+ return [
249
+ {
250
+ identifier: result.authoringItem?.identifier ?? pathIdentifier(path),
251
+ title: result.authoringItem?.title ?? itemTitleFromXmlSafe(xml),
252
+ authoringItem: result.authoringItem,
253
+ diagnostics: result.diagnostics,
254
+ },
255
+ ];
256
+ }
257
+
258
+ interface PendingMigrationItem {
259
+ readonly identifier: string;
260
+ readonly title: string;
261
+ readonly authoringItem?: Qti3AuthoringItem | undefined;
262
+ readonly diagnostics: readonly QtiMigrationDiagnostic[];
263
+ }
264
+
265
+ function finalizeItemResult(
266
+ pending: PendingMigrationItem,
267
+ href: string,
268
+ options: ReturnType<typeof resolveOptions>,
269
+ ): QtiMigrationItemResult {
270
+ const diagnostics = [...pending.diagnostics];
271
+ if (pending.authoringItem && !hasErrors(diagnostics)) {
272
+ const writer = writeQti3AssessmentItemResult(pending.authoringItem);
273
+ if (writer.ok) {
274
+ diagnostics.push(...validateWrittenXml(writer.xml));
275
+ return {
276
+ identifier: pending.identifier,
277
+ title: pending.title,
278
+ href,
279
+ authoringItem: pending.authoringItem,
280
+ xml: writer.xml,
281
+ diagnostics,
282
+ };
283
+ }
284
+ diagnostics.push({
285
+ code: "writer_diagnostics",
286
+ severity: "error",
287
+ message: "QTI 3 writer rejected migrated authoring item.",
288
+ writerDiagnostics: writer.diagnostics,
289
+ });
290
+ }
291
+
292
+ if (options.unsupportedPolicy === "stub") {
293
+ const stub = stubItem(pending.identifier, pending.title);
294
+ const writer = writeQti3AssessmentItemResult(stub);
295
+ return {
296
+ identifier: pending.identifier,
297
+ title: pending.title,
298
+ href,
299
+ authoringItem: stub,
300
+ xml: writer.ok ? writer.xml : undefined,
301
+ diagnostics: [
302
+ ...diagnostics,
303
+ diagnostic(
304
+ "unsupported_item_stubbed",
305
+ "warning",
306
+ "Unsupported item migrated as an extended-text review stub.",
307
+ ),
308
+ ],
309
+ };
310
+ }
311
+
312
+ return {
313
+ identifier: pending.identifier,
314
+ title: pending.title,
315
+ href,
316
+ authoringItem: options.unsupportedPolicy === "skip" ? undefined : pending.authoringItem,
317
+ diagnostics,
318
+ };
319
+ }
320
+
321
+ function validateWrittenXml(xml: string): QtiMigrationDiagnostic[] {
322
+ const parsed = parseQtiXml(xml);
323
+ const diagnostics: QtiMigrationDiagnostic[] = [];
324
+ if (!parsed.ok || !parsed.document) {
325
+ diagnostics.push(...coreDiagnostics("core_parse", parsed.diagnostics));
326
+ return diagnostics;
327
+ }
328
+ const validation = validateAssessmentItem(parsed.document);
329
+ if (!validation.ok) diagnostics.push(...coreDiagnostics("core_validate", validation.diagnostics));
330
+ return diagnostics;
331
+ }
332
+
333
+ function coreDiagnostics(
334
+ prefix: string,
335
+ diagnostics: readonly QtiDiagnostic[],
336
+ ): QtiMigrationDiagnostic[] {
337
+ return diagnostics.map((entry) =>
338
+ diagnostic(
339
+ `${prefix}.${entry.code}`,
340
+ entry.severity === "error" ? "error" : "warning",
341
+ entry.message,
342
+ ),
343
+ );
344
+ }
345
+
346
+ function stubItem(identifier: string, title: string): Qti3AuthoringItem {
347
+ return {
348
+ interactionType: "extendedText",
349
+ identifier,
350
+ title,
351
+ bodyHtml: qti3TrustedXmlFragment("<p>This item requires manual migration review.</p>"),
352
+ responseIdentifier: "RESPONSE",
353
+ responseBaseType: "string",
354
+ responseCardinality: "single",
355
+ expectedLines: 6,
356
+ };
357
+ }
358
+
359
+ function packageAssets(entries: readonly MigrationEntry[]): QtiMigrationAsset[] {
360
+ return entries
361
+ .filter((entry) => !entry.path.toLowerCase().endsWith(".xml"))
362
+ .map((entry) => ({ path: entry.path, data: entry.bytes, mediaType: mediaType(entry.path) }));
363
+ }
364
+
365
+ function partFromItems(
366
+ identifier: string,
367
+ title: string,
368
+ items: readonly QtiMigrationItemResult[],
369
+ ): QtiMigrationPart {
370
+ return { identifier, title, itemHrefs: items.map((item) => item.href) };
371
+ }
372
+
373
+ function pathIdentifier(path: string): string {
374
+ return path.replace(/\.[^.]+$/, "").replace(/[^A-Za-z0-9_]/g, "_") || "ITEM";
375
+ }
376
+
377
+ function itemTitleFromXmlSafe(xml: string): string {
378
+ try {
379
+ return itemTitleFromXml(xml);
380
+ } catch {
381
+ return "Imported Item";
382
+ }
383
+ }
384
+
385
+ function mediaType(path: string): string | undefined {
386
+ if (/\.png$/i.test(path)) return "image/png";
387
+ if (/\.jpe?g$/i.test(path)) return "image/jpeg";
388
+ if (/\.gif$/i.test(path)) return "image/gif";
389
+ if (/\.svg$/i.test(path)) return "image/svg+xml";
390
+ if (/\.mp3$/i.test(path)) return "audio/mpeg";
391
+ if (/\.mp4$/i.test(path)) return "video/mp4";
392
+ return undefined;
393
+ }
package/src/options.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { QtiMigrationOptions, ResolvedQtiMigrationOptions } from "./types.js";
2
+
3
+ export function resolveOptions(options: QtiMigrationOptions = {}): ResolvedQtiMigrationOptions {
4
+ return {
5
+ repairPolicy: options.repairPolicy ?? "none",
6
+ unsupportedPolicy: options.unsupportedPolicy ?? "diagnostic",
7
+ };
8
+ }