Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { richTextFromMarkdown } = require('@contentful/rich-text-from-markdown')
- const { createClient } = require('contentful-management')
- // Our function takes in the migration for free from the runMigration function in config.js. We also get our space id, environment id and access token.
- module.exports = async function(migration, { spaceId, accessToken, environmentId }) {
- // We need to find our client, space and environment because, like we saw when we used the ruby gem above, to get to the environment which is where we create entries, we need our space and client first.
- const client = await createClient({ accessToken: accessToken })
- const space = await client.getSpace(spaceId)
- const environment = await space.getEnvironment(environmentId)
- // We call the transformEntries function on our migration to ask the library to find our blog post content model and for each one, take its markdown field, do something to it (defined below) and push that result into its content field. The shouldPublish attribute set to true also publishes it rather than leaving it as a draft.
- migration.transformEntries({
- contentType: 'blogPost',
- from: ['markdown'],
- to: ['content'],
- shouldPublish: true,
- // The transformEntryForLocale attribute's value is an anonymous function that is called with the value of the current field (fromFields) and that field's locale (currentLocale)
- transformEntryForLocale: async function(fromFields, currentLocale) {
- // If the currentLocale isn't 'en-US' or if the markdown field is empty we want to move on and process the next field rather than waste time trying to process something that isn't there
- if (
- currentLocale !== 'en-US' ||
- fromFields.markdown === undefined
- ) {
- return
- }
- // This is where more ✨magic✨ happens. Here we call on the powers of the rich-text-from-markdown library to convert the nodes of our markdown field into nodes that the rich text field can understand. If it comes across a node that it can't automatically parse, it's passed into the second argument of our richTextFromMarkdown function which then passes it into a switch statement that is able to determine what kind of node it is. In our case, code blocks and images were the ones we had to define manually.
- const content = await richTextFromMarkdown(fromFields.markdown['en-US'], async (node) => {
- switch (node.type){
- case 'code':
- return processCode(node)
- case 'image':
- return await processImage(environment, node)
- }
- })
- // This is where the regular text nodes are handled
- try {
- return {
- content: content
- }
- } catch (error){
- console.error
- }
- }
- })
- }
- // If the richTextFromMarkdown comes across a code block, the node is passed into this helper function that converts it to a format that the rich text field can understand
- const processCode = async (node) => {
- return {
- nodeType: "blockquote",
- content: [
- {
- nodeType: "paragraph",
- data: {},
- content: [
- {
- nodeType: "text",
- value: node.value,
- marks: [],
- data: {}
- }
- ]
- }
- ],
- data: {}
- }
- }
- // If the richTextFromMarkdown comes across a image, the node is passed into this helper function that creates an asset in our Contentful environment, uploads and publishes that image and returns it in a format that the rich text field can understand
- const processImage = async (environment, node) => {
- const title = node.url.split('/').pop()
- const ext = title.split('.').pop()
- const asset = await environment.createAsset({
- fields: {
- title: {
- 'en-US': `Blog post image: ${title}`
- },
- description: {
- 'en-US': node.alt || `Blog post image: ${title}`
- },
- file: {
- 'en-US': {
- contentType: `image/${ext}`,
- fileName: title,
- upload: node.url
- }
- }
- }
- }).catch(e => console.log('in create asset catch'))
- asset.processForAllLocales()
- return {
- nodeType: 'embedded-asset-block',
- content: [],
- data: {
- target: {
- sys: {
- type: 'Link',
- linkType: 'Asset',
- id: asset.sys.id
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement