Skip to content

Commit

Permalink
fixed the issues with rich text type and bugs in entries helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-raj committed Jun 4, 2024
1 parent fc21dff commit 274d060
Show file tree
Hide file tree
Showing 9 changed files with 616 additions and 446 deletions.
985 changes: 578 additions & 407 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@colors/colors": "^1.5.0",
"@contentstack/cli-cm-export": "~1.11.4",
"@contentstack/cli-cm-import": "~1.15.6",
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-utilities": "~1.6.2",
"async": "^3.2.4",
Expand Down Expand Up @@ -72,4 +72,4 @@
"cm:stack-clone": "O-CLN"
}
}
}
}
4 changes: 2 additions & 2 deletions packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "1.15.6",
"version": "1.15.7",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down Expand Up @@ -99,4 +99,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
10 changes: 5 additions & 5 deletions packages/contentstack-import/src/utils/asset-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export const lookupAssets = function (
if (
schema[i].data_type === 'text' &&
schema[i].field_metadata &&
(schema[i].field_metadata.markdown || schema[i].field_metadata.rich_text_type)
(schema[i]?.field_metadata?.markdown || schema[i]?.field_metadata?.rich_text_type)
) {
parent.push(schema[i].uid);
findFileUrls(schema[i], entryToFind, assetUrls);
if (schema[i].field_metadata.rich_text_type) {
if (schema[i]?.field_metadata?.rich_text_type) {
findAssetIdsFromHtmlRte(entryToFind, schema[i]);
}
parent.pop();
Expand All @@ -110,7 +110,7 @@ export const lookupAssets = function (
}
// added rich_text_type field check because some marketplace extensions also
// have data_type has json
if (schema[i].data_type === 'json' && schema[i].field_metadata.rich_text_type) {
if (schema[i].data_type === 'json' && schema[i]?.field_metadata?.rich_text_type) {
parent.push(schema[i].uid);
// findFileUrls(schema[i], entry, assetUrls)
findAssetIdsFromJsonRte(data.entry, data.content_type.schema);
Expand Down Expand Up @@ -188,7 +188,7 @@ export const lookupAssets = function (
break;
}
case 'json': {
if (entryObj[element.uid] && element.field_metadata.rich_text_type) {
if (entryObj[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple) {
entryObj[element.uid].forEach((jsonRteData: any) => {
gatherJsonRteAssetIds(jsonRteData);
Expand Down Expand Up @@ -267,7 +267,7 @@ export const lookupAssets = function (
if (typeof uid !== 'undefined') {
const escapedAssetUid = assetUid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`\\b${escapedAssetUid}\\b`, 'img');
let { status } = validateRegex(new RegExp(regex, 'img'))
let { status } = validateRegex(new RegExp(regex, 'img'));
if (status === 'safe') {
entry = entry.replace(regex, uid);
matchedUids.push(assetUid);
Expand Down
18 changes: 9 additions & 9 deletions packages/contentstack-import/src/utils/content-type-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export const suppressSchemaReference = function (schema: any, flag: any) {
}
} else if (schema[i].data_type === 'reference') {
flag.references = true;
} else if (schema[i].data_type === 'json' && schema[i].field_metadata.rich_text_type) {
} else if (schema[i].data_type === 'json' && schema[i]?.field_metadata?.rich_text_type) {
flag.jsonRte = true;
if (schema[i].field_metadata.embed_entry === true) flag.jsonRteEmbeddedEntries = true;
} else if (schema[i].data_type === 'text' && schema[i].field_metadata.rich_text_type) {
} else if (schema[i].data_type === 'text' && schema[i]?.field_metadata?.rich_text_type) {
flag.rte = true;
if (schema[i].field_metadata.embed_entry === true) flag.rteEmbeddedEntries = true;
}
Expand Down Expand Up @@ -143,18 +143,18 @@ export const removeReferenceFields = async function (
} else if (
// handling entry references in json rte
schema[i].data_type === 'json' &&
schema[i].field_metadata.rich_text_type &&
schema[i].field_metadata.embed_entry &&
schema[i].reference_to.length > 1
schema[i]?.field_metadata?.rich_text_type &&
schema[i]?.field_metadata?.embed_entry &&
schema[i]?.reference_to?.length > 1
) {
flag.supressed = true;
schema[i].reference_to = ['sys_assets'];
} else if (
// handling entry references in rte
schema[i].data_type === 'text' &&
schema[i].field_metadata.rich_text_type &&
schema[i].field_metadata.embed_entry &&
schema[i].reference_to.length >= 1
schema[i]?.field_metadata?.rich_text_type &&
schema[i]?.field_metadata?.embed_entry &&
schema[i]?.reference_to?.length >= 1
) {
flag.supressed = true;
schema[i].reference_to = ['sys_assets'];
Expand Down Expand Up @@ -185,4 +185,4 @@ export const updateFieldRules = function (contentType: any) {
}
}
return fieldRules;
}
};
27 changes: 13 additions & 14 deletions packages/contentstack-import/src/utils/entries-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const lookupEntries = function (
if (_entry && _parent[j]) {
if (j === len - 1 && _entry[_parent[j]]) {
if (form_id !== '_assets') {
if (_entry[_parent[j]]?.length) {
_entry[_parent[j]].forEach((item: any, idx: any) => {
if (_entry[_parent[j]]?.length && Object.keys(_entry).includes(_parent[j])) {
_entry[_parent[j]]?.forEach((item: any, idx: any) => {
if (typeof item.uid === 'string' && item._content_type_uid) {
uids.push(item.uid);
} else if (typeof item === 'string' && preserveStackVersion === true) {
Expand Down Expand Up @@ -110,7 +110,7 @@ export const lookupEntries = function (
case 'reference':
if (Array.isArray(schema[i].reference_to)) {
isNewRefFields = true;
schema[i].reference_to.forEach((reference: any) => {
schema[i]?.reference_to?.forEach((reference: any) => {
parent.push(schema[i].uid);
update(parent, reference, _entry);
parent.pop();
Expand All @@ -137,7 +137,7 @@ export const lookupEntries = function (
}
break;
case 'json':
if (schema[i].field_metadata.rich_text_type) {
if (schema[i]?.field_metadata?.rich_text_type) {
findEntryIdsFromJsonRte(data.entry, data.content_type.schema);
}
break;
Expand Down Expand Up @@ -174,7 +174,7 @@ export const lookupEntries = function (
break;
}
case 'json': {
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple) {
entry[element.uid]?.forEach((jsonRteData: any) => {
gatherJsonRteEntryIds(jsonRteData);
Expand All @@ -201,7 +201,7 @@ export const lookupEntries = function (

uids = _.uniq(uids);
let entry = JSON.stringify(data.entry);
uids.forEach(function (uid: any) {
uids?.forEach(function (uid: any) {
if (mappedUids.hasOwnProperty(uid)) {
const sanitizedUid = escapeRegExp(uid);
const escapedMappedUid = escapeRegExp(mappedUids[uid]);
Expand Down Expand Up @@ -252,7 +252,7 @@ function findUidsInNewRefFields(entry: any, uids: string[]) {
if (entry.uid && entry._content_type_uid) {
uids.push(entry.uid);
} else if (Array.isArray(entry) && entry.length) {
entry.forEach(function (elem) {
entry?.forEach(function (elem) {
findUidsInNewRefFields(elem, uids);
});
} else if (Object.keys(entry).length) {
Expand Down Expand Up @@ -299,7 +299,7 @@ export const removeUidsFromJsonRteFields = (
break;
}
case 'json': {
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple) {
entry[element.uid] = entry[element.uid].map((jsonRteData: any) => {
delete jsonRteData.uid; // remove uid
Expand Down Expand Up @@ -394,7 +394,7 @@ export const removeEntryRefsFromJSONRTE = (entry: Record<string, any>, ctSchema:
}
case 'json': {
const structuredPTag = '{"type":"p","attrs":{},"children":[{"text":""}]}';
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple) {
entry[element.uid] = entry[element.uid].map(removeReferenceInJsonRTE);

Expand Down Expand Up @@ -427,7 +427,7 @@ export const removeEntryRefsFromJSONRTE = (entry: Record<string, any>, ctSchema:
break;
}
case 'text': {
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple) {
let rteContent = [];
for (let i = 0; i < entry[element.uid].length; i++) {
Expand Down Expand Up @@ -532,7 +532,7 @@ export const restoreJsonRteEntryRefs = (
break;
}
case 'json': {
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
if (element.multiple && Array.isArray(entry[element.uid])) {
entry[element.uid] = sourceStackEntry[element.uid].map((jsonRTE: any) => {
jsonRTE = restoreReferenceInJsonRTE(jsonRTE, uidMapper);
Expand All @@ -556,7 +556,7 @@ export const restoreJsonRteEntryRefs = (
break;
}
case 'text': {
if (entry[element.uid] && element.field_metadata.rich_text_type) {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
entry[element.uid] = sourceStackEntry[element.uid];
const matches = Object.keys(uidMapper).filter((uid) => {
if (sourceStackEntry[element.uid].indexOf(uid) !== -1) return uid;
Expand All @@ -582,8 +582,7 @@ function updateUids(str: string, match: string, uidMapper: Record<string, string
const sanitizedMatch = escapeRegExp(match);
const regex = new RegExp(`\\b${sanitizedMatch}\\b`, 'g');
let { status } = validateRegex(regex);
if (status === 'safe')
return str.replace(regex, (matchedString) => uidMapper[matchedString]);
if (status === 'safe') return str.replace(regex, (matchedString) => uidMapper[matchedString]);
}

function setDirtyTrue(jsonRteChild: any) {
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-cm-import": "~1.15.6",
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-utilities": "~1.6.2",
"inquirer": "8.2.4",
Expand Down Expand Up @@ -73,4 +73,4 @@
"version": "oclif readme && git add README.md",
"clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
}
}
}
4 changes: 2 additions & 2 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@contentstack/cli-cm-export": "~1.11.4",
"@contentstack/cli-cm-clone": "~1.10.5",
"@contentstack/cli-cm-export-to-csv": "~1.7.1",
"@contentstack/cli-cm-import": "~1.15.6",
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-cm-migrate-rte": "~1.4.17",
"@contentstack/cli-cm-seed": "~1.7.5",
"@contentstack/cli-command": "~1.2.18",
Expand Down Expand Up @@ -160,4 +160,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 274d060

Please sign in to comment.