How do I get RTF content as an actual RTF string

Hello,

I have a RTF field in my Prismic document. When I pull the document to my application and I look at the data, I see that the RTF content is a json object that looks like this:

0: {type: "heading1", text: "Title", spans: Array(0)}
1: {type: "paragraph", text: "This is rich text", spans: Array(2)}
2: {type: "paragraph", text: "·         Bullet point 1", spans: Array(0)}
3: {type: "paragraph", text: "·         Bullet point 2", spans: Array(0)}
4: {type: "paragraph", text: "·         Bullet point 3", spans: Array(0)}
5: {type: "paragraph", text: " ", spans: Array(0)}
6: {type: "paragraph", text: "1.       Bullet point a", spans: Array(0)}
7: {type: "paragraph", text: "2.       Bullet point b", spans: Array(0)}
8: {type: "paragraph", text: "3.       Bullet point c", spans: Array(0)}
9: {type: "paragraph", text: " ", spans: Array(0)}
10: {type: "paragraph", text: "Subscript1", spans: Array(0)}
11: {type: "paragraph", text: "Superscript1", spans: Array(0)}
12: {type: "paragraph", text: "Strikethrough", spans: Array(0)}
13: {type: "paragraph", text: "Red text", spans: Array(0)}
14: {type: "paragraph", text: "Yellow background", spans: Array(0)}
15: {type: "paragraph", text: "Big text", spans: Array(0)}
16: {type: "paragraph", text: "Small text", spans: Array(0)}
17: {type: "paragraph", text: "Right aligned", spans: Array(0)}
18: {type: "paragraph", text: "<center>centered</center>", spans: Array(0)}
19: {type: "paragraph", text: " ", spans: Array(0)}
20: {type: "paragraph", text: "Lorem ipsum dolor sit amet, ea tacimates convenire…animal fierent in, in pri stet audire honestatis.", spans: Array(0)}
21: {type: "paragraph", text: " ", spans: Array(0)}
22: {type: "paragraph", text: " ", spans: Array(0)}
23: {type: "paragraph", text: " ", spans: Array(0)}
24: {type: "paragraph", text: "Lorem ipsum dolor sit amet, ea tacimates convenire…animal fierent in, in pri stet audire honestatis.", spans: Array(0)}
25: {type: "paragraph", text: " ", spans: Array(0)}
26: {type: "paragraph", text: " ", spans: Array(0)}
27: {type: "paragraph", text: " ", spans: Array(0)}

I want to convert this to HTML. I'm using a node package called rtf-to-html:

Unfortunately, rtf-to-html takes actual rich text formatted strings, not json objects that represent RTF:

rtfToHTML.fromString('{\\rtf1\\ansi\\b hi there\\b0}', (err, html) => {
  console.log(html)
  // prints a document containing:
  // <p><strong>hi there</strong></p>
})

Is there a way to dowload the data from Prismic as an actual rich text formatted string? Or maybe a way to convert the json object to a RTF string?

Thank you.

Hey Gibran,

Our development kits like prismic-javascript and prismic-dom can handle that for you. You can see how to install those here:

Then to add custom configurations to your rich text on top of that to override the default configration, you can add an HTML Serializer:

Let me know what you think.

Thanks.

Thanks Phil, those work perfectly.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.