I'm trying to get the HTML string of some exported data with the help of @prismicio/helpers and @prismicio/richtext, but it seems to fail.
For example, I have a StructuredText consisting of a single heading1
. When I use prismicH.asHTML
with that StructuredText, it renders <h1></h1>
without the inner text.
Here is the structured text:
[
{
"type": "heading1",
"content": {
"text": "Redacted title",
"spans": [{ "start": 0, "end": 8, "type": "strong" }]
}
}
]
Used snippet;
const prismicH = require('@prismicio/helpers');
const data = [
{
"type": "heading1",
"content": {
"text": "Redacted title",
"spans": [{ "start": 0, "end": 8, "type": "strong" }]
}
}
];
const htmlOutput = prismicH.asHTML(data); // outputs "<h1></h1>"
Actual output (as mentioned in the snippet above):
<h1></h1>
Expected output:
<h1><strong>Redacted</strong> title</h1>
What I have tried so far and realized;
- I briefly tried to mimic the logic within
@prismicio/react
'sRichText
component, but it did not work so far. Before going further in this scenario, I wanted to ask it here so that I know if this is the direction to go in. - The exported data's structure differs from what's shown in the API browser, which surprised me.
- Irrelevant, but I'm keen to understand this; how are we supposed to associate related content to each other? Relation ids are different than the content's
grouplang
. Only the first 15 characters of these values match each other, and I could not find any documentation on this matter.
More on the second item to emphasize it; what I see on the API browser is as follows;
[
{
"type": "heading1",
"text": "Redacted title",
"spans": [
{
"start": 0,
"end": 8,
"type": "strong"
}
]
}
]
while the exported data is as follows;
[
{
"type": "heading1",
"content": {
"text": "Redacted title",
"spans": [
{
"start": 0,
"end": 8,
"type": "strong"
}
]
}
}
]
I see that the content
property's value is flattened into its parent, where type
becomes a sibling of the text
and spans
properties. This is where it gets interesting for me. I have the impression that I cannot utilize prismicH.asHTML
just because the data structure does not fit. When I try to use prismicH.asHTML
with the data from the API browser, it'll just work as I expect, where it outputs <h1><strong>Redacted</strong> title</h1>
.
So, I'd like to ask how I can utilize the existing utility/helper functions Prismic node packages provide to transform a structured text (as it's exported from Prismic dashboard) into an HTML string?