@learnosity/lib-qti-converter 0.12.4 → 0.12.6

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 (30) hide show
  1. package/README.md +28 -3
  2. package/docs/EXAMPLES.md +47 -21
  3. package/docs/FAQ.md +11 -1
  4. package/docs/GETTING_STARTED.md +34 -18
  5. package/package.json +1 -1
  6. package/packages/cli/dist/{batchProcessors-C55IxXri.js → batchProcessors-C8k2v0o_.js} +3 -2
  7. package/packages/cli/dist/batchProcessors-C8k2v0o_.js.map +1 -0
  8. package/packages/cli/dist/batchWorker.js +1 -1
  9. package/packages/cli/dist/{learnosityToQti-B0PoimXr.js → learnosityToQti-CXDJoJpl.js} +6 -5
  10. package/packages/cli/dist/learnosityToQti-CXDJoJpl.js.map +1 -0
  11. package/packages/cli/dist/{learnosityToQtiPackage-Dwgys922.js → learnosityToQtiPackage-7v-s4ybk.js} +10 -6
  12. package/packages/cli/dist/learnosityToQtiPackage-7v-s4ybk.js.map +1 -0
  13. package/packages/cli/dist/main.js +4 -4
  14. package/packages/cli/dist/{pathUtils-CwZnJ55s.js → pathUtils-CgoDQyJk.js} +2 -1
  15. package/packages/cli/dist/pathUtils-CgoDQyJk.js.map +1 -0
  16. package/packages/cli/dist/{qtiPackageToLearnosity-HPtU5x5h.js → qtiPackageToLearnosity-hT4QZ7YX.js} +3 -3
  17. package/packages/cli/dist/{qtiPackageToLearnosity-HPtU5x5h.js.map → qtiPackageToLearnosity-hT4QZ7YX.js.map} +1 -1
  18. package/packages/cli/dist/{qtiToLearnosity-jiFTBiQU.js → qtiToLearnosity-BU7mHhIu.js} +3 -3
  19. package/packages/cli/dist/{qtiToLearnosity-jiFTBiQU.js.map → qtiToLearnosity-BU7mHhIu.js.map} +1 -1
  20. package/packages/cli/dist/{resolveOptions-gH_YAxxc.js → resolveOptions-ehwJSkQy.js} +121 -13
  21. package/packages/cli/dist/resolveOptions-ehwJSkQy.js.map +1 -0
  22. package/packages/core/dist/index.d.ts +30 -0
  23. package/packages/core/dist/index.js +155 -17
  24. package/packages/core/dist/node.d.ts +35 -0
  25. package/packages/core/dist/node.js +349 -22
  26. package/packages/cli/dist/batchProcessors-C55IxXri.js.map +0 -1
  27. package/packages/cli/dist/learnosityToQti-B0PoimXr.js.map +0 -1
  28. package/packages/cli/dist/learnosityToQtiPackage-Dwgys922.js.map +0 -1
  29. package/packages/cli/dist/pathUtils-CwZnJ55s.js.map +0 -1
  30. package/packages/cli/dist/resolveOptions-gH_YAxxc.js.map +0 -1
@@ -21,6 +21,28 @@ interface FileAccess {
21
21
  * LOM XML, or `undefined` if none is found (or without filesystem access).
22
22
  */
23
23
  findAndReadLomFile(itemPath: string): string | undefined;
24
+ /**
25
+ * Resolve an image referenced by an `<object data="...">` / `<img src="...">`
26
+ * tag and read its intrinsic pixel dimensions from the file header.
27
+ *
28
+ * Used when the QTI markup omits `width`/`height` attributes (which is valid
29
+ * QTI) but a downstream Learnosity question needs pixel dimensions (e.g.
30
+ * hotspot coordinate conversion). Returns `undefined` if the file cannot be
31
+ * resolved/read, the format is unsupported, or when running without
32
+ * filesystem access.
33
+ *
34
+ * `packageRoot`, when provided, is the directory that bounds every read: any
35
+ * asset inside the package resolves regardless of directory naming, while
36
+ * paths escaping the package are rejected. When omitted, reads are confined
37
+ * to the source item's own directory.
38
+ *
39
+ * Optional so in-memory/browser callers and existing implementations that
40
+ * predate it remain valid.
41
+ */
42
+ resolveImageDimensions?: (_objectDataPath: string, _sourceFile?: string, _packageRoot?: string) => {
43
+ width: number;
44
+ height: number;
45
+ } | undefined;
24
46
  }
