@pie-qti/item-player 0.1.14 → 0.1.15
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.
|
@@ -66,20 +66,26 @@ export function processFeedbackInline(html, options) {
|
|
|
66
66
|
const { outcomeValues, applyHeuristics = false, wrapWithSpan = false, transforms } = options;
|
|
67
67
|
if (!html)
|
|
68
68
|
return html;
|
|
69
|
-
return html.replace(/<feedbackInline
|
|
70
|
-
const
|
|
69
|
+
return html.replace(/<feedbackInline\b([^>]*)>([\s\S]*?)<\/feedbackInline>/gi, (_match, attrs, content) => {
|
|
70
|
+
const outcomeId = feedbackInlineAttr(attrs, 'outcomeIdentifier');
|
|
71
|
+
const feedbackId = feedbackInlineAttr(attrs, 'identifier');
|
|
72
|
+
const showHide = feedbackInlineAttr(attrs, 'showHide') ?? 'show';
|
|
73
|
+
const outcomeValue = outcomeId ? outcomeValues[outcomeId] : undefined;
|
|
71
74
|
// If outcome value is not set (undefined, null, empty string), hide feedback
|
|
72
|
-
if (
|
|
75
|
+
if (outcomeId === null ||
|
|
76
|
+
feedbackId === null ||
|
|
77
|
+
outcomeValue === undefined ||
|
|
78
|
+
outcomeValue === null ||
|
|
79
|
+
outcomeValue === '') {
|
|
73
80
|
return '';
|
|
74
81
|
}
|
|
75
82
|
// Determine if feedback should be shown based on showHide attribute
|
|
76
|
-
const
|
|
77
|
-
? outcomeValue
|
|
78
|
-
: outcomeValue
|
|
83
|
+
const matchesFeedback = Array.isArray(outcomeValue)
|
|
84
|
+
? outcomeValue.includes(feedbackId)
|
|
85
|
+
: outcomeValue === feedbackId;
|
|
86
|
+
const shouldShow = showHide.toLowerCase() === 'show' ? matchesFeedback : !matchesFeedback;
|
|
79
87
|
if (!shouldShow)
|
|
80
88
|
return '';
|
|
81
|
-
// Extract content from feedbackInline tags
|
|
82
|
-
let content = match.replace(/<feedbackInline[^>]*>/, '').replace(/<\/feedbackInline>/, '');
|
|
83
89
|
// Apply text cleaning heuristics if enabled
|
|
84
90
|
if (applyHeuristics) {
|
|
85
91
|
content = cleanFeedbackText(content, transforms);
|
|
@@ -91,4 +97,9 @@ export function processFeedbackInline(html, options) {
|
|
|
91
97
|
return content;
|
|
92
98
|
});
|
|
93
99
|
}
|
|
100
|
+
function feedbackInlineAttr(attrs, name) {
|
|
101
|
+
const match = new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`, 'i').exec(attrs) ??
|
|
102
|
+
new RegExp(`\\b${name}\\s*=\\s*'([^']*)'`, 'i').exec(attrs);
|
|
103
|
+
return match?.[1] ?? null;
|
|
104
|
+
}
|
|
94
105
|
//# sourceMappingURL=feedbackUtils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feedbackUtils.js","sourceRoot":"","sources":["../../../src/components/utils/feedbackUtils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAYH;;;GAGG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAwB;IAC/D,sEAAsE;IACtE,EAAE,OAAO,EAAE,gCAAgC,EAAE,WAAW,EAAE,6BAA6B,EAAE;IACzF,EAAE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE;IAEtD,oEAAoE;IACpE,EAAE,OAAO,EAAE,8BAA8B,EAAE,WAAW,EAAE,2BAA2B,EAAE;IACrF,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE;CAClD,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAChC,OAAe,EACf,aAAkC,2BAA2B;IAE7D,IAAI,CAAC,OAAO;QAAE,OAAO,OAAO,CAAC;IAE7B,IAAI,MAAM,GAAG,OAAO,CAAC;IACrB,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACpC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC;IACnE,CAAC;IAED,OAAO,MAAM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AAI,eAAe,EAAE,CAAC;YACrB,OAAO,GAAG,iBAAiB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;QAClD,CAAC;QAED,oCAAoC;QACpC,IAAI,YAAY,EAAE,CAAC;YAClB,OAAO,sCAAsC,OAAO,SAAS,CAAC;QAC/D,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC,CACD,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAa,EAAE,IAAY;IACtD,MAAM,KAAK,GACV,IAAI,MAAM,CAAC,MAAM,IAAI,oBAAoB,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;QAC3D,IAAI,MAAM,CAAC,MAAM,IAAI,oBAAoB,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7D,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAC3B,CAAC","sourcesContent":["/**\n * Feedback text utilities for QTI items\n *\n * NOTE: The cleanFeedbackText function contains vendor-specific transforms\n * for content that uses semicolons after \"Correct\" and \"Incorrect\" labels.\n * This is common in some QTI authoring tools but not part of the QTI specification.\n *\n * To customize this behavior for your vendor's content, you can:\n * 1. Modify the patterns in this file\n * 2. Create a vendor-specific transform plugin (see VENDOR-TRANSFORM-PLUGIN-GUIDE.md)\n * 3. Pass custom transforms via the feedbackTransforms option\n */\n\n/**\n * Text pattern replacement for feedback content\n */\nexport interface FeedbackTransform {\n\t/** Regular expression to match the pattern */\n\tpattern: RegExp;\n\t/** Replacement string (can use capture groups like $1, $2) */\n\treplacement: string;\n}\n\n/**\n * Default feedback text transforms\n * These are vendor-specific patterns commonly found in certain QTI authoring tools\n */\nexport const DEFAULT_FEEDBACK_TRANSFORMS: FeedbackTransform[] = [\n\t// Remove semicolons after \"Incorrect\" (with or without <strong> tags)\n\t{ pattern: /<strong>Incorrect;<\\/strong>/gi, replacement: '<strong>Incorrect</strong> ' },\n\t{ pattern: /Incorrect;/gi, replacement: 'Incorrect ' },\n\n\t// Remove semicolons after \"Correct\" (with or without <strong> tags)\n\t{ pattern: /<strong>Correct;<\\/strong>/gi, replacement: '<strong>Correct</strong> ' },\n\t{ pattern: /Correct;/gi, replacement: 'Correct ' },\n];\n\n/**\n * Clean up feedback text formatting using configurable transforms\n *\n * @param content - The feedback content HTML string\n * @param transforms - Optional custom transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS)\n * @returns Cleaned feedback content\n */\nexport function cleanFeedbackText(\n\tcontent: string,\n\ttransforms: FeedbackTransform[] = DEFAULT_FEEDBACK_TRANSFORMS\n): string {\n\tif (!content) return content;\n\n\tlet result = content;\n\tfor (const transform of transforms) {\n\t\tresult = result.replace(transform.pattern, transform.replacement);\n\t}\n\n\treturn result;\n}\n\n/**\n * Default outcome identifier for feedback display\n * This can be overridden if your QTI content uses a different identifier\n */\nexport const DEFAULT_FEEDBACK_OUTCOME_ID = 'FEEDBACK';\n\n/**\n * Options for processing feedbackInline elements\n */\nexport interface ProcessFeedbackInlineOptions {\n\t/** Outcome values from response processing */\n\toutcomeValues: Record<string, any>;\n\t/** Whether to apply feedback text cleaning heuristics */\n\tapplyHeuristics?: boolean;\n\t/** Whether to wrap feedback content with qti-feedback-inline span */\n\twrapWithSpan?: boolean;\n\t/** Custom feedback transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS) */\n\ttransforms?: FeedbackTransform[];\n}\n\n/**\n * Process feedbackInline elements in QTI content\n *\n * Conditionally shows/hides feedback based on outcome values and the showHide attribute.\n * Optionally applies text cleaning heuristics and wraps content with styling span.\n *\n * @param html - The HTML content containing feedbackInline elements\n * @param options - Processing options\n * @returns Modified HTML with processed feedbackInline elements\n *\n * @example\n * ```typescript\n * const html = processFeedbackInline(content, {\n * outcomeValues: { FEEDBACK: 'choiceA' },\n * applyHeuristics: true,\n * wrapWithSpan: true\n * });\n * ```\n */\nexport function processFeedbackInline(\n\thtml: string,\n\toptions: ProcessFeedbackInlineOptions\n): string {\n\tconst { outcomeValues, applyHeuristics = false, wrapWithSpan = false, transforms } = options;\n\n\tif (!html) return html;\n\n\treturn html.replace(\n\t\t/<feedbackInline\\b([^>]*)>([\\s\\S]*?)<\\/feedbackInline>/gi,\n\t\t(_match, attrs, content) => {\n\t\t\tconst outcomeId = feedbackInlineAttr(attrs, 'outcomeIdentifier');\n\t\t\tconst feedbackId = feedbackInlineAttr(attrs, 'identifier');\n\t\t\tconst showHide = feedbackInlineAttr(attrs, 'showHide') ?? 'show';\n\t\t\tconst outcomeValue = outcomeId ? outcomeValues[outcomeId] : undefined;\n\n\t\t\t// If outcome value is not set (undefined, null, empty string), hide feedback\n\t\t\tif (\n\t\t\t\toutcomeId === null ||\n\t\t\t\tfeedbackId === null ||\n\t\t\t\toutcomeValue === undefined ||\n\t\t\t\toutcomeValue === null ||\n\t\t\t\toutcomeValue === ''\n\t\t\t) {\n\t\t\t\treturn '';\n\t\t\t}\n\n\t\t\t// Determine if feedback should be shown based on showHide attribute\n\t\t\tconst matchesFeedback = Array.isArray(outcomeValue)\n\t\t\t\t? outcomeValue.includes(feedbackId)\n\t\t\t\t: outcomeValue === feedbackId;\n\t\t\tconst shouldShow =\n\t\t\t\tshowHide.toLowerCase() === 'show' ? matchesFeedback : !matchesFeedback;\n\n\t\t\tif (!shouldShow) return '';\n\n\t\t\t// Apply text cleaning heuristics if enabled\n\t\t\tif (applyHeuristics) {\n\t\t\t\tcontent = cleanFeedbackText(content, transforms);\n\t\t\t}\n\n\t\t\t// Wrap with styling span if enabled\n\t\t\tif (wrapWithSpan) {\n\t\t\t\treturn ` <span class=\"qti-feedback-inline\">${content}</span>`;\n\t\t\t}\n\n\t\t\treturn content;\n\t\t}\n\t);\n}\n\nfunction feedbackInlineAttr(attrs: string, name: string): string | null {\n\tconst match =\n\t\tnew RegExp(`\\\\b${name}\\\\s*=\\\\s*\"([^\"]*)\"`, 'i').exec(attrs) ??\n\t\tnew RegExp(`\\\\b${name}\\\\s*=\\\\s*'([^']*)'`, 'i').exec(attrs);\n\treturn match?.[1] ?? null;\n}\n"]}
|