-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create function for transforming JSON state to text representation #1428
Comments
Looking for the same option. This would be really handy… |
There's a generateHTML from json method: https://github.com/ueberdosis/tiptap/blob/main/packages/html/src/generateHTML.ts if that's what you're looking for? |
@BrianHung No, the requirement is to generate text similar to prosemirrors |
@tobiasfuhlroth What about https://prosemirror.net/docs/ref/#model.Node^fromJSON? You can use that server-side, just need the schema. |
@BrianHung Sorry, but i don't see how that is related to the feature request described above. Can you please elaborate? |
@tobiasfuhlroth A rough sketch: import { Node } from "prosemirror-model";
const schema = editor.state.schema;
const jsonState = // get from database or somewhere
const doc = Node.fromJSON(schema, jsonState);
const textContent = doc.textContent; |
@BrianHung Ah ok, i see where you going at. The problem only starts with the last line. Please see how ProseMirror's
Currently there is no way in tiptap (at least that i know of), how to specify the text representation of a created node (other than of text type). This is actually what this feature request is about. |
Ah, you want to customize the behavior for each specific node and or mark -- that is something novel. For the time while its unsupported, you could try to write something like the |
@BrianHung thanks for the suggestions! Here is another caveat: the browser version MDN has some explanation of the difference between Another thing that would be nice about defining your own text representation is that you could prefix bullets with a dash or expand linked text |
@vpontis The implementation of |
This also sounds pretty close to the |
I also needed such function and due to the lack of one in the function generateText(editor) {
let text = '';
let separated = true;
const from = 0;
const to = editor.state.doc.content.size;
const blockSeparator = '\n';
const leafText = null;
editor.state.doc.nodesBetween(from, to, (node, pos) => {
const textSerializer = editor.extensionManager.textSerializers[node.type.name]
if (textSerializer) {
text += textSerializer({ node })
separated = !blockSeparator
} else if (node.isText) {
text += node.text.slice(Math.max(from, pos) - pos, to - pos)
separated = !blockSeparator
} else if (node.isLeaf && leafText) {
text += leafText
separated = !blockSeparator
} else if (!separated && node.isBlock) {
text += blockSeparator
separated = true
}
}, 0)
return text;
} In order for custom nodes to be converted to the text correctly, @philippkuehn @hanspagel it would be great if |
Well, I've just decided to submit a PR :) #1482 |
@vpontis I’m currently building a You also want to do this server side, right? |
I’ve implemented a first version of |
@philippkuehn thanks! It looks great :) I think you are right about those line breaks! For right now, we can't use this on the server side since our extensions are only on the client side. |
The problem I am facing
I let my users send newsletters to their audience (think Substack).
We store their custom rich text body as JSON and then when we want to send the email, we need to convert the message into HTML and text.
The solution I would like
I would like a function
generateText
that converts a JSON state to text.This could be done in two ways:
generateText
looks at nodes and converts them to texttoText
option on every Node / Mark in the schema and call that in order to convert a node to textAlternatives I have considered
I am not using TipTap yet, but I wrote my own wrapper around ProseMirror and created this file to serialize our JSON state to HTML / text — https://gist.github.com/vpontis/e5e373ea9950b413c18c867a5aedf352
The text was updated successfully, but these errors were encountered: