From a7862fc98d14a768f9496c1c59d160ecfe20ea47 Mon Sep 17 00:00:00 2001 From: "C. Fuhrman" Date: Sun, 15 Sep 2024 10:56:37 -0400 Subject: [PATCH] fixes #91 --- .../components/GiftTemplate/TextType.test.ts | 80 +++++++++++++++++++ .../GiftTemplate/GIFTTemplatePreview.tsx | 2 + .../GiftTemplate/templates/TextType.ts | 13 +++ 3 files changed, 95 insertions(+) create mode 100644 client/src/__tests__/components/GiftTemplate/TextType.test.ts diff --git a/client/src/__tests__/components/GiftTemplate/TextType.test.ts b/client/src/__tests__/components/GiftTemplate/TextType.test.ts new file mode 100644 index 0000000..9441b58 --- /dev/null +++ b/client/src/__tests__/components/GiftTemplate/TextType.test.ts @@ -0,0 +1,80 @@ +// TextType.test.ts + +import { TextFormat } from "gift-pegjs"; +import TextType from "../../../components/GiftTemplate/templates/TextType"; + +describe('TextType', () => { + it('should format text with basic characters correctly', () => { + const input: TextFormat = { + text: 'Hello, world! 5 > 3, right?', + format: 'plain' + }; + const expectedOutput = 'Hello, world! 5 > 3, right?'; + expect(TextType({ text: input })).toBe(expectedOutput); + }); + + it('should format text with newlines correctly', () => { + const input: TextFormat = { + text: 'Hello,\nworld!\n5 > 3, right?', + format: 'plain' + }; + const expectedOutput = 'Hello,
world!
5 > 3, right?'; + expect(TextType({ text: input })).toBe(expectedOutput); + }); + + it('should format text with LaTeX correctly', () => { + const input: TextFormat = { + text: '$$E=mc^2$$', + format: 'plain' + }; + // the following expected output is a bit long, but it's a good way to test the output. + // You could do a "snapshot" test if you prefer, but it's less readable. + // Hint -- if the output changes because of a change in the code or library, you can update + // by running the test and copying the "Received string:" in jest output + // when it fails (assuming the output is correct) + const expectedOutput = 'E=mc2E=mc^2E=mc2'; + expect(TextType({ text: input })).toContain(expectedOutput); + }); + + it('should format text with two equations (inline and separate) correctly', () => { + const input: TextFormat = { + text: '$a + b = c$ ? $$E=mc^2$$', + format: 'plain' + }; + // hint: katex-display is the class that indicates a separate equation + const expectedOutput = 'a+b=ca + b = ca+b=c ? E=mc2E=mc^2E=mc2'; + expect(TextType({ text: input })).toContain(expectedOutput); + }); + + it('should format text with a katex matrix correctly', () => { + const input: TextFormat = { + text: `Donnez le déterminant de la matrice suivante.$$\\begin\{pmatrix\} + a&b \\\\ + c&d +\\end\{pmatrix\}`, + format: 'plain' + }; + const expectedOutput = 'Donnez le déterminant de la matrice suivante.\\begin{pmatrix}
a&b \\\\
c&d
\\end{pmatrix}'; + expect(TextType({ text: input })).toContain(expectedOutput); + }); + + it('should format text with Markdown correctly', () => { + const input: TextFormat = { + text: '**Bold**', + format: 'markdown' + }; + const expectedOutput = 'Bold'; + expect(TextType({ text: input })).toContain(expectedOutput); + }); + + it('should format plain text correctly', () => { + const input: TextFormat = { + text: 'Just plain text', + format: 'plain' + }; + const expectedOutput = 'Just plain text'; + expect(TextType({ text: input })).toBe(expectedOutput); + }); + + // Add more tests for other formats if needed +}); diff --git a/client/src/components/GiftTemplate/GIFTTemplatePreview.tsx b/client/src/components/GiftTemplate/GIFTTemplatePreview.tsx index 3aa83f7..4202b80 100644 --- a/client/src/components/GiftTemplate/GIFTTemplatePreview.tsx +++ b/client/src/components/GiftTemplate/GIFTTemplatePreview.tsx @@ -21,6 +21,8 @@ const GIFTTemplatePreview: React.FC = ({ try { let previewHTML = ''; questions.forEach((giftQuestion) => { + // TODO : afficher un message que les images spécifiées par sont dépréciées et qu'il faut utiliser [markdown] et la syntaxe ![alt](url) + // const isImage = item.includes(']+>/i); diff --git a/client/src/components/GiftTemplate/templates/TextType.ts b/client/src/components/GiftTemplate/templates/TextType.ts index e949092..3d6b31d 100644 --- a/client/src/components/GiftTemplate/templates/TextType.ts +++ b/client/src/components/GiftTemplate/templates/TextType.ts @@ -16,12 +16,25 @@ function formatLatex(text: string): string { ); } +/** + * Formats text based on the format specified in the text object + * @param text Text object to format + * @returns Formatted text + * @throws Error if the text format is not supported + * @see TextFormat + * @see TextTypeOptions + * @see TemplateOptions + * @see formatLatex + * @see marked + * @see katex + */ export default function TextType({ text }: TextTypeOptions): string { const formatText = formatLatex(text.text.trim()); // latex needs pure "&", ">", etc. Must not be escaped switch (text.format) { case 'moodle': case 'plain': + // Replace newlines with
tags return formatText.replace(/(?:\r\n|\r|\n)/g, '
'); case 'html': // Strip outer paragraph tags (not a great approach with regex)