diff --git a/src/components/quiz-question/multi-choice/multi-choice.tsx b/src/components/quiz-question/multi-choice/multi-choice.tsx index 2887490..b19efd8 100644 --- a/src/components/quiz-question/multi-choice/multi-choice.tsx +++ b/src/components/quiz-question/multi-choice/multi-choice.tsx @@ -1,6 +1,6 @@ import {h} from 'preact'; import {useCallback, useEffect, useRef} from 'preact/hooks'; -import {makeQuestionLabels} from '../../../utils'; +import {makeQuestionLabels, wrapLinksWithTags} from '../../../utils'; import {QuestionProps, QuizTranslates} from '../../../types'; import {QuestionAddons} from '../question-addons'; import {A11yWrapper} from '@playkit-js/common/dist/hoc/a11y-wrapper'; @@ -85,7 +85,7 @@ export const MultiChoice = withText(translates)(
{`${otherProps.questionLabel} #${questionIndex}:`} - {question} +
{hint && }
diff --git a/src/components/quiz-question/open-question/open-question.tsx b/src/components/quiz-question/open-question/open-question.tsx index 2fe1278..711dcf5 100644 --- a/src/components/quiz-question/open-question/open-question.tsx +++ b/src/components/quiz-question/open-question/open-question.tsx @@ -3,6 +3,7 @@ import {useCallback, useEffect, useRef} from 'preact/hooks'; import {QuestionProps, QuizTranslates} from '../../../types'; import {QuestionAddons} from '../question-addons'; import * as styles from './open-question.scss'; +import {wrapLinksWithTags} from '../../../utils'; const {withText, Text} = KalturaPlayer.ui.preacti18n; @@ -37,7 +38,7 @@ export const OpenQuestion = withText(translates)(
{`${otherProps.questionLabel} #${questionIndex}:`} - {question} +
{hint && }
diff --git a/src/components/quiz-question/reflection-point/reflection-point.tsx b/src/components/quiz-question/reflection-point/reflection-point.tsx index be0a36c..3932a7e 100644 --- a/src/components/quiz-question/reflection-point/reflection-point.tsx +++ b/src/components/quiz-question/reflection-point/reflection-point.tsx @@ -2,6 +2,7 @@ import {h} from 'preact'; import {useEffect, useRef} from 'preact/hooks'; import {QuestionProps, QuizTranslates} from '../../../types'; import * as styles from './reflection-point.scss'; +import {wrapLinksWithTags} from '../../../utils'; const {withText, Text} = KalturaPlayer.ui.preacti18n; @@ -22,7 +23,7 @@ export const ReflectionPoint = withText(translates)(({question, questionIndex, .
{`${otherProps.questionLabel}# ${questionIndex}, ${otherProps.reflectionPoint}:`} - {question} +
); diff --git a/src/components/quiz-question/true-false/true-false.tsx b/src/components/quiz-question/true-false/true-false.tsx index 1fafb08..e4c638a 100644 --- a/src/components/quiz-question/true-false/true-false.tsx +++ b/src/components/quiz-question/true-false/true-false.tsx @@ -4,7 +4,7 @@ import {QuestionProps, QuizTranslates} from '../../../types'; import {QuestionAddons} from '../question-addons'; import {A11yWrapper} from '@playkit-js/common/dist/hoc/a11y-wrapper'; import * as styles from './true-false.scss'; - +import {wrapLinksWithTags} from '../../../utils'; const {withText, Text} = KalturaPlayer.ui.preacti18n; const translates = (): QuizTranslates => { @@ -58,7 +58,7 @@ export const TrueFalse = withText(translates)(
{`${otherProps.questionLabel} #${questionIndex}:`} - {question} +
{hint && }
diff --git a/src/utils.ts b/src/utils.ts index 782429e..b34a91d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,3 +19,36 @@ export const makeQuestionLabels = () => Array.from(Array(26)) .map((e, i) => i + 'A'.charCodeAt(0)) .map(x => String.fromCharCode(x)); // ["A", "B", "C", ... , "Z"] + + +/** + * Process the input text to wrap URLs in HTML anchor tags (). + * + * This function performs two main tasks: + * 1. It converts patterns of the form `[title|url]` into clickable links with the provided title. + * 2. It wraps standalone URLs in clickable links. + * + * Example: + * - Input: "[my title | http://example.com]" + * Output: "my title" + * + * - Input: "http://example.com" + * Output: "http://example.com" + * + * @param text - The input string containing potential URLs or patterns to be converted. + * @returns The processed string with URLs wrapped in tags. + */ +export const wrapLinksWithTags = (text: string): string => { + // Replace the pattern [title|url] with tags + const wrapLinksWithTitle = text.replace(/\[(\s+)?([^\|\]]*)(\|)(\s+)?((https?|ftps?):\/\/[^\s\]]+)(\s+)?(\])/gi, (url: string): string => { + const linkInfo = url.slice(1, -1).split('|'); + const title = linkInfo[0].trim(); + const href = linkInfo[1].trim(); + return `${title}`; + }); + + // Replace standalone URLs with tags + return wrapLinksWithTitle.replace(/((https?|ftps?):\/\/[^\s"<\]]+)(?![^<>]*>|[^"]*<\/a>)/gi, (url: string): string => { + return `${url}`; + }); +};