diff --git a/packages/text-to-speech/src/htmlToSsml.ts b/packages/text-to-speech/src/htmlToSsml.ts index 0f771303b..e4aca410b 100644 --- a/packages/text-to-speech/src/htmlToSsml.ts +++ b/packages/text-to-speech/src/htmlToSsml.ts @@ -117,7 +117,7 @@ function emit(textItems: string[], text: string) { } function cleanTextNode(textNode: ChildNode): string { - return _.escape(textNode.textContent ?? ''.replace(/\s+/g, ' ')) + return stripEmojis(_.escape(textNode.textContent ?? ''.replace(/\s+/g, ' '))) } function emitTextNode( @@ -252,6 +252,12 @@ export const htmlToSsmlItems = ( return items } +export const stripEmojis = (text: string): string => { + const emojiRegex = + /(?![*#0-9]+)[\p{Emoji}\p{Emoji_Modifier}\p{Emoji_Component}\p{Emoji_Modifier_Base}\p{Emoji_Presentation}]/gu + return text.replace(emojiRegex, '').replace(/\s+/g, ' ') +} + const textToUtterance = ({ tokenizer, idx, diff --git a/packages/text-to-speech/test/htmlToSsml.test.ts b/packages/text-to-speech/test/htmlToSsml.test.ts index 0ab3acdf1..bfc86c55c 100644 --- a/packages/text-to-speech/test/htmlToSsml.test.ts +++ b/packages/text-to-speech/test/htmlToSsml.test.ts @@ -1,6 +1,24 @@ import 'mocha' import { expect } from 'chai' -import { htmlToSsmlItems } from '../src/htmlToSsml' +import { htmlToSsmlItems, stripEmojis } from '../src/htmlToSsml' + +describe('stripEmojis', () => { + it('strips emojis from text and removes the extra space', () => { + const text = '🥛The Big Short guy is back with a new prediction' + + expect(stripEmojis(text)).to.equal( + 'The Big Short guy is back with a new prediction' + ) + }) + + it('strips emojis from html and removes the extra space', () => { + const text = `

🧠Brain food

` + + expect(stripEmojis(text)).to.equal( + `

Brain food

` + ) + }) +}) describe('htmlToSsmlItems', () => { const TEST_OPTIONS = {