How to Migrate Markdown to Contentful Rich Text via Code

52 views Asked by At

I need some help with the concept of this endeavour.

I have a custom CMS built in Laravel and PHP. The posts are stored in markdown format.

How can I build a local API to convert the markdown into Contentful's Rich Text via code? I have over 100 posts and don't want to migrate them manually.

Here's what I came up with so far. But I'm having problems converting with markdown to rich text.

const { PrismaClient } = require("@prisma/client")
const contentful = require("contentful-management")
const { richTextFromMarkdown } = require("@contentful/rich-text-from-markdown")
const { logFunction, logValue } = require("./lib/helpers.lib")
const { envVars, contentTypeIds } = require("./lib/contentful.lib")

const prisma = new PrismaClient()

const { title, slug, subtitle, summary, text, featured, status, published_at } =
  prisma.posts.findFirst({ where: { id: 1 } })

const richText = richTextFromMarkdown(text)
logValue(`richText`, richText)

const client = contentful.createClient({
  accessToken: envVars.accessToken,
})

// This API call will request a space with the specified ID
client.getSpace(envVars.spaceId).then((space) => {
  // This API call will request an environment with the specified ID
  space.getEnvironment(envVars.environmentId).then((environment) => {
    // let's get a content type
    environment.getContentType(contentTypeIds.posts).then((contentType) => {
      // and now let's update its name
      contentType.title = title
      contentType.slug = slug
      contentType.excerpt = summary
      contentType.subtitle = subtitle
      // contentType.content = richText <-- not working

      contentType.update().then((updatedContentType) => {
        console.log("Update was successful")
      })
    })
  })
})

0

There are 0 answers