Skip to content

Commit

Permalink
refactor: strip html in msg parsing for TTS, fix plaintext to copy
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Oct 11, 2024
1 parent 754f6a8 commit 4d95f39
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/components/ChatBubble/ChatBubble.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useLayoutEffect, useRef, useState } from 'react';
import React, { useLayoutEffect, useState } from 'react';
import cx from 'classnames';
import {
ExpertReference,
Expand All @@ -18,13 +18,12 @@ import FeedbackButtons from '../FeedbackButtons/FeedbackButtons';
import { useTranslation } from 'react-i18next';
import { marked } from 'marked';
import DOMPurify from 'dompurify';
import { cleanUrl } from '../../helpers/utils';
import Button from '../ui/Button';
import QuestionHelp from '../icons/QuestionHelp';
import Copy from '../icons/Copy';
import Code from '../icons/Code';
import WhyThisAnswer from '../WhyThisAnswer/WhyThisAnswer';
import { stripEmojis, escapeHTML, stripMarkdown } from '../../helpers/utils';
import { cleanUrl, stripHTML, stripOutputTags } from '../../helpers/utils';

import markedLinkifyIt from 'marked-linkify-it';
import markedKatex from 'marked-katex-extension';
Expand Down Expand Up @@ -149,7 +148,7 @@ const ChatBubble: React.FC<Props> = ({

const plainText = message.fromUser
? text
: escapeHTML(stripMarkdown(stripEmojis(text)));
: stripHTML(stripOutputTags(renderedText));

useLayoutEffect(() => {
if (typeof window !== 'undefined' && !message.fromUser) {
Expand Down
9 changes: 5 additions & 4 deletions src/components/MemoriWidget/MemoriWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import React, {
useCallback,
CSSProperties,
useRef,
useContext,
} from 'react';
import { useTranslation } from 'react-i18next';
import memoriApiClient from '@memori.ai/memori-api-client';
Expand Down Expand Up @@ -74,6 +73,7 @@ import {
escapeHTML,
stripMarkdown,
stripOutputTags,
stripHTML,
} from '../../helpers/utils';
import { anonTag, uiLanguages } from '../../helpers/constants';
import { getErrori18nKey } from '../../helpers/error';
Expand Down Expand Up @@ -1972,8 +1972,9 @@ const MemoriWidget = ({
});
};

//if there is an emotion, remove the tag <output > from the text
const textToSpeak = text.replace(/<output>(.*?)<\/output>/g, '$1');
const textToSpeak = escapeHTML(
stripMarkdown(stripEmojis(stripHTML(stripOutputTags(text))))
);

speechSynthesizer.speakSsmlAsync(
`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="${getCultureCodeByLanguage(
Expand All @@ -1983,7 +1984,7 @@ const MemoriWidget = ({
)}"><mstts:express-as style="${getAzureStyleForEmotion(
emotion
)}"><s>${replaceTextWithPhonemes(
escapeHTML(stripMarkdown(stripEmojis(stripOutputTags(textToSpeak)))),
textToSpeak,
userLang.toLowerCase()
)}</s></mstts:express-as></voice></speak>`,
result => {
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const stripEmojis = (text: string) => {

export const stripMarkdown = (text: string) => {
// Remove code blocks
text = text.replaceAll(/```*?```/g, '');
text = text.replaceAll(/```[\s\S]*?```/g, '');
// Remove inline code
text = text.replaceAll(/`[^`]*`/g, '');
Expand Down Expand Up @@ -194,6 +195,12 @@ export const stripOutputTags = (text: string): string => {
return stripOutputTags(textBefore + textAfter);
};

export const stripHTML = (text: string) => {
const el = document.createElement('div');
el.innerHTML = text;
return el.textContent || '';
};

export const escapeHTML = (text: string) => {
const el = document.createElement('textarea');
el.textContent = text;
Expand Down
4 changes: 2 additions & 2 deletions src/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Localhost.args = {
ownerUserName: 'nicola',
memoriID: '1a9c75e8-57aa-4ce3-8ea5-256185fa79a7',
ownerUserID: '04a8cff9-13d6-4367-9cb2-72b9af9ee494',
tenantID: 'app.memorytwin.com',
tenantID: 'www.aisuru.com',
apiURL: 'http://localhost:7778',
baseURL: 'http://localhost:3000',
uiLang: 'EN',
Expand All @@ -58,7 +58,7 @@ LocalhostBoE.args = {
ownerUserName: 'nicola',
memoriID: '2b094cd6-77b8-4e09-a807-0039b84c988e',
ownerUserID: '04a8cff9-13d6-4367-9cb2-72b9af9ee494',
tenantID: 'app.memorytwin.com',
tenantID: 'www.aisuru.com',
apiURL: 'http://localhost:7778',
baseURL: 'http://localhost:3000',
uiLang: 'EN',
Expand Down

0 comments on commit 4d95f39

Please sign in to comment.