25
47
  //#endregion
26
48
  //#region src/types.d.ts
@@ -166,6 +188,7 @@ declare const DiagnosticCode: {
166
188
  readonly QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS";
167
189
  readonly QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT";
168
190
  readonly QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER";
191
+ readonly QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED";
169
192
  readonly LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN";
170
193
  readonly QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE";
171
194
  readonly QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION";
@@ -2418,6 +2441,12 @@ interface ConvertActivityOptions {
2418
2441
  pretty?: boolean;
2419
2442
  /** QTI version (2.1 or 2.2) */
2420
2443
  version?: QtiVersion;
2444
+ /**
2445
+ * Base URL/prefix that replaces the asset reference prefix up to the
2446
+ * filename for images/audio/video (default: `./assets`). For example
2447
+ * `https://cdn.example.com/media` produces `https://cdn.example.com/media/<filename>`.
2448
+ */
2449
+ customAssetUrl?: string;
2421
2450
  }
2422
2451
  /**
2423
2452
  * Convert a Learnosity Activity to QTI 2.2/2.1 assessmentTest + items
@@ -2535,6 +2564,7 @@ interface ConvertOptions {
2535
2564
  itemReferenceSource?: ItemReferenceSource;
2536
2565
  truncateTags?: boolean;
2537
2566
  fileAccess?: FileAccess;
2567
+ packageRoot?: string;
2538
2568
  }
2539
2569
  /**
2540
2570
  * Convert QTI assessmentItem XML to Learnosity Item JSON
@@ -173,6 +173,7 @@ const DiagnosticCode = {
173
173
  QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS",
174
174
  QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT",
175
175
  QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER",
176
+ QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED",
176
177
  LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN",
177
178
  QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE",
178
179
  QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION",
@@ -20443,7 +20444,7 @@ function buildAssessmentItemXml(ast, pretty, version) {
20443
20444
  itemBody: "",
20444
20445
  modalFeedback: []
20445
20446
  };
20446
- sections.stylesheets.push(`${indent}<stylesheet href="assets/css/lrn-normalize.css" type="text/css"/>${nl}`);
20447
+ sections.stylesheets.push(`${indent}<stylesheet href="./assets/css/lrn-normalize.css" type="text/css"/>${nl}`);
20447
20448
  if (ast.layout && ast.interactions) buildMultiWidgetItemSections(ast, diagnostics, sections, indent, indent2, indent3, nl);
20448
20449
  else if (!ast.layout && ast.interactions && ast.interactions.length > 1) buildSequentialInteractionsSections(ast, diagnostics, sections, indent, indent2, indent3, nl);
20449
20450
  else {
@@ -23130,7 +23131,7 @@ const MEDIA_SRC_PATTERN = /(<(?:img|audio|video|source)\s+[^>]*src=["'])([^"']+)
23130
23131
  * "images/photo.png" → "photo.png"
23131
23132
  * "/assets/video.mp4" → "video.mp4"
23132
23133
  */
23133
- function extractFilename(url) {
23134
+ function extractFilename$1(url) {
23134
23135
  const segments = url.split("?")[0].split("#")[0].split("/");
23135
23136
  return segments[segments.length - 1];
23136
23137
  }
@@ -23157,7 +23158,7 @@ function transformUrl(url, organisationId, mediaVault = false, customAssetUrl) {
23157
23158
  if (url.includes("assets.learnosity.com") || url.includes("media-vault.learnosity.com")) return url;
23158
23159
  if (url.match(/^https?:\/\//i)) return url;
23159
23160
  if (url.startsWith("data:")) return url;
23160
- const filename = extractFilename(url);
23161
+ const filename = extractFilename$1(url);
23161
23162
  if (!isSupportedAsset(filename)) return url;
23162
23163
  return buildLearnosityAssetUrl(filename, organisationId, mediaVault, customAssetUrl);
23163
23164
  }
@@ -24902,13 +24903,28 @@ function buildHotspotValidation(correctResponses, choiceIdToIndex) {
24902
24903
  /**
24903
24904
  * Convert hotspotInteraction to Learnosity hotspot
24904
24905
  */
24905
- function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource) {
24906
+ function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
24906
24907
  const parsed = readAssessmentItemHotspot(inputXml);
24907
24908
  const updatedDiagnostics = addFileContextIfProvided(parsed.diagnostics, sourceFile);
24908
24909
  diagnostics.push(...updatedDiagnostics);
24909
24910
  if (hasErrors(diagnostics)) return { diagnostics };
24910
- if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource || !parsed.imageWidth || !parsed.imageHeight) return { diagnostics };
24911
+ if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource) return { diagnostics };
24911
24912
  if (parsed.hotspotChoices.length === 0) return { diagnostics };
24913
+ if (!parsed.imageWidth || !parsed.imageHeight) {
24914
+ const resolved = fileAccess?.resolveImageDimensions?.(parsed.imageSource, sourceFile, packageRoot);
24915
+ if (resolved) {
24916
+ parsed.imageWidth = parsed.imageWidth || resolved.width;
24917
+ parsed.imageHeight = parsed.imageHeight || resolved.height;
24918
+ } else {
24919
+ diagnostics.push({
24920
+ code: DiagnosticCode.LRN_MISSING_REQUIRED_FIELD,
24921
+ level: "error",
24922
+ file: sourceFile ?? "hotspotInteraction",
24923
+ message: `hotspotInteraction image "${parsed.imageSource}" is missing width/height attributes and its dimensions could not be determined from the image file. Hotspot coordinate conversion requires image dimensions.`
24924
+ });
24925
+ return { diagnostics };
24926
+ }
24927
+ }
24912
24928
  const itemRef = generateItemReference(parsed.identifier, itemReferenceSource, sourceFile);
24913
24929
  const questionRef = md5hex(itemRef);
24914
24930
  const stimulus = buildHotspotStimulus(parsed, organisationId, mediaVault, customAssetUrl);
@@ -28013,6 +28029,7 @@ function computeClosingTags(html) {
28013
28029
  */
28014
28030
  function getConverterMap() {
28015
28031
  const wrapSourceFirst = (fn) => (xml, diag, tags, orgId, sf, mv, cau, irs, stimuli) => fn(xml, diag, tags, sf, orgId, mv, cau, irs, stimuli);
28032
+ const hotspotConverter = (xml, diag, tags, orgId, sf, mv, cau, irs, _stimuli, fileAccess, packageRoot) => convertHotspotToLearnosity(xml, diag, tags, orgId, sf, mv, cau, irs, fileAccess, packageRoot);
28016
28033
  return {
28017
28034
  choiceInteraction: wrapSourceFirst(convertMcqToLearnosity),
28018
28035
  textEntryInteraction: wrapSourceFirst(convertTextEntryToLearnosity),
@@ -28021,7 +28038,7 @@ function getConverterMap() {
28021
28038
  gapMatchInteraction: convertGapMatchToLearnosity,
28022
28039
  inlineChoiceInteraction: convertInlineChoiceToLearnosity,
28023
28040
  orderInteraction: convertOrderToLearnosity,
28024
- hotspotInteraction: convertHotspotToLearnosity,
28041
+ hotspotInteraction: hotspotConverter,
28025
28042
  hottextInteraction: convertHottextToLearnosity,
28026
28043
  graphicGapMatchInteraction: convertGraphicGapMatchToLearnosity,
28027
28044
  drawingInteraction: convertDrawingToLearnosity,
@@ -28035,7 +28052,7 @@ const CONVERTER_MAP = getConverterMap();
28035
28052
  * Strategy: Call each individual converter for its interaction type,
28036
28053
  * then combine all questions and widgets into a single composite item
28037
28054
  */
28038
- function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess) {
28055
+ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
28039
28056
  const metadata = extractItemMetadata(inputXml);
28040
28057
  const { segments } = segmentItemBodyByInteractions(inputXml);
28041
28058
  const stimuliByType = /* @__PURE__ */ new Map();
@@ -28252,7 +28269,7 @@ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, or
28252
28269
  continue;
28253
28270
  }
28254
28271
  const converter = CONVERTER_MAP[group.type];
28255
- if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli), subDiagnostics);
28272
+ if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli, fileAccess, packageRoot), subDiagnostics);
28256
28273
  }
28257
28274
  if (allQuestions.length === 0) {
28258
28275
  diagnostics.push({
@@ -28889,7 +28906,7 @@ const SINGLE_INTERACTION_CONVERTERS = /* @__PURE__ */ new Map([
28889
28906
  ["matchInteraction", (xml, diag, lom, opts) => convertMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28890
28907
  ["gapMatchInteraction", (xml, diag, lom, opts) => convertGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28891
28908
  ["orderInteraction", (xml, diag, lom, opts) => convertOrderToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28892
- ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28909
+ ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot)],
28893
28910
  ["hottextInteraction", (xml, diag, lom, opts) => convertHottextToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28894
28911
  ["graphicGapMatchInteraction", (xml, diag, lom, opts) => convertGraphicGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28895
28912
  ["drawingInteraction", (xml, diag, lom, opts) => convertDrawingToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
@@ -29017,7 +29034,7 @@ function convertQtiAssessmentItemToLearnosity(inputXml, opts = {}) {
29017
29034
  const { counts, allInteractions } = countAllInteractions(inputXml);
29018
29035
  const presentTypes = INTERACTION_NAMES.filter((name) => counts[name] > 0);
29019
29036
  if (presentTypes.reduce((sum, name) => sum + counts[name], 0) === 0) return handleNoInteractions(inputXml, diagnostics, allInteractions, lomTags, opts);
29020
- if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess);
29037
+ if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot);
29021
29038
  return dispatchSingleInteractionConverter(presentTypes[0], inputXml, counts[presentTypes[0]], diagnostics, lomTags, opts);
29022
29039
  } catch (error) {
29023
29040
  diagnostics.push({
@@ -29030,6 +29047,97 @@ function convertQtiAssessmentItemToLearnosity(inputXml, opts = {}) {
29030
29047
  }
29031
29048
  }
29032
29049
  //#endregion
29050
+ //#region src/learnosity-mapper/forwardAssetUrlTransform.ts
29051
+ /**
29052
+ * Forward-direction (Learnosity → QTI) asset URL transformation.
29053
+ *
29054
+ * Learnosity item/stimulus content references assets using Learnosity's own
29055
+ * path convention (for example `/vendor/learnosity/itembank/assets/<hash>_<name>.mp3`).
29056
+ * When converting to a QTI Content Package, those references must point at the
29057
+ * assets that ship inside the package.
29058
+ *
29059
+ * This module rewrites the *prefix* of an asset reference while preserving the
29060
+ * final path segment (the filename), mirroring the reverse-direction helper in
29061
+ * `qti-to-learnosity/utils/assetUrlTransform.ts`.
29062
+ *
29063
+ * Rules:
29064
+ * - Default prefix is `./assets/` so a reference resolves to `./assets/<filename>`.
29065
+ * - A caller-supplied `customAssetUrl` replaces the entire prefix up to the
29066
+ * filename, e.g. `https://cdn.example.com/media` → `https://cdn.example.com/media/<filename>`.
29067
+ * - Remote (`http(s)://`), protocol-relative (`//`) and `data:` URIs are left
29068
+ * untouched.
29069
+ * - References that already point at internal package paths (`assets/`,
29070
+ * `./assets/`, `stimuli/`, `items/`, or a relative `../` path) are left
29071
+ * untouched so we never rewrite converter-generated internal references
29072
+ * (e.g. `assets/gapimgs/...`, `assets/css/lrn-normalize.css`).
29073
+ */
29074
+ /** Default output prefix applied when no `customAssetUrl` is provided. */
29075
+ const DEFAULT_FORWARD_ASSET_PREFIX = "./assets";
29076
+ /**
29077
+ * Extract the filename (last path segment) from a URL/path, ignoring any query
29078
+ * string or hash fragment.
29079
+ *
29080
+ * Examples:
29081
+ * "/vendor/learnosity/itembank/assets/hash_name.mp3" → "hash_name.mp3"
29082
+ * "images/photo.png?v=2" → "photo.png"
29083
+ */
29084
+ function extractFilename(url) {
29085
+ const segments = url.split("?")[0].split("#")[0].split("/");
29086
+ return segments[segments.length - 1];
29087
+ }
29088
+ /**
29089
+ * Determine whether a reference is one we should leave untouched (remote/data
29090
+ * URIs and converter-generated internal package paths).
29091
+ */
29092
+ function isNonRewritableReference(url) {
29093
+ const trimmed = url.trim();
29094
+ if (trimmed === "") return true;
29095
+ if (/^https?:\/\//i.test(trimmed) || trimmed.startsWith("//") || trimmed.startsWith("data:")) return true;
29096
+ if (trimmed.startsWith("assets/") || trimmed.startsWith("./assets/") || trimmed.startsWith("stimuli/") || trimmed.startsWith("items/") || trimmed.startsWith("tests/") || trimmed.startsWith("../")) return true;
29097
+ return false;
29098
+ }
29099
+ /**
29100
+ * Build the output base prefix (without a trailing slash).
29101
+ */
29102
+ function resolveBasePrefix(customAssetUrl) {
29103
+ const base = customAssetUrl && customAssetUrl.trim() !== "" ? customAssetUrl.trim() : DEFAULT_FORWARD_ASSET_PREFIX;
29104
+ return base.endsWith("/") ? base.slice(0, -1) : base;
29105
+ }
29106
+ /**
29107
+ * Rewrite a single asset reference to the forward-direction package prefix.
29108
+ *
29109
+ * @param url - The incoming Learnosity asset reference.
29110
+ * @param customAssetUrl - Optional base URL/prefix that replaces everything up
29111
+ * to the filename. Defaults to `./assets`.
29112
+ * @returns The rewritten reference, or the original string when it should not
29113
+ * be rewritten.
29114
+ */
29115
+ function transformForwardAssetUrl(url, customAssetUrl) {
29116
+ if (isNonRewritableReference(url)) return url;
29117
+ const filename = extractFilename(url);
29118
+ if (filename === "") return url;
29119
+ return `${resolveBasePrefix(customAssetUrl)}/${filename}`;
29120
+ }
29121
+ /**
29122
+ * Rewrite every asset reference found in a fragment of QTI XML / XHTML.
29123
+ *
29124
+ * Handles the attributes used by the forward serializers and mappers:
29125
+ * - `src="..."` (img, qh5:source, qh5:track, audio/video source)
29126
+ * - `data="..."` (object elements, e.g. media fallbacks, image backgrounds)
29127
+ * - `href="..."` (download fallback anchors for media)
29128
+ *
29129
+ * Internal references (`stimuli/...`, `assets/...`, the CSS stylesheet, gap
29130
+ * images, etc.) are preserved by {@link transformForwardAssetUrl}.
29131
+ */
29132
+ function transformForwardAssetUrlsInXml(xml, customAssetUrl) {
29133
+ if (!xml) return xml;
29134
+ return xml.replace(/\b(src|data|href)=("|')([^"']*)\2/gi, (match, attr, quote, value) => {
29135
+ const rewritten = transformForwardAssetUrl(value, customAssetUrl);
29136
+ if (rewritten === value) return match;
29137
+ return `${attr}=${quote}${rewritten}${quote}`;
29138
+ });
29139
+ }
29140
+ //#endregion
29033
29141
  //#region src/conversions.ts
29034
29142
  /**
29035
29143
  * Convert QTI XML to Learnosity JSON.
@@ -29096,8 +29204,9 @@ function convertLearnosityToQti(input, opts) {
29096
29204
  output: "",
29097
29205
  diagnostics: allDiagnostics
29098
29206
  });
29207
+ const output = transformForwardAssetUrlsInXml(serializeResult.xml, opts.customAssetUrl);
29099
29208
  return Promise.resolve({
29100
- output: serializeResult.xml,
29209
+ output,
29101
29210
  diagnostics: allDiagnostics
29102
29211
  });
29103
29212
  }
@@ -29763,6 +29872,7 @@ function convertActivity(bundle, activityRef, options = {}) {
29763
29872
  const stimuliByRef = /* @__PURE__ */ new Map();
29764
29873
  const pretty = options.pretty ?? false;
29765
29874
  const version = options.version ?? "2.2";
29875
+ const customAssetUrl = options.customAssetUrl;
29766
29876
  const itemRefs = bundle.activityItemRefs.get(activityRef);
29767
29877
  if (!itemRefs) {
29768
29878
  diagnostics.push({
@@ -29797,7 +29907,7 @@ function convertActivity(bundle, activityRef, options = {}) {
29797
29907
  diagnostics.push(...itemDiagnostics);
29798
29908
  if (parseResult.diagnostics.some((d) => d.code === DiagnosticCode.LRN_UNSUPPORTED_QUESTION_TYPE)) continue;
29799
29909
  if (!parseResult.model) continue;
29800
- collectStimuliFromFeatures(parseResult.model.features, stimuliByRef, stimuli, diagnostics);
29910
+ collectStimuliFromFeatures(parseResult.model.features, stimuliByRef, stimuli, diagnostics, customAssetUrl);
29801
29911
  const mapResult = mapLearnosityToAst(parseResult.model);
29802
29912
  const mapDiagnostics = mapResult.diagnostics.map((d) => ({
29803
29913
  ...d,
@@ -29816,7 +29926,7 @@ function convertActivity(bundle, activityRef, options = {}) {
29816
29926
  diagnostics.push(...serializeDiagnostics);
29817
29927
  if (serializeResult.xml) items.push({
29818
29928
  reference: itemRef,
29819
- xml: serializeResult.xml,
29929
+ xml: transformForwardAssetUrlsInXml(serializeResult.xml, customAssetUrl),
29820
29930
  ast: mapResult.ast,
29821
29931
  diagnostics: [
29822
29932
  ...parseResult.diagnostics,
@@ -29881,7 +29991,7 @@ function convertActivity(bundle, activityRef, options = {}) {
29881
29991
  /**
29882
29992
  * Collect sharedpassage features and convert them to stimulus entries (deduplicated)
29883
29993
  */
29884
- function collectStimuliFromFeatures(features, stimuliByRef, stimuli, diagnostics) {
29994
+ function collectStimuliFromFeatures(features, stimuliByRef, stimuli, diagnostics, customAssetUrl) {
29885
29995
  for (const feature of features) {
29886
29996
  if (feature.type !== "sharedpassage" || !feature.data || typeof feature.data !== "object" || !("type" in feature.data)) continue;
29887
29997
  if (stimuliByRef.has(feature.reference)) continue;
@@ -29889,7 +29999,7 @@ function collectStimuliFromFeatures(features, stimuliByRef, stimuli, diagnostics
29889
29999
  if (stimulusAst) {
29890
30000
  const stimulusEntry = {
29891
30001
  reference: feature.reference,
29892
- xml: serializeAssessmentStimulusAsHtml(stimulusAst)
30002
+ xml: transformForwardAssetUrlsInXml(serializeAssessmentStimulusAsHtml(stimulusAst), customAssetUrl)
29893
30003
  };
29894
30004
  stimuliByRef.set(feature.reference, stimulusEntry);
29895
30005
  stimuli.push(stimulusEntry);
@@ -30763,6 +30873,30 @@ function posixJoin(dir, rel) {
30763
30873
  return `${dir.replace(/\/+$/, "")}/${rel.replace(/^\.?\//, "")}`;
30764
30874
  }
30765
30875
  /**
30876
+ * Guarantee that a failed item conversion is represented by at least one
30877
+ * error-level diagnostic keyed to the item's href.
30878
+ *
30879
+ * Some converters can return without an item AND without pushing an error
30880
+ * (silent early-returns), which previously left the diagnostics report showing a
30881
+ * non-zero failed count with an empty errors array. When the per-item result
30882
+ * carried no error, this appends a synthetic {@link DiagnosticCode.QTI_ITEM_CONVERSION_FAILED}
30883
+ * so the failed count and the errors list can never diverge.
30884
+ *
30885
+ * @param itemDiagnostics - diagnostics produced for the single failed item
30886
+ * @param href - the item's manifest href (used as the diagnostic `file`)
30887
+ * @returns the diagnostics to append (either empty, when a real error already
30888
+ * exists, or a one-element array with the synthetic fallback)
30889
+ */
30890
+ function ensureFailedItemDiagnostic(itemDiagnostics, href) {
30891
+ if (itemDiagnostics.some((d) => d.level === "error")) return [];
30892
+ return [{
30893
+ code: DiagnosticCode.QTI_ITEM_CONVERSION_FAILED,
30894
+ level: "error",
30895
+ file: href,
30896
+ message: `Item conversion failed for "${href}" but produced no specific error diagnostic. The item may use an unsupported interaction or be missing required data.`
30897
+ }];
30898
+ }
30899
+ /**
30766
30900
  * Normalize a forward-slash relative path (collapse `.`/`//`, resolve `..`).
30767
30901
  * Pure replacement for `node:path`'s `normalize(...).split(sep).join('/')` on
30768
30902
  * the forward-slash paths used as entry keys.
@@ -30988,7 +31122,8 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
30988
31122
  mediaVault: opts?.mediaVault,
30989
31123
  customAssetUrl: opts?.customAssetUrl,
30990
31124
  itemReferenceSource: opts?.itemReferenceSource,
30991
- fileAccess: opts?.fileAccess
31125
+ fileAccess: opts?.fileAccess,
31126
+ packageRoot: opts?.sourceDir
30992
31127
  });
30993
31128
  diagnostics.push(...itemResult.diagnostics);
30994
31129
  if (itemResult.item) {
@@ -31038,7 +31173,10 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31038
31173
  if (!qtiRefToMd5Refs.has(itemResource.identifier)) qtiRefToMd5Refs.set(itemResource.identifier, []);
31039
31174
  qtiRefToMd5Refs.get(itemResource.identifier)?.push(md5Ref);
31040
31175
  convertedSourceFiles.push(itemResource.href);
31041
- } else failedSourceFiles.push(itemResource.href);
31176
+ } else {
31177
+ failedSourceFiles.push(itemResource.href);
31178
+ diagnostics.push(...ensureFailedItemDiagnostic(itemResult.diagnostics, itemResource.href));
31179
+ }
31042
31180
  }
31043
31181
  if (items.size === 0 && itemResourcesToConvert.length > 0) {
31044
31182
  diagnostics.push({
@@ -21,6 +21,28 @@ interface FileAccess {
21
21
  * LOM XML, or `undefined` if none is found (or without filesystem access).
22
22
  */
23
23
  findAndReadLomFile(itemPath: string): string | undefined;
24
+ /**
25
+ * Resolve an image referenced by an `<object data="...">` / `<img src="...">`
26
+ * tag and read its intrinsic pixel dimensions from the file header.
27
+ *
28
+ * Used when the QTI markup omits `width`/`height` attributes (which is valid
29
+ * QTI) but a downstream Learnosity question needs pixel dimensions (e.g.
30
+ * hotspot coordinate conversion). Returns `undefined` if the file cannot be
31
+ * resolved/read, the format is unsupported, or when running without
32
+ * filesystem access.
33
+ *
34
+ * `packageRoot`, when provided, is the directory that bounds every read: any
35
+ * asset inside the package resolves regardless of directory naming, while
36
+ * paths escaping the package are rejected. When omitted, reads are confined
37
+ * to the source item's own directory.
38
+ *
39
+ * Optional so in-memory/browser callers and existing implementations that
40
+ * predate it remain valid.
41
+ */
42
+ resolveImageDimensions?: (_objectDataPath: string, _sourceFile?: string, _packageRoot?: string) => {
43
+ width: number;
44
+ height: number;
45
+ } | undefined;
24
46
  }
25
47
  //#endregion
26
48
  //#region src/types.d.ts
@@ -166,6 +188,7 @@ declare const DiagnosticCode: {
166
188
  readonly QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS";
167
189
  readonly QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT";
168
190
  readonly QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER";
191
+ readonly QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED";
169
192
  readonly LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN";
170
193
  readonly QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE";
171
194
  readonly QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION";
@@ -2418,6 +2441,12 @@ interface ConvertActivityOptions {
2418
2441
  pretty?: boolean;
2419
2442
  /** QTI version (2.1 or 2.2) */
2420
2443
  version?: QtiVersion;
2444
+ /**
2445
+ * Base URL/prefix that replaces the asset reference prefix up to the
2446
+ * filename for images/audio/video (default: `./assets`). For example
2447
+ * `https://cdn.example.com/media` produces `https://cdn.example.com/media/<filename>`.
2448
+ */
2449
+ customAssetUrl?: string;
2421
2450
  }
2422
2451
  /**
2423
2452
  * Convert a Learnosity Activity to QTI 2.2/2.1 assessmentTest + items
@@ -2543,6 +2572,7 @@ interface ConvertOptions {
2543
2572
  itemReferenceSource?: ItemReferenceSource;
2544
2573
  truncateTags?: boolean;
2545
2574
  fileAccess?: FileAccess;
2575
+ packageRoot?: string;
2546
2576
  }
2547
2577
  /**
2548
2578
  * Convert QTI assessmentItem XML to Learnosity Item JSON
@@ -2891,6 +2921,11 @@ interface ConvertLearnosityToQtiPackageOptions {
2891
2921
  qtiVersion?: QtiVersion;
2892
2922
  /** Pretty-print XML output */
2893
2923
  pretty?: boolean;
2924
+ /**
2925
+ * Base URL/prefix that replaces the asset reference prefix up to the
2926
+ * filename for images/audio/video (default: `./assets`).
2927
+ */
2928
+ customAssetUrl?: string;
2894
2929
  }
2895
2930
  /**
2896
2931
  * Result of converting a Learnosity bundle to a QTI Content Package