Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. const { richTextFromMarkdown } = require('@contentful/rich-text-from-markdown')
  2. const { createClient } = require('contentful-management')
  3.  
  4. // 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.
  5.  
  6. module.exports = async function(migration, { spaceId, accessToken, environmentId }) {
  7.  
  8. // 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.
  9.  
  10. const client = await createClient({ accessToken: accessToken })
  11. const space = await client.getSpace(spaceId)
  12. const environment = await space.getEnvironment(environmentId)
  13.  
  14. // 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.
  15.  
  16. migration.transformEntries({
  17. contentType: 'blogPost',
  18. from: ['markdown'],
  19. to: ['content'],
  20. shouldPublish: true,
  21.  
  22. // 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)
  23. transformEntryForLocale: async function(fromFields, currentLocale) {
  24.  
  25. // 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
  26.  
  27. if (
  28. currentLocale !== 'en-US' ||
  29. fromFields.markdown === undefined
  30. ) {
  31. return
  32. }
  33.  
  34. // 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.
  35.  
  36. const content = await richTextFromMarkdown(fromFields.markdown['en-US'], async (node) => {
  37. switch (node.type){
  38. case 'code':
  39. return processCode(node)
  40. case 'image':
  41. return await processImage(environment, node)
  42. }
  43. })
  44.  
  45. // This is where the regular text nodes are handled
  46.  
  47. try {
  48. return {
  49. content: content
  50. }
  51. } catch (error){
  52. console.error
  53. }
  54. }
  55. })
  56. }
  57.  
  58. // 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
  59.  
  60. const processCode = async (node) => {
  61. return {
  62. nodeType: "blockquote",
  63. content: [
  64. {
  65. nodeType: "paragraph",
  66. data: {},
  67. content: [
  68. {
  69. nodeType: "text",
  70. value: node.value,
  71. marks: [],
  72. data: {}
  73. }
  74. ]
  75. }
  76. ],
  77. data: {}
  78. }
  79. }
  80.  
  81. // 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
  82.  
  83. const processImage = async (environment, node) => {
  84. const title = node.url.split('/').pop()
  85. const ext = title.split('.').pop()
  86.  
  87. const asset = await environment.createAsset({
  88. fields: {
  89. title: {
  90. 'en-US': `Blog post image: ${title}`
  91. },
  92. description: {
  93. 'en-US': node.alt || `Blog post image: ${title}`
  94. },
  95. file: {
  96. 'en-US': {
  97. contentType: `image/${ext}`,
  98. fileName: title,
  99. upload: node.url
  100. }
  101. }
  102. }
  103. }).catch(e => console.log('in create asset catch'))
  104.  
  105. asset.processForAllLocales()
  106.  
  107. return {
  108. nodeType: 'embedded-asset-block',
  109. content: [],
  110. data: {
  111. target: {
  112. sys: {
  113. type: 'Link',
  114. linkType: 'Asset',
  115. id: asset.sys.id
  116. }
  117. }
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement