@@ -5,7 +5,7 @@ import axios from "axios";
55//import _ from "lodash";
66import { LOCALE_MAPPER , MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
77import jsdom from "jsdom" ;
8- import { htmlToJson } from "@contentstack/json-rte-serializer" ;
8+ import { htmlToJson , jsonToHtml } from "@contentstack/json-rte-serializer" ;
99import customLogger from "../utils/custom-logger.utils.js" ;
1010import { getLogMessage } from "../utils/index.js" ;
1111import { Advanced } from "../models/FieldMapper.js" ;
@@ -110,20 +110,28 @@ let assetData: Record<string, any> | any = {};
110110let blog_base_url = "" ;
111111
112112//helper function to convert entries with content type
113- function mapContentTypeToEntry ( contentType : any , data : any ) {
114- return contentType ?. fieldMapping ?. reduce ( ( acc : { [ key : string ] : any } , field : FieldMapping ) => {
113+ async function mapContentTypeToEntry ( contentType : any , data : any ) {
114+ const result : { [ key : string ] : any } = { } ;
115+
116+ for ( const field of contentType ?. fieldMapping || [ ] ) {
115117 const fieldValue = data ?. [ field ?. uid ] ;
116118 let formattedValue ;
117119
118120 switch ( field ?. contentstackFieldType ) {
119121 case "single_line_text" :
120122 case "text" :
121- case 'html' :
122123 formattedValue = fieldValue ;
123124 break ;
124- case "json" :
125- formattedValue = convertHtmlToJson ( fieldValue ) ;
125+ case "html" :
126+ formattedValue =
127+ fieldValue && typeof fieldValue === "object"
128+ ? await convertJsonToHtml ( fieldValue )
129+ : fieldValue ;
126130 break ;
131+ case "json" :
132+
133+ formattedValue = await convertHtmlToJson ( fieldValue ) ;
134+ break ;
127135 case "reference" :
128136 formattedValue = getParent ( data , data [ field . uid ] ) ;
129137 break ;
@@ -134,10 +142,12 @@ function mapContentTypeToEntry(contentType: any, data: any) {
134142 formattedValue = Array . isArray ( formattedValue ) ? formattedValue : [ formattedValue ] ;
135143 }
136144
137- acc [ field . contentstackFieldUid ] = formattedValue ;
138- return acc ;
139- } , { } ) ;
145+ result [ field ?. contentstackFieldUid ] = formattedValue ;
146+ }
147+
148+ return result ;
140149}
150+
141151// helper functions
142152async function writeFileAsync ( filePath : string , data : any , tabSpaces : number ) {
143153 filePath = path . resolve ( filePath ) ;
@@ -679,7 +689,7 @@ async function getAllreference(affix: string, packagePath: string, destinationSt
679689 ...processReferenceData ( referenceTerms , "terms" , "wp:term_slug" , terms )
680690 ) ;
681691 referenceArray . push (
682- ...processReferenceData ( referenceTags , "tags " , "wp:tag_slug" , tag )
692+ ...processReferenceData ( referenceTags , "tag " , "wp:tag_slug" , tag )
683693 ) ;
684694
685695 if ( referenceArray . length > 0 ) {
@@ -887,7 +897,7 @@ async function saveAuthors(authorDetails: any[], destinationStackId: string, pro
887897 authordata [ customId ] = {
888898 ...authordata [ customId ] ,
889899 uid : customId ,
890- ...mapContentTypeToEntry ( contentType , authordataEntry ) ,
900+ ...( await mapContentTypeToEntry ( contentType , authordataEntry ) ) ,
891901 } ;
892902 authordata [ customId ] . publish_details = [ ] ;
893903
@@ -1633,15 +1643,16 @@ async function saveTerms(
16331643 try {
16341644 const termsFilePath = path . join ( termsFolderPath , `${ master_locale } .json` ) ;
16351645 const termsdata = termsDetails . reduce (
1636- ( acc : { [ key : string ] : any } , data ) => {
1646+ async ( acc : { [ key : string ] : any } , data ) => {
1647+
16371648 const { id } = data ;
16381649 const uid = `terms_${ id } ` ;
16391650 const customId = uid ;
16401651
16411652 acc [ customId ] = {
16421653 ...acc [ customId ] ,
16431654 uid : customId ,
1644- ...mapContentTypeToEntry ( contentType , data ) ,
1655+ ...mapContentTypeToEntry ( contentType , data ) , // Pass individual term object
16451656 } ;
16461657 acc [ customId ] . publish_details = [ ] ;
16471658 return acc ;
@@ -1813,17 +1824,19 @@ async function saveTags(
18131824) {
18141825 const srcFunc = 'saveTags' ;
18151826 try {
1816- const tagsFilePath = path . join ( tagsFolderPath , `${ master_locale } .json` ) ;
1817-
1827+ const tagsFilePath = path . join (
1828+ tagsFolderPath ,
1829+ `${ master_locale } .json`
1830+ ) ;
18181831 const tagdata = tagDetails . reduce ( ( acc : { [ key : string ] : any } , data ) => {
18191832 const { id } = data ;
18201833 const uid = `tags_${ id } ` ;
18211834 const customId = idCorrector ( uid ) ;
18221835
18231836 acc [ customId ] = {
18241837 ...acc [ customId ] ,
1825- uid : customId ,
1826- ...mapContentTypeToEntry ( contenttype , data ) ,
1838+ uid :customId ,
1839+ ...mapContentTypeToEntry ( contenttype , data ) ,
18271840 } ;
18281841 acc [ customId ] . publish_details = [ ] ;
18291842
@@ -1977,11 +1990,22 @@ async function startingDirCategories(
19771990}
19781991
19791992const convertHtmlToJson = ( htmlString : any ) => {
1980- const dom = new JSDOM ( htmlString ?. replace ( / & a m p ; / g, "&" ) ) ;
1981- const htmlDoc = dom . window . document . querySelector ( "body" ) ;
1982- return htmlToJson ( htmlDoc ) ;
1993+ if ( typeof htmlString === 'string' ) {
1994+ const dom = new JSDOM ( htmlString ?. replace ( / & a m p ; / g, "&" ) ) ;
1995+ const htmlDoc = dom . window . document . querySelector ( "body" ) ;
1996+ const jsonValue = htmlToJson ( htmlDoc )
1997+ return jsonValue ;
1998+
1999+ }
2000+ else return htmlString ;
19832001} ;
19842002
2003+ const convertJsonToHtml = async ( json : any ) => {
2004+ const htmlValue = await jsonToHtml ( json ) ;
2005+ return htmlValue ;
2006+
2007+ }
2008+
19852009function getParent ( data : any , id : string ) {
19862010 const parentId : any = fs . readFileSync (
19872011 path . join ( referencesFolder , MIGRATION_DATA_CONFIG . REFERENCES_FILE_NAME ) ,
@@ -2015,17 +2039,17 @@ async function saveCategories(
20152039 const srcFunc = 'saveCategories' ;
20162040 try {
20172041 const categorydata = categoryDetails . reduce (
2018- ( acc : { [ key : string ] : any } , data ) => {
2042+ async ( acc : { [ key : string ] : any } , data ) => {
20192043 const uid = `category_${ data [ "id" ] } ` ;
20202044
20212045 const customId = uid ;
20222046
20232047 // Accumulate category data
20242048 acc [ customId ] = {
20252049 ...acc [ customId ] ,
2026- uid : customId ,
2027- ...mapContentTypeToEntry ( contenttype , data ) ,
2028- } ;
2050+ uid :customId ,
2051+ ...mapContentTypeToEntry ( contenttype , data ) ,
2052+ }
20292053 acc [ customId ] . publish_details = [ ] ;
20302054
20312055 return acc ;
@@ -2378,23 +2402,23 @@ async function processChunkData(
23782402 postdata
23792403 ) ;
23802404 const formattedPosts = Object ?. entries ( formatted_posts ) ?. reduce (
2381- ( acc : { [ key : string ] : any } , data : any ) => {
2382-
2405+ ( acc : { [ key : string ] : any } , data :any ) => {
2406+
23832407 const customId = idCorrector ( data [ "uid" ] )
2384-
2408+
23852409 // Accumulate category data
2386- acc [ customId ] = {
2410+ acc [ customId ] = {
23872411 ...acc [ customId ] ,
23882412 uid : customId ,
2389- ...mapContentTypeToEntry ( contenttype , data ) ,
2413+ ...mapContentTypeToEntry ( contenttype , data ) ,
23902414 } ;
23912415 acc [ customId ] . publish_details = [ ] ;
2392-
2416+
23932417 return acc ;
23942418 } ,
23952419 { }
23962420 ) ;
2397- Object . assign ( postdata , formattedPosts ) ;
2421+ Object . assign ( postdata , formattedPosts ) ;
23982422
23992423 // await writeFileAsync(
24002424 // path.join(postFolderPath, filename),
@@ -2415,6 +2439,7 @@ async function processChunkData(
24152439 if ( isLastChunk && allSuccess ) {
24162440 console . info ( "last data" ) ;
24172441 }
2442+ console . info ( "postData ---> " , postdata )
24182443 return postdata
24192444 } catch ( error ) {
24202445 console . error ( error ) ;
@@ -2433,8 +2458,8 @@ async function extractPosts(
24332458 project : any
24342459) {
24352460 const srcFunc = "extractPosts" ;
2436- const ct : any = keyMapper ?. [ "categories" ] ;
2437- const contenttype = contentTypes ?. find ( ( item : any ) => item ?. otherCmsUid === 'categories' ) ;
2461+ const ct :any = keyMapper ?. [ "categories" ] ;
2462+ const contenttype = contentTypes ?. find ( ( item :any ) => item ?. otherCmsUid === 'categories' ) ;
24382463
24392464 try {
24402465 await startingDirPosts ( ct , master_locale , project ?. locales ) ;
0 commit comments