2
0
Files
bot/packages/lib/serializer/convertRichTextToMarkdown.ts
2024-01-11 15:09:13 +01:00

42 lines
1.1 KiB
TypeScript

import { TElement } from '@udecode/plate-common'
import serialize from './serialize'
import { defaultNodeTypes } from './ast-types'
export const convertRichTextToMarkdown = (
richText: TElement[],
options?: { flavour?: 'common' | 'whatsapp' }
) => {
let extraNewLinesCount = 0
const test = richText
.reduce<string[]>((acc, node) => {
if (node.type === 'variable') {
return [
...acc,
...node.children.map(
(child) =>
serialize(child, {
nodeTypes: defaultNodeTypes,
flavour: options?.flavour,
}) as string
),
]
}
const serializedElement = serialize(node, {
nodeTypes: defaultNodeTypes,
flavour: options?.flavour,
})
if (!serializedElement || serializedElement === '<br>\n\n') {
if (extraNewLinesCount > 0) {
return [...acc, '']
}
extraNewLinesCount++
return acc
}
extraNewLinesCount = 0
return [...acc, serializedElement]
}, [])
.join('\n')
return test.endsWith('\n') ? test.slice(0, -1) : test
}