2
0

(whatsapp) Improve / fix markdown serializer

Forked remark-slate code to create Typebot's own serializer

Closes #1056
This commit is contained in:
Baptiste Arnaud
2024-01-08 08:40:25 +01:00
parent 7d6c964a0f
commit 244a29423b
11 changed files with 629 additions and 88 deletions

View File

@ -1,11 +1,11 @@
import { ButtonItem, ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isDefined, isEmpty } from '@typebot.io/lib/utils'
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { defaultPictureChoiceOptions } from '@typebot.io/schemas/features/blocks/inputs/pictureChoice/constants'
import { defaultChoiceInputOptions } from '@typebot.io/schemas/features/blocks/inputs/choice/constants'
import { convertRichTextToMarkdown } from '@typebot.io/lib/serializer/convertRichTextToMarkdown'
export const convertInputToWhatsAppMessages = (
input: NonNullable<ContinueChatResponse['input']>,
@ -13,7 +13,9 @@ export const convertInputToWhatsAppMessages = (
): WhatsAppSendingMessage[] => {
const lastMessageText =
lastMessage?.type === BubbleBlockType.TEXT
? convertRichTextToWhatsAppText(lastMessage.content.richText ?? [])
? convertRichTextToMarkdown(lastMessage.content.richText ?? [], {
flavour: 'whatsapp',
})
: undefined
switch (input.type) {
case InputBlockType.DATE:

View File

@ -1,9 +1,9 @@
import { ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isSvgSrc } from '@typebot.io/lib/utils'
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
import { VideoBubbleContentType } from '@typebot.io/schemas/features/blocks/bubbles/video/constants'
import { convertRichTextToMarkdown } from '@typebot.io/lib/serializer/convertRichTextToMarkdown'
const mp4HttpsUrlRegex = /^https:\/\/.*\.mp4$/
@ -17,7 +17,9 @@ export const convertMessageToWhatsAppMessage = (
return {
type: 'text',
text: {
body: convertRichTextToWhatsAppText(message.content.richText),
body: convertRichTextToMarkdown(message.content.richText, {
flavour: 'whatsapp',
}),
},
}
}

View File

@ -1,9 +0,0 @@
import { TElement } from '@udecode/plate-common'
import { serialize } from 'remark-slate'
export const convertRichTextToWhatsAppText = (richText: TElement[]): string =>
richText
.map((chunk) =>
serialize(chunk)?.replaceAll('**', '*').replaceAll('&amp;#39;', "'")
)
.join('\n')