@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
@@ -0,0 +1,96 @@
1
+ import {
2
+ qti3TrustedXmlFragment,
3
+ type Qti3AssociateChoice,
4
+ type Qti3AuthoringChoice,
5
+ type Qti3GapMatchChoice,
6
+ type Qti3GraphicGapChoice,
7
+ type Qti3MatchChoice,
8
+ } from "@longsightgroup/qti3-writer";
9
+ import { normalizeIdentifier } from "./text.js";
10
+ import {
11
+ attr,
12
+ findAllDescendantsByLocalName,
13
+ findDescendantByLocalName,
14
+ localName,
15
+ serializeChildren,
16
+ textOf,
17
+ toNumber,
18
+ type XmlElement,
19
+ } from "./xml.js";
20
+
21
+ export function simpleChoices(root: XmlElement): Qti3AuthoringChoice[] {
22
+ return findAllDescendantsByLocalName(root, "simplechoice").map((choice, index) => ({
23
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `CHOICE_${index + 1}`),
24
+ contentHtml: trusted(serializeChildren(choice)),
25
+ text: textOf(choice) || undefined,
26
+ fixed: attr(choice, "fixed") === "true",
27
+ }));
28
+ }
29
+
30
+ export function associableChoices(
31
+ root: XmlElement | undefined,
32
+ prefix: string,
33
+ ): (Qti3MatchChoice | Qti3AssociateChoice)[] {
34
+ if (!root) return [];
35
+ return findAllDescendantsByLocalName(root, "simpleassociablechoice").map((choice, index) => ({
36
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `${prefix}_${index + 1}`),
37
+ contentHtml: trusted(serializeChildren(choice)),
38
+ text: textOf(choice) || undefined,
39
+ fixed: attr(choice, "fixed") === "true",
40
+ matchMax: toNumber(attr(choice, "matchMax")),
41
+ }));
42
+ }
43
+
44
+ export function gapChoice(choice: XmlElement, index: number): Qti3GapMatchChoice {
45
+ if (localName(choice) === "gapimg") {
46
+ const object = findDescendantByLocalName(choice, "object");
47
+ return {
48
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `G${index + 1}`),
49
+ kind: "image",
50
+ object: {
51
+ data: attr(object, "data") ?? "",
52
+ alt: attr(object, "alt") ?? attr(object, "label") ?? "Image",
53
+ type: attr(object, "type") ?? undefined,
54
+ },
55
+ matchMax: toNumber(attr(choice, "matchMax")),
56
+ fixed: attr(choice, "fixed") === "true",
57
+ };
58
+ }
59
+ return {
60
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `G${index + 1}`),
61
+ kind: "text",
62
+ contentHtml: trusted(serializeChildren(choice)),
63
+ text: textOf(choice) || undefined,
64
+ matchMax: toNumber(attr(choice, "matchMax")),
65
+ fixed: attr(choice, "fixed") === "true",
66
+ };
67
+ }
68
+
69
+ export function graphicGapChoice(choice: XmlElement, index: number): Qti3GraphicGapChoice {
70
+ if (localName(choice) === "gapimg") {
71
+ const object = findDescendantByLocalName(choice, "object");
72
+ return {
73
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `G${index + 1}`),
74
+ kind: "image",
75
+ object: {
76
+ data: attr(object, "data") ?? "",
77
+ alt: attr(object, "alt") ?? attr(object, "label") ?? "Image",
78
+ type: attr(object, "type") ?? undefined,
79
+ },
80
+ matchMax: toNumber(attr(choice, "matchMax")),
81
+ fixed: attr(choice, "fixed") === "true",
82
+ };
83
+ }
84
+ return {
85
+ identifier: normalizeIdentifier(attr(choice, "identifier"), `G${index + 1}`),
86
+ kind: "text",
87
+ contentHtml: trusted(serializeChildren(choice)),
88
+ text: textOf(choice) || undefined,
89
+ matchMax: toNumber(attr(choice, "matchMax")),
90
+ fixed: attr(choice, "fixed") === "true",
91
+ };
92
+ }
93
+
94
+ function trusted(html: string): ReturnType<typeof qti3TrustedXmlFragment> {
95
+ return qti3TrustedXmlFragment(html.trim() || "<p></p>");
96
+ }
@@ -0,0 +1,32 @@
1
+ import { attr, toNumber, type XmlElement } from "./xml.js";
2
+
3
+ export function graphicObject(object: XmlElement | null, coords: readonly string[]) {
4
+ const dimensions = inferImageDimensions(coords);
5
+ return {
6
+ data: attr(object, "data") ?? "",
7
+ alt: attr(object, "alt") ?? attr(object, "label") ?? "Image",
8
+ type: attr(object, "type") ?? undefined,
9
+ width: toNumber(attr(object, "width")) ?? dimensions.width,
10
+ height: toNumber(attr(object, "height")) ?? dimensions.height,
11
+ };
12
+ }
13
+
14
+ export function hotspotShape(value: string | null): "circle" | "rect" | "poly" {
15
+ return value === "circle" || value === "poly" ? value : "rect";
16
+ }
17
+
18
+ function inferImageDimensions(coords: readonly string[]): { width: number; height: number } {
19
+ let maxX = 1;
20
+ let maxY = 1;
21
+ for (const entry of coords) {
22
+ const coordinateValues = entry
23
+ .split(/[\s,]+/)
24
+ .map(Number)
25
+ .filter(Number.isFinite);
26
+ for (let index = 0; index < coordinateValues.length; index += 2) {
27
+ maxX = Math.max(maxX, coordinateValues[index] ?? 1);
28
+ maxY = Math.max(maxY, coordinateValues[index + 1] ?? 1);
29
+ }
30
+ }
31
+ return { width: Math.ceil(maxX), height: Math.ceil(maxY) };
32
+ }