From 1b229beb0114ca661cecab007f415a936a7b5faa Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 19 Sep 2022 17:14:06 +0800 Subject: [PATCH] Escape HTML entity name in the title --- packages/text-to-speech/src/htmlToSsml.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/text-to-speech/src/htmlToSsml.ts b/packages/text-to-speech/src/htmlToSsml.ts index 817049114..637bb82e6 100644 --- a/packages/text-to-speech/src/htmlToSsml.ts +++ b/packages/text-to-speech/src/htmlToSsml.ts @@ -116,8 +116,12 @@ function emit(textItems: string[], text: string) { textItems.push(text) } +const cleanText = (text: string): string => { + return stripEmojis(_.escape(text.replace(/\s+/g, ' '))) +} + function cleanTextNode(textNode: ChildNode): string { - return stripEmojis(_.escape(textNode.textContent ?? ''.replace(/\s+/g, ' '))) + return cleanText(textNode.textContent ?? '') } function emitTextNode( @@ -273,7 +277,7 @@ const textToUtterance = ({ voice?: string isHtml?: boolean }): Utterance => { - const text = stripEmojis(textItems.join('')) + const text = textItems.join('') let textWithWordOffset = text if (isHtml) { try { @@ -323,7 +327,7 @@ export const htmlToSpeechFile = (htmlInput: HtmlInput): SpeechFile => { const titleUtterance = textToUtterance({ tokenizer, idx: '', - textItems: [title], + textItems: [cleanText(title)], // title could have HTML entity names like & or emoji wordOffset, isHtml: false, })