@pie-qti/ims-cp-core 0.1.25 → 0.1.26

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.
@@ -84,20 +84,8 @@ function parseResource(resourceEl) {
84
84
  const dependencies = Array.from(depElements)
85
85
  .map(d => d.getAttribute('identifierref'))
86
86
  .filter((id) => !!id);
87
- // Extract metadata if present
88
- let metadata;
89
87
  const metadataEl = resourceEl.querySelector('metadata');
90
- if (metadataEl) {
91
- const title = metadataEl.querySelector('title')?.textContent?.trim();
92
- const description = metadataEl.querySelector('description')?.textContent?.trim();
93
- // Extract IMS LOM language (supports multiple selector patterns)
94
- const languageEl = metadataEl.querySelector('language, [\\:language]') ||
95
- metadataEl.querySelector('[name="language"]');
96
- const language = languageEl?.textContent?.trim();
97
- if (title || description || language) {
98
- metadata = { title, description, language };
99
- }
100
- }
88
+ const metadata = metadataEl ? parseResourceMetadata(metadataEl) : undefined;
101
89
  return {
102
90
  identifier,
103
91
  type,
@@ -108,6 +96,58 @@ function parseResource(resourceEl) {
108
96
  metadata,
109
97
  };
110
98
  }
99
+ /**
100
+ * Title, description and language of a resource's `<metadata>`
101
+ *
102
+ * LOM is matched by local name, so a prefixed binding (`imsmd:title`) reads the same as an unprefixed
103
+ * one, and LangString text comes from its first `string` (or LOM 1.2 `langstring`). When the metadata
104
+ * holds LOM, only `general` is read: a `classification` description is not the resource's.
105
+ */
106
+ function parseResourceMetadata(metadataEl) {
107
+ let title;
108
+ let description;
109
+ let language;
110
+ const loms = descendantsNamed(metadataEl, 'lom');
111
+ if (loms.length > 0) {
112
+ const general = loms.flatMap((lom) => childrenNamed(lom, 'general'))[0];
113
+ if (general) {
114
+ title = langStringText(childrenNamed(general, 'title')[0]);
115
+ description = langStringText(childrenNamed(general, 'description')[0]);
116
+ language = textOf(childrenNamed(general, 'language')[0]);
117
+ }
118
+ }
119
+ else {
120
+ title = langStringText(descendantsNamed(metadataEl, 'title')[0]);
121
+ description = langStringText(descendantsNamed(metadataEl, 'description')[0]);
122
+ const languageEl = descendantsNamed(metadataEl, 'language')[0] ||
123
+ metadataEl.querySelector('[\\:language]') ||
124
+ metadataEl.querySelector('[name="language"]');
125
+ language = textOf(languageEl ?? undefined);
126
+ }
127
+ return title || description || language ? { title, description, language } : undefined;
128
+ }
129
+ function langStringText(el) {
130
+ if (!el)
131
+ return undefined;
132
+ const string = [...childrenNamed(el, 'string'), ...childrenNamed(el, 'langstring')][0];
133
+ return textOf(string ?? el);
134
+ }
135
+ function textOf(el) {
136
+ return el?.textContent?.trim() || undefined;
137
+ }
138
+ /** Tag name without its namespace prefix, lowercased. */
139
+ function localName(el) {
140
+ return (el.rawTagName ?? '').replace(/^[^:]*:/, '').toLowerCase();
141
+ }
142
+ function childrenNamed(el, name) {
143
+ return el.children.filter((child) => localName(child) === name);
144
+ }
145
+ function descendantsNamed(el, name) {
146
+ return el.children.flatMap((child) => [
147
+ ...(localName(child) === name ? [child] : []),
148
+ ...descendantsNamed(child, name),
149
+ ]);
150
+ }
111
151
  /**
112
152
  * Check if resource type is an item
113
153
  */
@@ -1 +1 @@
1
- {"version":3,"file":"manifest-parser.js","sourceRoot":"","sources":["../src/manifest-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAmE3D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,QAAiB;IAClE,MAAM,GAAG,GAAG,KAAK,CAAC,WAAW,EAAE;QAC7B,gBAAgB,EAAE,KAAK;QACvB,OAAO,EAAE,KAAK;KACf,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,GAAG,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1F,IAAI,CAAC,QAAQ,EAAE,CAAC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ACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACxC,QAAwB,EACxB,SAAiB;IAEjB,OAAO,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAClC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CACtC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * QTI Manifest (imsmanifest.xml) parser\n *\n * Parses IMS Content Package manifests to extract:\n * - Resource definitions (items, passages, assessments)\n * - Dependencies between resources\n * - File locations and types\n */\n\nimport type { HTMLElement } from 'node-html-parser';\nimport { parse } from 'node-html-parser';\nimport { PassageRegistry } from './passage-reusability.js';\n\n/**\n * QTI resource type\n */\nexport type ResourceType =\n | 'imsqti_item_xmlv2p1'\n | 'imsqti_item_xmlv2p2'\n | 'imsqti_test_xmlv2p1'\n | 'imsqti_test_xmlv2p2'\n | 'imsqti_assessment_xmlv2p1'\n | 'imsqti_apip_xmlv2p2'\n | 'webcontent'\n | 'associatedcontent/imsqti_item_xmlv2p1'\n | string;\n\n/**\n * Manifest resource entry\n */\nexport interface ManifestResource {\n /** Resource identifier */\n identifier: string;\n /** Resource type */\n type: ResourceType;\n /**\n * Optional xml:base for this resource element.\n * Used by IMS CP to resolve relative hrefs when packages use nested bases.\n */\n xmlBase?: string;\n /** Main file href */\n href?: string;\n /** All files in this resource */\n files: string[];\n /** Dependencies (identifierrefs) */\n dependencies: string[];\n /** Metadata */\n metadata?: {\n title?: string;\n description?: string;\n /** IMS LOM language code (e.g., \"en-US\", \"es-ES\") */\n language?: string;\n };\n}\n\n/**\n * Parsed manifest structure\n */\nexport interface ParsedManifest {\n /** Manifest identifier */\n identifier?: string;\n /**\n * Optional xml:base at the manifest element level.\n * Used by IMS CP to resolve relative hrefs.\n */\n xmlBase?: string;\n /** All resources */\n resources: Map<string, ManifestResource>;\n /** Item resources */\n items: ManifestResource[];\n /** Passage/stimulus resources */\n passages: ManifestResource[];\n /** Test/assessment resources */\n tests: ManifestResource[];\n /** Base path for resolving relative paths */\n basePath?: string;\n}\n\n/**\n * Parse QTI manifest XML\n */\nexport function parseManifest(manifestXml: string, basePath?: string): ParsedManifest {\n const doc = parse(manifestXml, {\n lowerCaseTagName: false,\n comment: false,\n });\n\n const manifest = doc.querySelector('manifest') || doc.getElementsByTagName('manifest')[0];\n\n if (!manifest) {\n throw new Error('No manifest element found in XML');\n }\n\n const manifestId = manifest.getAttribute('identifier');\n const manifestXmlBase = manifest.getAttribute('xml:base') || undefined;\n\n // Parse all resources\n const resourcesElement = manifest.querySelector('resources') ||\n manifest.getElementsByTagName('resources')[0];\n\n if (!resourcesElement) {\n return {\n identifier: manifestId,\n resources: new Map(),\n items: [],\n passages: [],\n tests: [],\n basePath,\n };\n }\n\n const resourceElements = resourcesElement.getElementsByTagName('resource');\n const resources = new Map<string, ManifestResource>();\n const items: ManifestResource[] = [];\n const passages: ManifestResource[] = [];\n const tests: ManifestResource[] = [];\n\n for (const resourceEl of Array.from(resourceElements)) {\n const resource = parseResource(resourceEl);\n resources.set(resource.identifier, resource);\n\n // Categorize by type\n if (isItemType(resource.type)) {\n items.push(resource);\n } else if (isPassageType(resource.type)) {\n passages.push(resource);\n } else if (isTestType(resource.type)) {\n tests.push(resource);\n }\n }\n\n return {\n identifier: manifestId,\n xmlBase: manifestXmlBase,\n resources,\n items,\n passages,\n tests,\n basePath,\n };\n}\n\n/**\n * Parse individual resource element\n */\nfunction parseResource(resourceEl: HTMLElement): ManifestResource {\n const identifier = resourceEl.getAttribute('identifier') || '';\n const type = resourceEl.getAttribute('type') || '';\n const href = resourceEl.getAttribute('href') || undefined;\n const xmlBase = resourceEl.getAttribute('xml:base') || undefined;\n\n // Extract all file elements\n const fileElements = resourceEl.getElementsByTagName('file');\n const files = Array.from(fileElements)\n .map(f => f.getAttribute('href'))\n .filter((h): h is string => !!h);\n\n // If href not specified but files exist, use first file as href\n const mainHref = href || (files.length > 0 ? files[0] : undefined);\n\n // Extract dependencies\n const depElements = resourceEl.getElementsByTagName('dependency');\n const dependencies = Array.from(depElements)\n .map(d => d.getAttribute('identifierref'))\n .filter((id): id is string => !!id);\n\n // Extract metadata if present\n let metadata: { title?: string; description?: string; language?: string } | undefined;\n const metadataEl = resourceEl.querySelector('metadata');\n if (metadataEl) {\n const title = metadataEl.querySelector('title')?.textContent?.trim();\n const description = metadataEl.querySelector('description')?.textContent?.trim();\n\n // Extract IMS LOM language (supports multiple selector patterns)\n const languageEl = metadataEl.querySelector('language, [\\\\:language]') ||\n metadataEl.querySelector('[name=\"language\"]');\n const language = languageEl?.textContent?.trim();\n\n if (title || description || language) {\n metadata = { title, description, language };\n }\n }\n\n return {\n identifier,\n type,\n xmlBase,\n href: mainHref,\n files,\n dependencies,\n metadata,\n };\n}\n\n/**\n * Check if resource type is an item\n */\nfunction isItemType(type: string): boolean {\n return type.includes('imsqti_item') ||\n type.includes('qti_item') ||\n type === 'associatedcontent/imsqti_item_xmlv2p1';\n}\n\n/**\n * Check if resource type is a passage/stimulus\n */\nfunction isPassageType(type: string): boolean {\n return type === 'webcontent' ||\n type.includes('passage') ||\n type.includes('stimulus') ||\n type.includes('apip');\n}\n\n/**\n * Check if resource type is a test/assessment\n */\nfunction isTestType(type: string): boolean {\n return type.includes('test') || type.includes('assessment');\n}\n\n/**\n * Build passage registry from manifest\n *\n * Analyzes manifest to identify:\n * - Which passages are referenced by items\n * - Which passages are reusable (multiple items reference them)\n */\nexport function buildPassageRegistryFromManifest(\n manifest: ParsedManifest\n): PassageRegistry {\n const registry = new PassageRegistry();\n\n // Register all passage resources\n for (const passage of manifest.passages) {\n // Find all items that depend on this passage\n for (const item of manifest.items) {\n if (item.dependencies.includes(passage.identifier)) {\n registry.registerReference(item.identifier, {\n id: passage.identifier, // Use QTI identifier directly\n source: 'manifest',\n qtiIdentifier: passage.identifier,\n filePath: passage.href,\n isReusable: false, // Will be updated by registry\n });\n }\n }\n }\n\n return registry;\n}\n\n/**\n * Get passage content file path from manifest\n */\nexport function getPassageFilePath(\n manifest: ParsedManifest,\n passageId: string\n): string | undefined {\n const passage = manifest.resources.get(passageId);\n return passage?.href;\n}\n\n/**\n * Get all items that depend on a passage\n */\nexport function getItemsReferencingPassage(\n manifest: ParsedManifest,\n passageId: string\n): ManifestResource[] {\n return manifest.items.filter(item =>\n item.dependencies.includes(passageId)\n );\n}\n"]}
1
+ {"version":3,"file":"manifest-parser.js","sourceRoot":"","sources":["../src/manifest-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAmE3D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,QAAiB;IAClE,MAAM,GAAG,GAAG,KAAK,CAAC,WAAW,EAAE;QAC7B,gBAAgB,EAAE,KAAK;QACvB,OAAO,EAAE,KAAK;KACf,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,GAAG,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1F,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;IACvD,MAAM,eAAe,GAAG,QAAQ,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,SAAS,CAAC;IAEvE,sBAAsB;IACtB,MAAM,gBAAgB,GAAG,QAAQ,CAAC,aAAa,CAAC,WAAW,CAAC;QACpC,QAAQ,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IAEtE,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACtB,OAAO;YACL,UAAU,EAAE,UAAU;YACtB,SAAS,EAAE,IAAI,GAAG,EAAE;Y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C;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAAwB,EACxB,SAAiB;IAEjB,MAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAClD,OAAO,OAAO,EAAE,IAAI,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACxC,QAAwB,EACxB,SAAiB;IAEjB,OAAO,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAClC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CACtC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * QTI Manifest (imsmanifest.xml) parser\n *\n * Parses IMS Content Package manifests to extract:\n * - Resource definitions (items, passages, assessments)\n * - Dependencies between resources\n * - File locations and types\n */\n\nimport type { HTMLElement } from 'node-html-parser';\nimport { parse } from 'node-html-parser';\nimport { PassageRegistry } from './passage-reusability.js';\n\n/**\n * QTI resource type\n */\nexport type ResourceType =\n | 'imsqti_item_xmlv2p1'\n | 'imsqti_item_xmlv2p2'\n | 'imsqti_test_xmlv2p1'\n | 'imsqti_test_xmlv2p2'\n | 'imsqti_assessment_xmlv2p1'\n | 'imsqti_apip_xmlv2p2'\n | 'webcontent'\n | 'associatedcontent/imsqti_item_xmlv2p1'\n | string;\n\n/**\n * Manifest resource entry\n */\nexport interface ManifestResource {\n /** Resource identifier */\n identifier: string;\n /** Resource type */\n type: ResourceType;\n /**\n * Optional xml:base for this resource element.\n * Used by IMS CP to resolve relative hrefs when packages use nested bases.\n */\n xmlBase?: string;\n /** Main file href */\n href?: string;\n /** All files in this resource */\n files: string[];\n /** Dependencies (identifierrefs) */\n dependencies: string[];\n /** Metadata */\n metadata?: {\n title?: string;\n description?: string;\n /** IMS LOM language code (e.g., \"en-US\", \"es-ES\") */\n language?: string;\n };\n}\n\n/**\n * Parsed manifest structure\n */\nexport interface ParsedManifest {\n /** Manifest identifier */\n identifier?: string;\n /**\n * Optional xml:base at the manifest element level.\n * Used by IMS CP to resolve relative hrefs.\n */\n xmlBase?: string;\n /** All resources */\n resources: Map<string, ManifestResource>;\n /** Item resources */\n items: ManifestResource[];\n /** Passage/stimulus resources */\n passages: ManifestResource[];\n /** Test/assessment resources */\n tests: ManifestResource[];\n /** Base path for resolving relative paths */\n basePath?: string;\n}\n\n/**\n * Parse QTI manifest XML\n */\nexport function parseManifest(manifestXml: string, basePath?: string): ParsedManifest {\n const doc = parse(manifestXml, {\n lowerCaseTagName: false,\n comment: false,\n });\n\n const manifest = doc.querySelector('manifest') || doc.getElementsByTagName('manifest')[0];\n\n if (!manifest) {\n throw new Error('No manifest element found in XML');\n }\n\n const manifestId = manifest.getAttribute('identifier');\n const manifestXmlBase = manifest.getAttribute('xml:base') || undefined;\n\n // Parse all resources\n const resourcesElement = manifest.querySelector('resources') ||\n manifest.getElementsByTagName('resources')[0];\n\n if (!resourcesElement) {\n return {\n identifier: manifestId,\n resources: new Map(),\n items: [],\n passages: [],\n tests: [],\n basePath,\n };\n }\n\n const resourceElements = resourcesElement.getElementsByTagName('resource');\n const resources = new Map<string, ManifestResource>();\n const items: ManifestResource[] = [];\n const passages: ManifestResource[] = [];\n const tests: ManifestResource[] = [];\n\n for (const resourceEl of Array.from(resourceElements)) {\n const resource = parseResource(resourceEl);\n resources.set(resource.identifier, resource);\n\n // Categorize by type\n if (isItemType(resource.type)) {\n items.push(resource);\n } else if (isPassageType(resource.type)) {\n passages.push(resource);\n } else if (isTestType(resource.type)) {\n tests.push(resource);\n }\n }\n\n return {\n identifier: manifestId,\n xmlBase: manifestXmlBase,\n resources,\n items,\n passages,\n tests,\n basePath,\n };\n}\n\n/**\n * Parse individual resource element\n */\nfunction parseResource(resourceEl: HTMLElement): ManifestResource {\n const identifier = resourceEl.getAttribute('identifier') || '';\n const type = resourceEl.getAttribute('type') || '';\n const href = resourceEl.getAttribute('href') || undefined;\n const xmlBase = resourceEl.getAttribute('xml:base') || undefined;\n\n // Extract all file elements\n const fileElements = resourceEl.getElementsByTagName('file');\n const files = Array.from(fileElements)\n .map(f => f.getAttribute('href'))\n .filter((h): h is string => !!h);\n\n // If href not specified but files exist, use first file as href\n const mainHref = href || (files.length > 0 ? files[0] : undefined);\n\n // Extract dependencies\n const depElements = resourceEl.getElementsByTagName('dependency');\n const dependencies = Array.from(depElements)\n .map(d => d.getAttribute('identifierref'))\n .filter((id): id is string => !!id);\n\n const metadataEl = resourceEl.querySelector('metadata');\n const metadata = metadataEl ? parseResourceMetadata(metadataEl) : undefined;\n\n return {\n identifier,\n type,\n xmlBase,\n href: mainHref,\n files,\n dependencies,\n metadata,\n };\n}\n\n/**\n * Title, description and language of a resource's `<metadata>`\n *\n * LOM is matched by local name, so a prefixed binding (`imsmd:title`) reads the same as an unprefixed\n * one, and LangString text comes from its first `string` (or LOM 1.2 `langstring`). When the metadata\n * holds LOM, only `general` is read: a `classification` description is not the resource's.\n */\nfunction parseResourceMetadata(metadataEl: HTMLElement): ManifestResource['metadata'] {\n let title: string | undefined;\n let description: string | undefined;\n let language: string | undefined;\n\n const loms = descendantsNamed(metadataEl, 'lom');\n if (loms.length > 0) {\n const general = loms.flatMap((lom) => childrenNamed(lom, 'general'))[0];\n if (general) {\n title = langStringText(childrenNamed(general, 'title')[0]);\n description = langStringText(childrenNamed(general, 'description')[0]);\n language = textOf(childrenNamed(general, 'language')[0]);\n }\n } else {\n title = langStringText(descendantsNamed(metadataEl, 'title')[0]);\n description = langStringText(descendantsNamed(metadataEl, 'description')[0]);\n const languageEl = descendantsNamed(metadataEl, 'language')[0] ||\n metadataEl.querySelector('[\\\\:language]') ||\n metadataEl.querySelector('[name=\"language\"]');\n language = textOf(languageEl ?? undefined);\n }\n\n return title || description || language ? { title, description, language } : undefined;\n}\n\nfunction langStringText(el: HTMLElement | undefined): string | undefined {\n if (!el) return undefined;\n const string = [...childrenNamed(el, 'string'), ...childrenNamed(el, 'langstring')][0];\n return textOf(string ?? el);\n}\n\nfunction textOf(el: HTMLElement | undefined): string | undefined {\n return el?.textContent?.trim() || undefined;\n}\n\n/** Tag name without its namespace prefix, lowercased. */\nfunction localName(el: HTMLElement): string {\n return (el.rawTagName ?? '').replace(/^[^:]*:/, '').toLowerCase();\n}\n\nfunction childrenNamed(el: HTMLElement, name: string): HTMLElement[] {\n return el.children.filter((child) => localName(child) === name);\n}\n\nfunction descendantsNamed(el: HTMLElement, name: string): HTMLElement[] {\n return el.children.flatMap((child) => [\n ...(localName(child) === name ? [child] : []),\n ...descendantsNamed(child, name),\n ]);\n}\n\n/**\n * Check if resource type is an item\n */\nfunction isItemType(type: string): boolean {\n return type.includes('imsqti_item') ||\n type.includes('qti_item') ||\n type === 'associatedcontent/imsqti_item_xmlv2p1';\n}\n\n/**\n * Check if resource type is a passage/stimulus\n */\nfunction isPassageType(type: string): boolean {\n return type === 'webcontent' ||\n type.includes('passage') ||\n type.includes('stimulus') ||\n type.includes('apip');\n}\n\n/**\n * Check if resource type is a test/assessment\n */\nfunction isTestType(type: string): boolean {\n return type.includes('test') || type.includes('assessment');\n}\n\n/**\n * Build passage registry from manifest\n *\n * Analyzes manifest to identify:\n * - Which passages are referenced by items\n * - Which passages are reusable (multiple items reference them)\n */\nexport function buildPassageRegistryFromManifest(\n manifest: ParsedManifest\n): PassageRegistry {\n const registry = new PassageRegistry();\n\n // Register all passage resources\n for (const passage of manifest.passages) {\n // Find all items that depend on this passage\n for (const item of manifest.items) {\n if (item.dependencies.includes(passage.identifier)) {\n registry.registerReference(item.identifier, {\n id: passage.identifier, // Use QTI identifier directly\n source: 'manifest',\n qtiIdentifier: passage.identifier,\n filePath: passage.href,\n isReusable: false, // Will be updated by registry\n });\n }\n }\n }\n\n return registry;\n}\n\n/**\n * Get passage content file path from manifest\n */\nexport function getPassageFilePath(\n manifest: ParsedManifest,\n passageId: string\n): string | undefined {\n const passage = manifest.resources.get(passageId);\n return passage?.href;\n}\n\n/**\n * Get all items that depend on a passage\n */\nexport function getItemsReferencingPassage(\n manifest: ParsedManifest,\n passageId: string\n): ManifestResource[] {\n return manifest.items.filter(item =>\n item.dependencies.includes(passageId)\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-qti/ims-cp-core",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "Core IMS Content Package manifest parsing utilities (browser + Node.js)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -36,8 +36,8 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@pie-qti/logger": "0.1.25",
40
- "node-html-parser": "^9.0.1"
39
+ "@pie-qti/logger": "0.1.26",
40
+ "node-html-parser": "^9.0.4"
41
41
  },
42
42
  "devDependencies": {
43
43
  "typescript": "^5.9.3"