🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Adding HTML to a document in Sanity using client.create() function and HTML to Portable Text Field plugin.

24 replies
Last updated: Nov 1, 2021
Does Sanity have any documentations adding HTML to a document via the
client.create()
function?
Oct 29, 2021, 3:15 PM
So we have a route and useres can upload editor based content.When they hit submit we’d like to hit an api (using Nuxt) that would take the data from the form and create a document in Sanity.
Oct 29, 2021, 6:17 PM
Got it. So, basically what you're doing is taking the value of those fields in the form => creating an object that matches the schema of the document in your Content Lake => committing the changes to your Content Lake. You would have to do something like this:
const doc = {
  _id: uuid4(), //I use the uuid package to generate ids for docs. Remember to install/import at the top
  _type: '<your-document-type>',
  title: '<value-of-your-forms-title-field>',
  company: '<value-of-your-forms-company-field>',
  description: <value-of-your-description-field>'
}

client.create(doc).then(res => {console.log('Job was created', document id is ${res._id})}
The tricky part is your description field. I'm assuming that editor on your frontend creates HTML elements, so I suggest you set that field in your schema to an
HTML to Portable Text Field field. That plugin will allow you to pass in your HTML and have an on-the-fly conversion to Portable Text.
Oct 29, 2021, 6:37 PM
yup that makes sense!
Oct 29, 2021, 8:04 PM
user M
shoudl i be using the
block-tools
Oct 29, 2021, 8:04 PM
That package isn't quite ready for public consumption yet, so I'd say stick with your current setup and use the HTML input for now!
Oct 29, 2021, 8:08 PM
That package isn't quite ready for public consumption yet, so I'd say stick with your current setup and use the HTML input for now!
Oct 29, 2021, 8:08 PM
ahh okay
Oct 29, 2021, 8:10 PM
user M
to confirm the approach you’r suggesting is for node.js?
Oct 29, 2021, 8:10 PM
and lastly, are there any docs or examples for this?
Oct 29, 2021, 8:11 PM
Sorry one more, if i understand will the field for schema actually be portable text if we want to create them in the backend as well?
Oct 29, 2021, 8:15 PM
Likely, since this is on your frontend, you'll want to use the JS client and execute the patch script when the form is submitted. There are a few different examples shown in the client documentation here . And yes, the HTML to Portable Text plugin will take in your HTML then store it as Portable Text in your Content Lake.
Oct 29, 2021, 8:18 PM
Likely, since this is on your frontend, you'll want to use the JS client and execute the patch script when the form is submitted. There are a few different examples shown in the client documentation here . And yes, the HTML to Portable Text plugin will take in your HTML then store it as Portable Text in your Content Lake.
Oct 29, 2021, 8:18 PM
ahh okay i can poke around. okay if i ping ya a little later?
Oct 29, 2021, 8:23 PM
Yeah, go for it!
Oct 29, 2021, 8:24 PM
Likely, since this is on your frontend, you'll want to use the JS client and execute the patch script when the form is submitted. There are a few different examples shown in the client documentation here . And yes, the HTML to Portable Text plugin will take in your HTML then store it as Portable Text in your Content Lake.
Oct 29, 2021, 8:18 PM
you’re the best 🙂
Oct 29, 2021, 8:29 PM
🙂
Oct 29, 2021, 8:36 PM
I've been looking for a solution to this too. I tried the HTML to Portable Text Field Plugin but it wasn't working well for me.So I dug into the source of the plugin to find out that there is no big deal.
Jumping from a package to another, it turns out that the plugin
@sanity/block-tools
is well documented enough to provide examples for scripts.If you are running this on node.js, think to read
the doc until the `parseHtml` options where you can provide a node-based DOMParser.
Oct 30, 2021, 4:30 PM
I've been looking for a solution to this too. I tried the HTML to Portable Text Field Plugin but it wasn't working well for me.So I dug into the source of the plugin to find out that there is no big deal.
Jumping from a package to another, it turns out that the plugin
@sanity/block-tools
is well documented enough to provide examples for scripts.If you are running this on node.js, think to read
the doc until the `parseHtml` options where you can provide a node-based DOMParser.
Oct 30, 2021, 4:30 PM
to follow up here (
user M
if you’re interested).
I realized I could use
@sanity/block-tools
on the front-end before I submit the form data. Which worked very easily.At first I thought I needed to do it all on the backend but this is so much easier.
Oct 31, 2021, 11:21 PM
Awesome! Thanks for the update! Do you mind sharing your implementation so I can store it away for future use? 🙂
Nov 1, 2021, 4:43 PM
for ure!
Nov 1, 2021, 4:53 PM
for sure!
Nov 1, 2021, 4:53 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Deploying Next JS on VercelJan 20, 2021
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
DOMParser is not defined at defaultParseHtmlDec 3, 2020
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023
Error in Next.js tutorial with event handlers in server components.Feb 28, 2024
How to populate a list of values in a Sanity schema using a separate fileSep 6, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle