tiptap/packages/core/src/helpers/generateText.ts
Philipp Kühn fe6a3e7491
feat: add getText() and generateText() methods (fix #1428) (#1875)
* move getTextBetween method

* add getText method

* refactoring

* refactoring

* refactoring

* move renderText to schema, add generateText method

* add GenerateText demo

* docs: update

* remove demo from html page
2021-09-09 23:51:05 +02:00

30 lines
782 B
TypeScript

import { Node } from 'prosemirror-model'
import getSchema from './getSchema'
import { Extensions, JSONContent, TextSerializer } from '../types'
import getTextSeralizersFromSchema from './getTextSeralizersFromSchema'
import getText from './getText'
export default function generateText(
doc: JSONContent,
extensions: Extensions,
options?: {
blockSeparator?: string,
textSerializers?: Record<string, TextSerializer>,
},
): string {
const {
blockSeparator = '\n\n',
textSerializers = {},
} = options || {}
const schema = getSchema(extensions)
const contentNode = Node.fromJSON(schema, doc)
return getText(contentNode, {
blockSeparator,
textSerializers: {
...textSerializers,
...getTextSeralizersFromSchema(schema),
},
})
}