Merge pull request #1183 from omnivore-app/fix/remove-emoji-from-speech-file

This commit is contained in:
Jackson Harper 2022-09-09 19:42:02 +08:00 committed by GitHub
commit 047dd4735f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View file

@ -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,

View file

@ -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 = `<h2 data-omnivore-anchor-idx="37">🧠Brain food</h2>`
expect(stripEmojis(text)).to.equal(
`<h2 data-omnivore-anchor-idx="37">Brain food</h2>`
)
})
})
describe('htmlToSsmlItems', () => {
const TEST_OPTIONS = {