@khanacademy/perseus-core 0.0.0-PR3091-20251204213510 → 0.0.0-PR3092-20251204233001
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/es/index.js +5 -12
- package/dist/es/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +9 -14
- package/dist/index.js.map +1 -1
- package/dist/utils/generators/expression-widget-generator.d.ts +4 -0
- package/dist/utils/generators/free-response-widget-generator.d.ts +3 -0
- package/package.json +1 -1
- package/dist/utils/extract-perseus-ai-data.d.ts +0 -39
package/dist/index.d.ts
CHANGED
|
@@ -137,6 +137,7 @@ export { default as PerseusFeatureFlags, isFeatureOn } from "./feature-flags";
|
|
|
137
137
|
export { traverse } from "./traversal";
|
|
138
138
|
export { isItemAccessible, violatingWidgets } from "./accessibility";
|
|
139
139
|
export { isLabeledSVG, getRealImageUrl, getBaseUrl, getSvgUrl, getDataUrl, getImageSizeModern, } from "./utils/util.graphie";
|
|
140
|
+
export { generateExpressionOptions, generateExpressionAnswerForm, generateExpressionWidget, } from "./utils/generators/expression-widget-generator";
|
|
141
|
+
export { generateFreeResponseOptions, generateFreeResponseWidget, } from "./utils/generators/free-response-widget-generator";
|
|
140
142
|
export { generateImageOptions, generateImageWidget, } from "./utils/generators/image-widget-generator";
|
|
141
143
|
export { generateVideoWidget } from "./utils/generators/video-widget-generator";
|
|
142
|
-
export { getAnswersFromWidgets, injectWidgets, getPerseusAIData, } from "./utils/extract-perseus-ai-data";
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,6 @@ require('tiny-invariant');
|
|
|
7
7
|
var KAS = require('@khanacademy/kas');
|
|
8
8
|
var perseusUtils = require('@khanacademy/perseus-utils');
|
|
9
9
|
var pureMarkdown = require('@khanacademy/pure-markdown');
|
|
10
|
-
var wonderStuffCore = require('@khanacademy/wonder-stuff-core');
|
|
11
10
|
|
|
12
11
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
13
12
|
|
|
@@ -414,19 +413,13 @@ const noop=function(){};const deepCallbackFor=function(contentCallback,widgetCal
|
|
|
414
413
|
|
|
415
414
|
function violatingWidgets(itemData){const widgetTypes=[];traverse(itemData.question,null,function(info){if(info.type&&!isAccessible(info.type,info.options)){widgetTypes.push(info.type);}});return [...new Set(widgetTypes)]}function isItemAccessible(itemData){const ast=pureMarkdown.parse(itemData.question.content);const widgetIdsInUse=new Set;let hasInaccessibleImage=false;pureMarkdown.traverseContent(ast,node=>{if(node.type==="image"&&(node.alt==null||node.alt==="")){hasInaccessibleImage=true;return}if(node.type==="widget"){widgetIdsInUse.add(node.id);}});if(hasInaccessibleImage){return false}const itemDataWithOnlyActiveWidgets={...itemData,question:{...itemData.question,widgets:Object.fromEntries(Object.entries(itemData.question.widgets).filter(([id])=>widgetIdsInUse.has(id)))}};return violatingWidgets(itemDataWithOnlyActiveWidgets).length===0}
|
|
416
415
|
|
|
417
|
-
function
|
|
416
|
+
function generateExpressionOptions(options){return {...expressionWidgetLogic.defaultWidgetOptions,...options}}function generateExpressionAnswerForm(answerFormOptions){return {value:"",form:false,simplify:false,considered:"wrong",...answerFormOptions}}function generateExpressionWidget(expressionWidgetProperties){return {type:"expression",graded:true,version:{major:0,minor:0},static:false,alignment:"default",options:generateExpressionOptions(),...expressionWidgetProperties}}
|
|
418
417
|
|
|
419
|
-
function
|
|
418
|
+
function generateFreeResponseOptions(options){return {...freeResponseWidgetLogic.defaultWidgetOptions,...options}}function generateFreeResponseWidget(freeResponseWidgetProperties){return {type:"free-response",graded:true,version:{major:0,minor:0},static:false,alignment:"default",options:generateFreeResponseOptions(),...freeResponseWidgetProperties}}
|
|
420
419
|
|
|
421
|
-
|
|
422
|
-
| --- | --- |`;const tableRows=left.map((leftItem,index)=>{return `| ${leftItem} | ${right[index]} |`});const table=[tableHeader,...tableRows].join("\n");answers.push(table);}break}});return answers}function injectWidgets(content,widgets,widgetProps){if(!content){return ""}if(!widgets){return content}let context=content;wonderStuffCore.keys(widgets).forEach(widgetID=>{const widget=widgets[widgetID];if(!widget.options){return}switch(widget.type){case "radio":const radio=widget;const radioProps=widgetProps?.[widgetID];if(radio.options?.choices?.length){let radioContext=joinOptionContents(radioProps?radioProps.choices.map(({content},i)=>({content:`Option ${toOptionLetter(i)}: ${content}`})):radio.options.choices);if(!radioProps&&radio.options?.randomize){radioContext+="\nThose options are displayed in a different order to the user. If the user says the letter, number, or ordinal number, always ask them clarify which option they are referring to.\n";}context=context.replace(`[[☃ ${widgetID}]]`,radioContext);}break;case "image":const image=widget;if(image.options?.alt){context=context.replace(`[[☃ ${widgetID}]]`,`<img id="${widgetID}" alt="${image.options.alt}">`);}break;case "label-image":const labelImage=widget;if(labelImage.options?.imageAlt){context=context.replace(`[[☃ ${widgetID}]]`,`[An image with dots that user needs to label. Label choices: [${labelImage.options.choices.join(", ")}]. Image alt text: ${labelImage.options?.imageAlt??""}]`);}break;case "explanation":const explanation=widget;if(explanation.options?.explanation){context=context.replace(`[[☃ ${widgetID}]]`,injectWidgets(explanation.options.explanation,explanation.options.widgets));}break;case "passage":const passage=widget;if(passage.options?.passageTitle||passage.options?.passageText){const{passageTitle,passageText}=passage.options;context=context.replace(`[[☃ ${widgetID}]]`,`# ${passageTitle}
|
|
420
|
+
function generateImageOptions(options){const defaultImageOptions={backgroundImage:{}};return {...defaultImageOptions,...options}}function generateImageWidget(imageWidgetProperties){return {type:"image",graded:true,version:{major:0,minor:0},static:false,alignment:"default",options:generateImageOptions({}),...imageWidgetProperties}}
|
|
423
421
|
|
|
424
|
-
|
|
425
|
-
Items:
|
|
426
|
-
${items.join("\n")}
|
|
427
|
-
`);}break;case "dropdown":const dropdown=widget;if(dropdown.options?.choices){const choices=dropdown.options.choices.map(choice=>choice.content);context=context.replace(`[[☃ ${widgetID}]]`,`[${choices.join(" | ")}]`);}break;case "definition":const definition=widget;if(definition.options?.togglePrompt){context=context.replace(`[[☃ ${widgetID}]]`,definition.options.togglePrompt);}break;case "orderer":const orderer=widget;if(orderer.options?.options){context=context.replace(`[[☃ ${widgetID}]]`,joinOptionContents(orderer.options.options));}break;case "sorter":const sorter=widget;if(sorter.options?.correct){const choices=sorter.options.correct;context=context.replace(`[[☃ ${widgetID}]]`,`[${choices.join(" | ")}]`);}break;case "interactive-graph":const interactiveGraph=widget;if(interactiveGraph.options?.range.length===2){const[x,y]=interactiveGraph.options.range;context=context.replace(`[[☃ ${widgetID}]]`,`[Graph with an x range of ${x[0]} to ${x[1]} and y range of ${y[0]} to ${y[1]}]`);}break;case "number-line":const numberLine=widget;if(numberLine.options?.range.length===2&&numberLine.options?.tickStep&&numberLine.options?.initialX){const[min,max]=numberLine.options.range;const step=numberLine.options.tickStep;const initialPosition=numberLine.options.initialX;context=context.replace(`[[☃ ${widgetID}]]`,`[Number line with a range of ${min} to ${max}, a step of ${step}, and an initial position of ${initialPosition}]`);}break;case "matrix":const matrix=widget;if(matrix.options?.matrixBoardSize.length===2){const[rows,columns]=matrix.options.matrixBoardSize;context=context.replace(`[[☃ ${widgetID}]]`,`[Matrix with ${rows} rows and ${columns} columns. The user can click on each cell to enter a value]`);}break;case "matcher":const matcher=widget;if(matcher.options?.left&&matcher.options?.right){const{left,right}=matcher.options;const[leftHeader,rightHeader]=matcher.options.labels;const tableHeader=`| ${leftHeader} | ${rightHeader} |
|
|
428
|
-
| --- | --- |`;const tableRows=left.map((leftItem,index)=>{return `| ${leftItem} | ${right[index]} |`});const table=[tableHeader,...tableRows].join("\n");const matcherWidgetExplanation="The user needs to move items in the right column to match the correct option on the left. The order of items on the right side will be different from what the user sees.";context=context.replace(`[[☃ ${widgetID}]]`,`${matcherWidgetExplanation}
|
|
429
|
-
${table}`);}break;case "numeric-input":case "input-number":case "expression":context=context.replace(`[[☃ ${widgetID}]]`,"?");break;default:context=context.replace(`[[☃ ${widgetID}]]`,`[[Unsupported ${widget.type} widget: Explain to the user that you are unable to understand the content in this widget and ask them to describe it.]]`);}});return context}function getPerseusAIData(perseusItem){const answers=getAnswersFromWidgets(perseusItem.question.widgets);const hints=perseusItem.hints.map(hint=>injectWidgets(hint.content,hint.widgets));return {answers,hints}}
|
|
422
|
+
function generateVideoWidget(videoWidgetProperties){return {type:"video",graded:true,version:{major:0,minor:0},static:false,alignment:"default",options:{location:""},...videoWidgetProperties}}
|
|
430
423
|
|
|
431
424
|
registerCoreWidgets();
|
|
432
425
|
|
|
@@ -454,12 +447,16 @@ exports.dropdownLogic = dropdownWidgetLogic;
|
|
|
454
447
|
exports.explanationLogic = explanationWidgetLogic;
|
|
455
448
|
exports.expressionLogic = expressionWidgetLogic;
|
|
456
449
|
exports.freeResponseLogic = freeResponseWidgetLogic;
|
|
450
|
+
exports.generateExpressionAnswerForm = generateExpressionAnswerForm;
|
|
451
|
+
exports.generateExpressionOptions = generateExpressionOptions;
|
|
452
|
+
exports.generateExpressionWidget = generateExpressionWidget;
|
|
453
|
+
exports.generateFreeResponseOptions = generateFreeResponseOptions;
|
|
454
|
+
exports.generateFreeResponseWidget = generateFreeResponseWidget;
|
|
457
455
|
exports.generateImageOptions = generateImageOptions;
|
|
458
456
|
exports.generateImageWidget = generateImageWidget;
|
|
459
457
|
exports.generateTestPerseusItem = generateTestPerseusItem;
|
|
460
458
|
exports.generateTestPerseusRenderer = generateTestPerseusRenderer;
|
|
461
459
|
exports.generateVideoWidget = generateVideoWidget;
|
|
462
|
-
exports.getAnswersFromWidgets = getAnswersFromWidgets;
|
|
463
460
|
exports.getBaseUrl = getBaseUrl;
|
|
464
461
|
exports.getCSProgramPublicWidgetOptions = getCSProgramPublicWidgetOptions;
|
|
465
462
|
exports.getCategorizerPublicWidgetOptions = getCategorizerPublicWidgetOptions;
|
|
@@ -483,7 +480,6 @@ exports.getMatrixSize = getMatrixSize;
|
|
|
483
480
|
exports.getNumberLinePublicWidgetOptions = getNumberLinePublicWidgetOptions;
|
|
484
481
|
exports.getNumericInputPublicWidgetOptions = getNumericInputPublicWidgetOptions;
|
|
485
482
|
exports.getOrdererPublicWidgetOptions = getOrdererPublicWidgetOptions;
|
|
486
|
-
exports.getPerseusAIData = getPerseusAIData;
|
|
487
483
|
exports.getPlotterPublicWidgetOptions = getPlotterPublicWidgetOptions;
|
|
488
484
|
exports.getRadioPublicWidgetOptions = getRadioPublicWidgetOptions;
|
|
489
485
|
exports.getRealImageUrl = getRealImageUrl;
|
|
@@ -498,7 +494,6 @@ exports.grapherLogic = grapherWidgetLogic;
|
|
|
498
494
|
exports.groupLogic = groupWidgetLogic;
|
|
499
495
|
exports.iframeLogic = iframeWidgetLogic;
|
|
500
496
|
exports.imageLogic = imageWidgetLogic;
|
|
501
|
-
exports.injectWidgets = injectWidgets;
|
|
502
497
|
exports.inputNumberLogic = inputNumberWidgetLogic;
|
|
503
498
|
exports.interactionLogic = interactionWidgetLogic;
|
|
504
499
|
exports.interactiveGraphLogic = interactiveGraphWidgetLogic;
|