diff --git a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRederer.tsx b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRederer.tsx index 26e2eb2b206..9240af161c2 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRederer.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRederer.tsx @@ -24,6 +24,7 @@ import { MdNode, } from '@toast-ui/editor'; import { t } from 'i18next'; +import katex from 'katex'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; import { ReactComponent as CopyIcon } from '../../../../assets/svg/icon-copy.svg'; @@ -241,4 +242,39 @@ export const customHTMLRenderer: CustomHTMLRenderer = { { type: 'closeTag', tagName: 'section', outerNewLine: true }, ]; }, + + latex(node) { + const content = katex.renderToString(node.literal ?? '', { + throwOnError: false, + output: 'mathml', + }); + + return [ + { type: 'openTag', tagName: 'div', outerNewLine: true }, + { type: 'html', content: content }, + { type: 'closeTag', tagName: 'div', outerNewLine: true }, + ]; + }, +}; + +export const replaceLatex = (content: string) => { + try { + const latexPattern = /\$\$latex[\s\S]*?\$\$/g; + const latexContentPattern = /\$\$latex\s*([\s\S]*?)\s*\$\$/g; + + return content.replace(latexPattern, (latex) => { + const matches = [...latex.matchAll(latexContentPattern)]; + + if (matches.length === 0) { + return latex; + } + + return katex.renderToString(matches[0][1] ?? '', { + throwOnError: false, + output: 'mathml', + }); + }); + } catch (error) { + return content; + } }; diff --git a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRenderer.test.ts b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRenderer.test.ts new file mode 100644 index 00000000000..1359f4c69d8 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/CustomHtmlRederer/CustomHtmlRenderer.test.ts @@ -0,0 +1,53 @@ +/* + * Copyright 2024 Collate. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* eslint-disable max-len */ +import { replaceLatex } from './CustomHtmlRederer'; + +jest.mock('katex', () => ({ + renderToString: jest.fn().mockReturnValue('latex content'), +})); + +describe('replaceLatex', () => { + it('returns the original string if no LaTeX is present', () => { + const input = 'This is a test string without LaTeX.'; + const output = replaceLatex(input); + + expect(output).toBe(input); + }); + + it('replaces LaTeX content correctly', () => { + const content = + 'This dimension table contains a row for each channel or app that your customers use to create orders. Some examples of these include Facebook and Online Store. You can join this table with the sales table to measure channel performance.\n\n$$latex\n\text{$dfrac{NetSales}{Quantity Invoiced}=Average Price, :$ If QuantityInvoiced is Null then,$: AveragePrice=Null$}\n$$'; + + const output = replaceLatex(content); + + expect(output).toContain('latex content'); + + expect(output).not.toContain('$$latex'); + }); + + it('replaces multiple LaTeX contents correctly', () => { + const content = `$$latex \\frac{a}{b}$$ $$latex \\frac{c}{d}$$`; + const output = replaceLatex(content); + + expect(output).toContain('latex content latex content'); + }); + + it('returns the original LaTeX string if it is malformed', () => { + const malformedLatex = '$$latex \\frac{a}{b}'; + const input = `This is malformed: ${malformedLatex}`; + const output = replaceLatex(input); + + expect(output).toContain(malformedLatex); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditor.tsx b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditor.tsx index fc3bd969b64..f4153666068 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditor.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditor.tsx @@ -24,6 +24,7 @@ import React, { useState, } from 'react'; import i18n from '../../../utils/i18next/LocalUtil'; +import { customHTMLRenderer } from './CustomHtmlRederer/CustomHtmlRederer'; import { EDITOR_TOOLBAR_ITEMS } from './EditorToolBar'; import './rich-text-editor.less'; import { editorRef, RichTextEditorProp } from './RichTextEditor.interface'; @@ -103,6 +104,7 @@ const RichTextEditor = forwardRef( dir={i18n.dir()}> ( previewHighlight={previewHighlight} previewStyle={previewStyle} ref={richTextEditorRef} + customHTMLRenderer={customHTMLRenderer} toolbarItems={[EDITOR_TOOLBAR_ITEMS]} useCommandShortcut={useCommandShortcut} onChange={onChangeHandler} diff --git a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditorPreviewer.tsx b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditorPreviewer.tsx index b1b5282b3d4..edab1db6c92 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditorPreviewer.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditorPreviewer.tsx @@ -20,7 +20,10 @@ import { useTranslation } from 'react-i18next'; import { DESCRIPTION_MAX_PREVIEW_CHARACTERS } from '../../../constants/constants'; import { formatContent, isHTMLString } from '../../../utils/BlockEditorUtils'; import { getTrimmedContent } from '../../../utils/CommonUtils'; -import { customHTMLRenderer } from './CustomHtmlRederer/CustomHtmlRederer'; +import { + customHTMLRenderer, + replaceLatex, +} from './CustomHtmlRederer/CustomHtmlRederer'; import './rich-text-editor-previewer.less'; import { PreviewerProp } from './RichTextEditor.interface'; @@ -112,7 +115,7 @@ const RichTextEditorPreviewer = ({