@pie-qti/qti-common 1.0.0-beta.2 → 1.0.0-beta.4
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.
- package/dist/diagnostics/warn-once.d.ts +14 -0
- package/dist/diagnostics/warn-once.js +21 -0
- package/dist/diagnostics/warn-once.js.map +1 -0
- package/dist/feedback/feedback-text.d.ts +77 -0
- package/dist/feedback/feedback-text.js +138 -0
- package/dist/feedback/feedback-text.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-time console diagnostics for host setup mistakes.
|
|
3
|
+
*
|
|
4
|
+
* These fire only when something is actually misconfigured (a message would
|
|
5
|
+
* render as a raw key, an attribute would be a no-op, a default endpoint is
|
|
6
|
+
* assumed), so they are not environment-gated: a warning that only appears
|
|
7
|
+
* because a host forgot wiring is useful in production too, and gating on
|
|
8
|
+
* NODE_ENV/import.meta is unreliable across the tsc- and vite-built outputs
|
|
9
|
+
* this workspace ships.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Emit `message` via console.warn at most once per unique `key` per page.
|
|
13
|
+
*/
|
|
14
|
+
export declare function warnOnce(key: string, message: string): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-time console diagnostics for host setup mistakes.
|
|
3
|
+
*
|
|
4
|
+
* These fire only when something is actually misconfigured (a message would
|
|
5
|
+
* render as a raw key, an attribute would be a no-op, a default endpoint is
|
|
6
|
+
* assumed), so they are not environment-gated: a warning that only appears
|
|
7
|
+
* because a host forgot wiring is useful in production too, and gating on
|
|
8
|
+
* NODE_ENV/import.meta is unreliable across the tsc- and vite-built outputs
|
|
9
|
+
* this workspace ships.
|
|
10
|
+
*/
|
|
11
|
+
const warned = new Set();
|
|
12
|
+
/**
|
|
13
|
+
* Emit `message` via console.warn at most once per unique `key` per page.
|
|
14
|
+
*/
|
|
15
|
+
export function warnOnce(key, message) {
|
|
16
|
+
if (warned.has(key))
|
|
17
|
+
return;
|
|
18
|
+
warned.add(key);
|
|
19
|
+
console.warn(`[pie-qti] ${message}`);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=warn-once.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"warn-once.js","sourceRoot":"","sources":["../../src/diagnostics/warn-once.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;AAEjC;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAW,EAAE,OAAe;IACpD,IAAI,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO;IAC5B,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,OAAO,CAAC,IAAI,CAAC,aAAa,OAAO,EAAE,CAAC,CAAC;AACtC,CAAC","sourcesContent":["/**\n * One-time console diagnostics for host setup mistakes.\n *\n * These fire only when something is actually misconfigured (a message would\n * render as a raw key, an attribute would be a no-op, a default endpoint is\n * assumed), so they are not environment-gated: a warning that only appears\n * because a host forgot wiring is useful in production too, and gating on\n * NODE_ENV/import.meta is unreliable across the tsc- and vite-built outputs\n * this workspace ships.\n */\n\nconst warned = new Set<string>();\n\n/**\n * Emit `message` via console.warn at most once per unique `key` per page.\n */\nexport function warnOnce(key: string, message: string): void {\n\tif (warned.has(key)) return;\n\twarned.add(key);\n\tconsole.warn(`[pie-qti] ${message}`);\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feedback text utilities for QTI items
|
|
3
|
+
*
|
|
4
|
+
* NOTE: The cleanFeedbackText function contains vendor-specific transforms
|
|
5
|
+
* for content that uses semicolons after "Correct" and "Incorrect" labels.
|
|
6
|
+
* This is common in some QTI authoring tools but not part of the QTI specification.
|
|
7
|
+
*
|
|
8
|
+
* To customize this behavior for your vendor's content, you can:
|
|
9
|
+
* 1. Modify the patterns in this file
|
|
10
|
+
* 2. Create a vendor-specific transform plugin (see VENDOR-TRANSFORM-PLUGIN-GUIDE.md)
|
|
11
|
+
* 3. Pass custom transforms via the feedbackTransforms option
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Text pattern replacement for feedback content
|
|
15
|
+
*/
|
|
16
|
+
export interface FeedbackTransform {
|
|
17
|
+
/** Regular expression to match the pattern */
|
|
18
|
+
pattern: RegExp;
|
|
19
|
+
/** Replacement string (can use capture groups like $1, $2) */
|
|
20
|
+
replacement: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Default feedback text transforms
|
|
24
|
+
* These are vendor-specific patterns commonly found in certain QTI authoring tools
|
|
25
|
+
*/
|
|
26
|
+
export declare const DEFAULT_FEEDBACK_TRANSFORMS: FeedbackTransform[];
|
|
27
|
+
/**
|
|
28
|
+
* Clean up feedback text formatting using configurable transforms
|
|
29
|
+
*
|
|
30
|
+
* @param content - The feedback content HTML string
|
|
31
|
+
* @param transforms - Optional custom transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS)
|
|
32
|
+
* @returns Cleaned feedback content
|
|
33
|
+
*/
|
|
34
|
+
export declare function cleanFeedbackText(content: string, transforms?: FeedbackTransform[]): string;
|
|
35
|
+
/**
|
|
36
|
+
* Default outcome identifier for feedback display
|
|
37
|
+
* This can be overridden if your QTI content uses a different identifier
|
|
38
|
+
*/
|
|
39
|
+
export declare const DEFAULT_FEEDBACK_OUTCOME_ID = "FEEDBACK";
|
|
40
|
+
/**
|
|
41
|
+
* Options for processing feedbackInline elements
|
|
42
|
+
*/
|
|
43
|
+
export interface ProcessFeedbackInlineOptions {
|
|
44
|
+
/** Outcome values from response processing */
|
|
45
|
+
outcomeValues: Record<string, any>;
|
|
46
|
+
/** Whether to apply feedback text cleaning heuristics */
|
|
47
|
+
applyHeuristics?: boolean;
|
|
48
|
+
/** Whether to wrap inline feedback content with qti-feedback-inline span */
|
|
49
|
+
wrapWithSpan?: boolean;
|
|
50
|
+
/** Custom feedback transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS) */
|
|
51
|
+
transforms?: FeedbackTransform[];
|
|
52
|
+
/** Drop every feedback element whatever the outcomes. */
|
|
53
|
+
hideAll?: boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Process feedbackInline and feedbackBlock elements in QTI content
|
|
57
|
+
*
|
|
58
|
+
* Shows or hides each `feedbackInline` and `feedbackBlock` (`qti-feedback-inline` and
|
|
59
|
+
* `qti-feedback-block` in QTI 3) by the shared visibility rule: its `identifier` against the
|
|
60
|
+
* outcome value, by membership for a container, inverted by `showHide="hide"`. Nested feedback
|
|
61
|
+
* resolves inside shown feedback; hidden feedback drops with everything inside it. Markup outside
|
|
62
|
+
* feedback elements is left as it is.
|
|
63
|
+
*
|
|
64
|
+
* @param html - The HTML content containing feedbackInline elements
|
|
65
|
+
* @param options - Processing options
|
|
66
|
+
* @returns Modified HTML with processed feedbackInline elements
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const html = processFeedbackInline(content, {
|
|
71
|
+
* outcomeValues: { FEEDBACK: 'choiceA' },
|
|
72
|
+
* applyHeuristics: true,
|
|
73
|
+
* wrapWithSpan: true
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function processFeedbackInline(html: string, options: ProcessFeedbackInlineOptions): string;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feedback text utilities for QTI items
|
|
3
|
+
*
|
|
4
|
+
* NOTE: The cleanFeedbackText function contains vendor-specific transforms
|
|
5
|
+
* for content that uses semicolons after "Correct" and "Incorrect" labels.
|
|
6
|
+
* This is common in some QTI authoring tools but not part of the QTI specification.
|
|
7
|
+
*
|
|
8
|
+
* To customize this behavior for your vendor's content, you can:
|
|
9
|
+
* 1. Modify the patterns in this file
|
|
10
|
+
* 2. Create a vendor-specific transform plugin (see VENDOR-TRANSFORM-PLUGIN-GUIDE.md)
|
|
11
|
+
* 3. Pass custom transforms via the feedbackTransforms option
|
|
12
|
+
*/
|
|
13
|
+
import { isShownByIdentifier } from './visibility.js';
|
|
14
|
+
/**
|
|
15
|
+
* Default feedback text transforms
|
|
16
|
+
* These are vendor-specific patterns commonly found in certain QTI authoring tools
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_FEEDBACK_TRANSFORMS = [
|
|
19
|
+
// Remove semicolons after "Incorrect" (with or without <strong> tags)
|
|
20
|
+
{ pattern: /<strong>Incorrect;<\/strong>/gi, replacement: '<strong>Incorrect</strong> ' },
|
|
21
|
+
{ pattern: /Incorrect;/gi, replacement: 'Incorrect ' },
|
|
22
|
+
// Remove semicolons after "Correct" (with or without <strong> tags)
|
|
23
|
+
{ pattern: /<strong>Correct;<\/strong>/gi, replacement: '<strong>Correct</strong> ' },
|
|
24
|
+
{ pattern: /Correct;/gi, replacement: 'Correct ' },
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* Clean up feedback text formatting using configurable transforms
|
|
28
|
+
*
|
|
29
|
+
* @param content - The feedback content HTML string
|
|
30
|
+
* @param transforms - Optional custom transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS)
|
|
31
|
+
* @returns Cleaned feedback content
|
|
32
|
+
*/
|
|
33
|
+
export function cleanFeedbackText(content, transforms = DEFAULT_FEEDBACK_TRANSFORMS) {
|
|
34
|
+
if (!content)
|
|
35
|
+
return content;
|
|
36
|
+
let result = content;
|
|
37
|
+
for (const transform of transforms) {
|
|
38
|
+
result = result.replace(transform.pattern, transform.replacement);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Default outcome identifier for feedback display
|
|
44
|
+
* This can be overridden if your QTI content uses a different identifier
|
|
45
|
+
*/
|
|
46
|
+
export const DEFAULT_FEEDBACK_OUTCOME_ID = 'FEEDBACK';
|
|
47
|
+
/**
|
|
48
|
+
* Process feedbackInline and feedbackBlock elements in QTI content
|
|
49
|
+
*
|
|
50
|
+
* Shows or hides each `feedbackInline` and `feedbackBlock` (`qti-feedback-inline` and
|
|
51
|
+
* `qti-feedback-block` in QTI 3) by the shared visibility rule: its `identifier` against the
|
|
52
|
+
* outcome value, by membership for a container, inverted by `showHide="hide"`. Nested feedback
|
|
53
|
+
* resolves inside shown feedback; hidden feedback drops with everything inside it. Markup outside
|
|
54
|
+
* feedback elements is left as it is.
|
|
55
|
+
*
|
|
56
|
+
* @param html - The HTML content containing feedbackInline elements
|
|
57
|
+
* @param options - Processing options
|
|
58
|
+
* @returns Modified HTML with processed feedbackInline elements
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const html = processFeedbackInline(content, {
|
|
63
|
+
* outcomeValues: { FEEDBACK: 'choiceA' },
|
|
64
|
+
* applyHeuristics: true,
|
|
65
|
+
* wrapWithSpan: true
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function processFeedbackInline(html, options) {
|
|
70
|
+
if (!html)
|
|
71
|
+
return html;
|
|
72
|
+
const feedback = feedbackElements(html);
|
|
73
|
+
if (feedback.length === 0)
|
|
74
|
+
return html;
|
|
75
|
+
const { outcomeValues, applyHeuristics = false, wrapWithSpan = false, transforms, hideAll = false } = options;
|
|
76
|
+
const render = (start, end, elements) => {
|
|
77
|
+
let out = '';
|
|
78
|
+
let position = start;
|
|
79
|
+
for (const element of elements) {
|
|
80
|
+
out += html.slice(position, element.openStart);
|
|
81
|
+
position = element.closeEnd;
|
|
82
|
+
const outcomeId = feedbackInlineAttr(element.attrs, 'outcomeIdentifier');
|
|
83
|
+
const feedbackId = feedbackInlineAttr(element.attrs, 'identifier');
|
|
84
|
+
if (hideAll || outcomeId === null || feedbackId === null)
|
|
85
|
+
continue;
|
|
86
|
+
const showHide = feedbackInlineAttr(element.attrs, 'showHide');
|
|
87
|
+
if (!isShownByIdentifier(outcomeValues[outcomeId], feedbackId, showHide))
|
|
88
|
+
continue;
|
|
89
|
+
let content = render(element.openEnd, element.closeStart, element.children);
|
|
90
|
+
if (applyHeuristics)
|
|
91
|
+
content = cleanFeedbackText(content, transforms);
|
|
92
|
+
out += wrapWithSpan && !element.block ? ` <span class="qti-feedback-inline">${content}</span>` : content;
|
|
93
|
+
}
|
|
94
|
+
return out + html.slice(position, end);
|
|
95
|
+
};
|
|
96
|
+
return render(0, html.length, feedback);
|
|
97
|
+
}
|
|
98
|
+
const FEEDBACK_TAG = /<(\/?)(feedbackInline|qti-feedback-inline|feedbackBlock|qti-feedback-block)\b([^>]*?)(\/?)>/gi;
|
|
99
|
+
/** The top-level feedback elements in `html`, each with the feedback nested inside it. */
|
|
100
|
+
function feedbackElements(html) {
|
|
101
|
+
const roots = [];
|
|
102
|
+
const open = [];
|
|
103
|
+
const add = (element) => (open[open.length - 1]?.children ?? roots).push(element);
|
|
104
|
+
for (const match of html.matchAll(FEEDBACK_TAG)) {
|
|
105
|
+
const [tag, closing, name = '', attrs = '', selfClosing] = match;
|
|
106
|
+
const start = match.index;
|
|
107
|
+
const end = start + tag.length;
|
|
108
|
+
if (closing) {
|
|
109
|
+
const element = open.pop();
|
|
110
|
+
if (!element)
|
|
111
|
+
continue;
|
|
112
|
+
element.closeStart = start;
|
|
113
|
+
element.closeEnd = end;
|
|
114
|
+
add(element);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const block = /block$/i.test(name);
|
|
118
|
+
const element = { block, attrs, openStart: start, openEnd: end, closeStart: end, closeEnd: end, children: [] };
|
|
119
|
+
if (selfClosing)
|
|
120
|
+
add(element);
|
|
121
|
+
else
|
|
122
|
+
open.push(element);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// An unclosed element is left as written, with any feedback inside it.
|
|
126
|
+
return roots;
|
|
127
|
+
}
|
|
128
|
+
/** An attribute in either QTI spelling, e.g. `outcomeIdentifier` or `outcome-identifier`. */
|
|
129
|
+
function feedbackInlineAttr(attrs, name) {
|
|
130
|
+
const kebab = name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
131
|
+
for (const spelling of kebab === name ? [name] : [name, kebab]) {
|
|
132
|
+
const match = new RegExp(`(?:^|\\s)${spelling}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`, 'i').exec(attrs);
|
|
133
|
+
if (match)
|
|
134
|
+
return match[1] ?? match[2] ?? '';
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=feedback-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feedback-text.js","sourceRoot":"","sources":["../../src/feedback/feedback-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAYtD;;;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,SA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AAI,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC9C,CAAC;IACD,OAAO,IAAI,CAAC;AACb,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\nimport { isShownByIdentifier } from './visibility.js';\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 inline feedback content with qti-feedback-inline span */\n\twrapWithSpan?: boolean;\n\t/** Custom feedback transforms (defaults to DEFAULT_FEEDBACK_TRANSFORMS) */\n\ttransforms?: FeedbackTransform[];\n\t/** Drop every feedback element whatever the outcomes. */\n\thideAll?: boolean;\n}\n\n/**\n * Process feedbackInline and feedbackBlock elements in QTI content\n *\n * Shows or hides each `feedbackInline` and `feedbackBlock` (`qti-feedback-inline` and\n * `qti-feedback-block` in QTI 3) by the shared visibility rule: its `identifier` against the\n * outcome value, by membership for a container, inverted by `showHide=\"hide\"`. Nested feedback\n * resolves inside shown feedback; hidden feedback drops with everything inside it. Markup outside\n * feedback elements is left as it is.\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\tif (!html) return html;\n\tconst feedback = feedbackElements(html);\n\tif (feedback.length === 0) return html;\n\n\tconst { outcomeValues, applyHeuristics = false, wrapWithSpan = false, transforms, hideAll = false } = options;\n\tconst render = (start: number, end: number, elements: readonly FeedbackElement[]): string => {\n\t\tlet out = '';\n\t\tlet position = start;\n\t\tfor (const element of elements) {\n\t\t\tout += html.slice(position, element.openStart);\n\t\t\tposition = element.closeEnd;\n\t\t\tconst outcomeId = feedbackInlineAttr(element.attrs, 'outcomeIdentifier');\n\t\t\tconst feedbackId = feedbackInlineAttr(element.attrs, 'identifier');\n\t\t\tif (hideAll || outcomeId === null || feedbackId === null) continue;\n\t\t\tconst showHide = feedbackInlineAttr(element.attrs, 'showHide');\n\t\t\tif (!isShownByIdentifier(outcomeValues[outcomeId], feedbackId, showHide)) continue;\n\n\t\t\tlet content = render(element.openEnd, element.closeStart, element.children);\n\t\t\tif (applyHeuristics) content = cleanFeedbackText(content, transforms);\n\t\t\tout += wrapWithSpan && !element.block ? ` <span class=\"qti-feedback-inline\">${content}</span>` : content;\n\t\t}\n\t\treturn out + html.slice(position, end);\n\t};\n\treturn render(0, html.length, feedback);\n}\n\nconst FEEDBACK_TAG = /<(\\/?)(feedbackInline|qti-feedback-inline|feedbackBlock|qti-feedback-block)\\b([^>]*?)(\\/?)>/gi;\n\ninterface FeedbackElement {\n\treadonly block: boolean;\n\treadonly attrs: string;\n\treadonly openStart: number;\n\treadonly openEnd: number;\n\tcloseStart: number;\n\tcloseEnd: number;\n\treadonly children: FeedbackElement[];\n}\n\n/** The top-level feedback elements in `html`, each with the feedback nested inside it. */\nfunction feedbackElements(html: string): FeedbackElement[] {\n\tconst roots: FeedbackElement[] = [];\n\tconst open: FeedbackElement[] = [];\n\tconst add = (element: FeedbackElement) => (open[open.length - 1]?.children ?? roots).push(element);\n\tfor (const match of html.matchAll(FEEDBACK_TAG)) {\n\t\tconst [tag, closing, name = '', attrs = '', selfClosing] = match;\n\t\tconst start = match.index;\n\t\tconst end = start + tag.length;\n\t\tif (closing) {\n\t\t\tconst element = open.pop();\n\t\t\tif (!element) continue;\n\t\t\telement.closeStart = start;\n\t\t\telement.closeEnd = end;\n\t\t\tadd(element);\n\t\t} else {\n\t\t\tconst block = /block$/i.test(name);\n\t\t\tconst element: FeedbackElement = { block, attrs, openStart: start, openEnd: end, closeStart: end, closeEnd: end, children: [] };\n\t\t\tif (selfClosing) add(element);\n\t\t\telse open.push(element);\n\t\t}\n\t}\n\t// An unclosed element is left as written, with any feedback inside it.\n\treturn roots;\n}\n\n/** An attribute in either QTI spelling, e.g. `outcomeIdentifier` or `outcome-identifier`. */\nfunction feedbackInlineAttr(attrs: string, name: string): string | null {\n\tconst kebab = name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n\tfor (const spelling of kebab === name ? [name] : [name, kebab]) {\n\t\tconst match = new RegExp(`(?:^|\\\\s)${spelling}\\\\s*=\\\\s*(?:\"([^\"]*)\"|'([^']*)')`, 'i').exec(attrs);\n\t\tif (match) return match[1] ?? match[2] ?? '';\n\t}\n\treturn null;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export { Qti3AttributeNameMapper } from './element-mapper/Qti3AttributeNameMappe
|
|
|
7
7
|
export { QTI2_PROMPT_BEARING_INTERACTIONS, QTI3_ATTRIBUTE_TO_QTI2, } from './element-mapper/qti3-attribute-map.js';
|
|
8
8
|
export { convertQti2ToQti3, QTI3_ASI_SCHEMA_LOCATION, QTI3_NAMESPACE, QTI3_RPTEMPLATE_BASE, } from './element-mapper/qti2-to-qti3.js';
|
|
9
9
|
export { isShownByIdentifier, matchesIdentifier } from './feedback/visibility.js';
|
|
10
|
+
export { cleanFeedbackText, processFeedbackInline, DEFAULT_FEEDBACK_OUTCOME_ID, DEFAULT_FEEDBACK_TRANSFORMS, type FeedbackTransform, type ProcessFeedbackInlineOptions, } from './feedback/feedback-text.js';
|
|
10
11
|
export { detectQtiVersion } from './version-detection/detectQtiVersion.js';
|
|
11
12
|
export type { QtiVersion } from './version-detection/detectQtiVersion.js';
|
|
12
13
|
export { getAttribute, getBooleanAttribute, getNumberAttribute, hasAttribute, toCamelCase, toKebabCase, } from './xml/attributes.js';
|
|
@@ -14,4 +15,5 @@ export { assignProps, type AssignPropsOptions } from './dom/assignProps.js';
|
|
|
14
15
|
export { createSvelteMountController, type SvelteMountController, type SvelteMountControllerOptions, } from './dom/svelteMountController.js';
|
|
15
16
|
export { touchDrag, touchDragStyles } from './dom/touchDrag.js';
|
|
16
17
|
export { languageDirection, type TextDirection } from './language/direction.js';
|
|
18
|
+
export { warnOnce } from './diagnostics/warn-once.js';
|
|
17
19
|
export { createQtiParser, createMapperForVersion, createAttributeMapperForVersion, isQti2, isQti3, type QtiParserResult, type CreateParserOptions, } from './parser-factory.js';
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { QTI2_PROMPT_BEARING_INTERACTIONS, QTI3_ATTRIBUTE_TO_QTI2, } from './ele
|
|
|
6
6
|
export { convertQti2ToQti3, QTI3_ASI_SCHEMA_LOCATION, QTI3_NAMESPACE, QTI3_RPTEMPLATE_BASE, } from './element-mapper/qti2-to-qti3.js';
|
|
7
7
|
// Feedback and template visibility
|
|
8
8
|
export { isShownByIdentifier, matchesIdentifier } from './feedback/visibility.js';
|
|
9
|
+
export { cleanFeedbackText, processFeedbackInline, DEFAULT_FEEDBACK_OUTCOME_ID, DEFAULT_FEEDBACK_TRANSFORMS, } from './feedback/feedback-text.js';
|
|
9
10
|
// Version detection
|
|
10
11
|
export { detectQtiVersion } from './version-detection/detectQtiVersion.js';
|
|
11
12
|
// XML attribute utilities
|
|
@@ -16,6 +17,7 @@ export { createSvelteMountController, } from './dom/svelteMountController.js';
|
|
|
16
17
|
export { touchDrag, touchDragStyles } from './dom/touchDrag.js';
|
|
17
18
|
// Writing direction
|
|
18
19
|
export { languageDirection } from './language/direction.js';
|
|
20
|
+
export { warnOnce } from './diagnostics/warn-once.js';
|
|
19
21
|
// Parser factory
|
|
20
22
|
export { createQtiParser, createMapperForVersion, createAttributeMapperForVersion, isQti2, isQti3, } from './parser-factory.js';
|
|
21
23
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,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DAAoD;AACpD,OAAO,EAAE,WAAW,EAA2B,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EACN,2BAA2B,GAG3B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEhE,oBAAoB;AACpB,OAAO,EAAE,iBAAiB,EAAsB,MAAM,yBAAyB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAEtD,iBAAiB;AACjB,OAAO,EACN,eAAe,EACf,sBAAsB,EACtB,+BAA+B,EAC/B,MAAM,EACN,MAAM,GAGN,MAAM,qBAAqB,CAAC","sourcesContent":["// Element name mapping\nexport type { ElementNameMapper } from './element-mapper/ElementNameMapper.js';\nexport { Qti2xElementNameMapper } from './element-mapper/Qti2xElementNameMapper.js';\nexport { Qti3ElementNameMapper } from './element-mapper/Qti3ElementNameMapper.js';\n\n// Attribute name mapping\nexport type { AttributeNameMapper } from './element-mapper/AttributeNameMapper.js';\nexport { Qti2xAttributeNameMapper } from './element-mapper/Qti2xAttributeNameMapper.js';\nexport { Qti3AttributeNameMapper } from './element-mapper/Qti3AttributeNameMapper.js';\nexport {\n\tQTI2_PROMPT_BEARING_INTERACTIONS,\n\tQTI3_ATTRIBUTE_TO_QTI2,\n} from './element-mapper/qti3-attribute-map.js';\nexport {\n\tconvertQti2ToQti3,\n\tQTI3_ASI_SCHEMA_LOCATION,\n\tQTI3_NAMESPACE,\n\tQTI3_RPTEMPLATE_BASE,\n} from './element-mapper/qti2-to-qti3.js';\n\n// Feedback and template visibility\nexport { isShownByIdentifier, matchesIdentifier } from './feedback/visibility.js';\nexport {\n\tcleanFeedbackText,\n\tprocessFeedbackInline,\n\tDEFAULT_FEEDBACK_OUTCOME_ID,\n\tDEFAULT_FEEDBACK_TRANSFORMS,\n\ttype FeedbackTransform,\n\ttype ProcessFeedbackInlineOptions,\n} from './feedback/feedback-text.js';\n\n// Version detection\nexport { detectQtiVersion } from './version-detection/detectQtiVersion.js';\nexport type { QtiVersion } from './version-detection/detectQtiVersion.js';\n\n// XML attribute utilities\nexport {\n\tgetAttribute,\n\tgetBooleanAttribute,\n\tgetNumberAttribute,\n\thasAttribute,\n\ttoCamelCase,\n\ttoKebabCase,\n} from './xml/attributes.js';\n\n// DOM utility helpers shared across player packages\nexport { assignProps, type AssignPropsOptions } from './dom/assignProps.js';\nexport {\n\tcreateSvelteMountController,\n\ttype SvelteMountController,\n\ttype SvelteMountControllerOptions,\n} from './dom/svelteMountController.js';\nexport { touchDrag, touchDragStyles } from './dom/touchDrag.js';\n\n// Writing direction\nexport { languageDirection, type TextDirection } from './language/direction.js';\nexport { warnOnce } from './diagnostics/warn-once.js';\n\n// Parser factory\nexport {\n\tcreateQtiParser,\n\tcreateMapperForVersion,\n\tcreateAttributeMapperForVersion,\n\tisQti2,\n\tisQti3,\n\ttype QtiParserResult,\n\ttype CreateParserOptions,\n} from './parser-factory.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-qti/qti-common",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"node-html-parser": "^9.0.
|
|
6
|
+
"node-html-parser": "^9.0.5"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@types/bun": "^1.4.
|
|
9
|
+
"@types/bun": "^1.4.4",
|
|
10
10
|
"@xmldom/xmldom": "^0.9.12",
|
|
11
11
|
"typescript": "^5.9.3"
|
|
12
12
|
},
|