diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.test.ts b/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.test.ts index f04c638dea5..491d8a93a36 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.test.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.test.ts @@ -10,7 +10,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { getTextFromHtmlString } from './BlockEditorUtils'; +import { + getHtmlStringFromMarkdownString, + getTextFromHtmlString, +} from './BlockEditorUtils'; describe('getTextFromHtmlString', () => { it('should return empty string when input is undefined', () => { @@ -72,3 +75,45 @@ describe('getTextFromHtmlString', () => { expect(getTextFromHtmlString(input)).toBe(output); }); }); + +describe('getHtmlStringFromMarkdownString', () => { + it('should return the same string if input is already HTML', () => { + const input = '
Hello World
'; + + expect(getHtmlStringFromMarkdownString(input)).toBe(input); + }); + + it('should convert markdown to HTML', () => { + const input = 'Hello **World**'; + const expectedOutput = 'Hello World
'; + + expect(getHtmlStringFromMarkdownString(input)).toBe(expectedOutput); + }); + + it('should handle empty string', () => { + expect(getHtmlStringFromMarkdownString('')).toBe(''); + }); + + it('should preserve special characters in markdown', () => { + const input = 'Hello & World! @ #$%^'; + const expectedOutput = 'Hello & World! @ #$%^
'; + + expect(getHtmlStringFromMarkdownString(input)).toBe(expectedOutput); + }); + + it('should handle complex markdown structure', () => { + const input = ` + ## Demo Title + Small Subtitle. + - Item 1 + - Item 2 + `; + const expectedOutput = ` +##DemoTitleSmallSubtitle.-Item1-Item2
+ `;
+
+ expect(getHtmlStringFromMarkdownString(input).replace(/\s+/g, '')).toBe(
+ expectedOutput.replace(/\s+/g, '')
+ );
+ });
+});
diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.ts b/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.ts
index 1019c519b1f..091647a06e9 100644
--- a/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.ts
+++ b/openmetadata-ui/src/main/resources/ui/src/utils/BlockEditorUtils.ts
@@ -139,6 +139,12 @@ const _convertMarkdownStringToHtmlString = new Showdown.Converter({
ellipsis: false,
});
+export const getHtmlStringFromMarkdownString = (content: string) => {
+ return isHTMLString(content)
+ ? content
+ : _convertMarkdownStringToHtmlString.makeHtml(content);
+};
+
/**
* Set the content of the editor
* @param editor The editor instance
@@ -146,7 +152,7 @@ const _convertMarkdownStringToHtmlString = new Showdown.Converter({
*/
export const setEditorContent = (editor: Editor, newContent: string) => {
// Convert the markdown string to an HTML string
- const htmlString = _convertMarkdownStringToHtmlString.makeHtml(newContent);
+ const htmlString = getHtmlStringFromMarkdownString(newContent);
editor.commands.setContent(htmlString);