2023-02-03 00:37:33 +08:00
|
|
|
import { Node } from '@tiptap/pm/model'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
2021-09-10 05:51:05 +08:00
|
|
|
import { Extensions, JSONContent, TextSerializer } from '../types'
|
2022-06-08 20:10:25 +08:00
|
|
|
import { getSchema } from './getSchema'
|
2021-12-06 19:00:09 +08:00
|
|
|
import { getText } from './getText'
|
2022-06-08 20:10:25 +08:00
|
|
|
import { getTextSerializersFromSchema } from './getTextSerializersFromSchema'
|
2021-09-10 05:51:05 +08:00
|
|
|
|
2021-12-06 19:00:09 +08:00
|
|
|
export function generateText(
|
2021-09-10 05:51:05 +08:00
|
|
|
doc: JSONContent,
|
|
|
|
extensions: Extensions,
|
|
|
|
options?: {
|
2023-02-03 00:37:33 +08:00
|
|
|
blockSeparator?: string
|
|
|
|
textSerializers?: Record<string, TextSerializer>
|
2021-09-10 05:51:05 +08:00
|
|
|
},
|
|
|
|
): string {
|
2023-02-03 00:37:33 +08:00
|
|
|
const { blockSeparator = '\n\n', textSerializers = {} } = options || {}
|
2021-09-10 05:51:05 +08:00
|
|
|
const schema = getSchema(extensions)
|
|
|
|
const contentNode = Node.fromJSON(schema, doc)
|
|
|
|
|
|
|
|
return getText(contentNode, {
|
|
|
|
blockSeparator,
|
|
|
|
textSerializers: {
|
2022-05-03 04:01:07 +08:00
|
|
|
...getTextSerializersFromSchema(schema),
|
2022-12-16 19:47:03 +08:00
|
|
|
...textSerializers,
|
2021-09-10 05:51:05 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|