Trouble with handling images in the block-content-to-markdown plugin.

5 replies
Last updated: Aug 16, 2023
Is there anyone here who has knowledge of or uses this plug-in? https://github.com/sanity-io/block-content-to-markdown I have problems handling images and I don’t know the solution. Does anyone have a code that actually works? I have tried to follow the documentation but still get an error message. My code is based on the example from the documentation, but where do I insert the parameter for handling images.

const toMarkdown = require('@sanity/block-content-to-markdown')
const client = require('@sanity/client')({
  projectId: '<your project id>',
  dataset: '<some dataset>',
  useCdn: true
})

const serializers = {
  types: {
    code: props => '```' + props.node.language + '\n' + props.node.code + '\n```'
  }
}

client.fetch('*[_type == "article"][0]').then(article => {
  console.log(toMarkdown(article.body, {serializers}))
})

Aug 15, 2023, 7:25 AM
At the same moment I posted this I realized how to solve it 😆 This was a https://en.wikipedia.org/wiki/Rubber_duck_debugging
Aug 15, 2023, 7:31 AM
What was the solution?
Aug 15, 2023, 8:00 PM
Add the serializers after imageOptions.
Aug 16, 2023, 8:08 AM
client.fetch('*[_type == "article"][0]').then(article => {
  console.log(toMarkdown(article.body, {
    imageOptions: {w: 779, fit: 'max'},
    projectId: "X",
    dataset: "production",
    serializers
  }))
})
Aug 16, 2023, 8:09 AM
Beautiful, thanks for sharing!
Aug 16, 2023, 2:58 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?

Categorized in