diff --git a/CHANGELOG.md b/CHANGELOG.md index 36afa42..475f660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog + +## [1.3.10](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.10) (2024-07-08) + - Enhancement: Update default node options + ## [1.3.9](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.9) (2024-07-08) - Fix: SRE vulnerabilities fixed diff --git a/__test__/gql/gql-json-to-html.test.ts b/__test__/gql/gql-json-to-html.test.ts index 7a836d4..9627f0a 100644 --- a/__test__/gql/gql-json-to-html.test.ts +++ b/__test__/gql/gql-json-to-html.test.ts @@ -91,8 +91,8 @@ describe('Node parser reference content', () => { GQL.jsonToHTML({entry, paths}) - expect(entry.single_rte).toEqual('
') - expect(entry.multiple_rte).toEqual(['
']) + expect(entry.single_rte).toEqual('\"dummy.pdf\"') + expect(entry.multiple_rte).toEqual(['\"dummy.pdf\"']) done() }) @@ -101,8 +101,8 @@ describe('Node parser reference content', () => { GQL.jsonToHTML({entry, paths}) - expect(entry[0].single_rte).toEqual('
') - expect(entry[0].multiple_rte).toEqual(['
']) + expect(entry[0].single_rte).toEqual('\"dummy.pdf\"') + expect(entry[0].multiple_rte).toEqual(['\"dummy.pdf\"']) done() }) diff --git a/__test__/json-to-html.test.ts b/__test__/json-to-html.test.ts index 2d351ef..51cb753 100644 --- a/__test__/json-to-html.test.ts +++ b/__test__/json-to-html.test.ts @@ -31,7 +31,8 @@ import { unorderListJson2, orderListJson2, testJsonRte, - testJsonAsset} from './mock/json-element-mock' + testJsonAsset, + embeddedAssetAsLinkJsonEntry} from './mock/json-element-mock' import { blockquoteHtml, codeHtml, @@ -116,7 +117,7 @@ describe('Node parser paragraph content', () => { describe('Node parser reference content', () => { - it.skip('Should render reference asset to html from Entry', done => { + it('Should render reference asset to html from Entry', done => { const entry = {...embeddedAssetJsonEntry} jsonToHTML({entry, paths: ['rich_text_editor', 'rte']}) @@ -126,7 +127,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entries', done => { + it('Should render reference asset to html from Entries', done => { const entry = [{ ...embeddedAssetJsonEntry }] jsonToHTML({entry, paths: ['rich_text_editor', 'rte']}) @@ -136,7 +137,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entry with custom render option', done => { + it('Should render reference asset to html from Entry with custom render option', done => { const entry = {...embeddedAssetJsonEntry} jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption}) @@ -146,7 +147,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entries with custom render option', done => { + it('Should render reference asset to html from Entries with custom render option', done => { const entry = [{ ...embeddedAssetJsonEntry }] jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption}) diff --git a/__test__/mock/json-element-mock-result.ts b/__test__/mock/json-element-mock-result.ts index 45ccc9c..8d96b49 100644 --- a/__test__/mock/json-element-mock-result.ts +++ b/__test__/mock/json-element-mock-result.ts @@ -22,7 +22,9 @@ const classAndIdAttrsHtml = " const styleObjHtml = "

heading1

heading2

heading3

heading4

heading5
heading6
" const referenceObjHtml = "

Embed entry as a link

Open entry as a link in new tab

Bold entry

Bold entry open in new tab

" const referenceObjHtmlBlock = "

Embed entry as a link

Embed entry as a link open in new tab

" -const imagetags = "
\"batman\"
The Batman
" + +const imagetags = "
\"batman\"
The Batman
" + export { h1Html, h2Html, diff --git a/__test__/mock/json-element-mock.ts b/__test__/mock/json-element-mock.ts index 4d1559a..94c0540 100644 --- a/__test__/mock/json-element-mock.ts +++ b/__test__/mock/json-element-mock.ts @@ -1138,6 +1138,41 @@ const paragraphJsonArrayEntry = { } +const embeddedAssetAsLinkJsonEntry = { + uid: 'entry_uid', + rte_data: { + type: 'doc', + attrs: {}, + uid: 'rte_uid', + children: [ + { + type: 'p', + uid: 'p_uid', + attrs: {}, + children: [ + { text: '' }, + { + uid: 'ref_uid', + type: 'reference', + attrs: { + 'display-type': 'link', + type: 'asset', + 'class-name': 'embedded-entry redactor-component undefined-entry', + 'asset-uid': 'asset_uid_2', + 'content-type-uid': 'sys_assets', + target: '_self', + href: 'https://picsum.photos/200' + }, + children: [ { text: 'Door matching', bold: true, underline: true } ] + }, + { text: '' } + ] + } + ], + _version: 4 + } +} + const embeddedAssetJsonEntry = { uid: 'entry_uid', rich_text_editor: {...assetReferenceJson}, @@ -2258,16 +2293,16 @@ const testJsonAsset={ "json_rte":{ "type": "doc", "attrs": {}, - "uid": "ddec1e08f4634eaca512b113ba4da946", + "uid": "UID", "children": [ { - "uid": "7dd9bbe6b18449b8b83cabe084b02da4", + "uid": "UID", "type": "reference", "attrs": { "display-type": "display", - "asset-uid": "bltb87e0bd5764c421e", + "asset-uid": "asset-UID", "content-type-uid": "sys_assets", - "asset-link": "https://images.contentstack.io/v3/assets/blt90bbfcce23680d08/bltb87e0bd5764c421e/64abd49b7b26dfaeede17525/batman.png", + "asset-link": "https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png", "asset-name": "batman.png", "asset-type": "image/png", "type": "asset", @@ -2328,9 +2363,10 @@ export { blockquoteJson, paragraphEntry, unorderListJson, - assetReferenceJson, + assetReferenceJson, paragraphJsonArrayEntry, embeddedAssetJsonEntry, + embeddedAssetAsLinkJsonEntry, embeddedEntryJsonEntry, entryReferenceLinkJson, entryReferenceBlockJson, diff --git a/__test__/reference-to-html.test.ts b/__test__/reference-to-html.test.ts index d6882b2..7fc0293 100644 --- a/__test__/reference-to-html.test.ts +++ b/__test__/reference-to-html.test.ts @@ -152,14 +152,14 @@ describe('Reference Node To HTML', () => { } const resultHTML = referenceToHTML(node, renderOption) - expect(resultHTML).toEqual('
') + expect(resultHTML).toEqual('') done() }) it('Should return image for undefined node asset from default node option', done => { const node = assetReferenceJson.children[0] as unknown as Node const resultHTML = referenceToHTML(node, defaultNodeOption) - expect(resultHTML).toEqual('
') + expect(resultHTML).toEqual('
') done() }) diff --git a/package-lock.json b/package-lock.json index 9d83846..0e4b5f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/utils", - "version": "1.3.9", + "version": "1.3.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/utils", - "version": "1.3.9", + "version": "1.3.10", "license": "MIT", "devDependencies": { "@babel/preset-env": "^7.22.20", diff --git a/package.json b/package.json index ae864ae..68d003e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/utils", - "version": "1.3.9", + "version": "1.3.10", "description": "Contentstack utilities for Javascript", "main": "dist/index.es.js", "types": "dist/types/index.d.ts", diff --git a/src/helper/enumerate-entries.ts b/src/helper/enumerate-entries.ts index 4d825cb..d6f0906 100644 --- a/src/helper/enumerate-entries.ts +++ b/src/helper/enumerate-entries.ts @@ -77,47 +77,6 @@ export function referenceToHTML( renderOption: RenderOption, renderEmbed?: (metadata: Metadata) => EmbeddedItem | EntryNode, ): string { - if ( node.children!== undefined && node.attrs.type === 'asset' && node.attrs['content-type-uid'] === 'sys_assets') { - - // Extract image information - const src = node.attrs['asset-link']; - const alt = node.attrs?.['redactor-attributes']?.['alt']; - const link = node.attrs.link; - const target = node.attrs.target || ""; - const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || ""; - const style = node.attrs.style; - const asset_uid= node.attrs['asset-uid']; - - // Build img tag with optional attributes - let imageTag = `${alt}` + - (link ? `` : "") + - imageTag + - (link ? `` : "") + - (caption ? `
${caption}
` : "") + - ``; - return figureTag; - } - if (node.attrs.type === 'entry' && node.attrs['display-type'] === 'link') { const entryText = node.children ? nodeChildrenToHTML(node.children, renderOption, renderEmbed) : ''; if (node.attrs.target) { diff --git a/src/options/default-node-options.ts b/src/options/default-node-options.ts index 6e10756..24b228d 100644 --- a/src/options/default-node-options.ts +++ b/src/options/default-node-options.ts @@ -105,7 +105,22 @@ export const defaultNodeOption: RenderOption = { ['reference']:(node: Node, next: Next) => { if (node.attrs.type === 'asset') { - return `` + const src = node.attrs['asset-link']; + const alt = node.attrs?.['redactor-attributes']?.['alt']; + const link = node.attrs.link; + const target = node.attrs.target || ""; + const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || ""; + const style = node.attrs.style; + const asset_uid= node.attrs['asset-uid']; + + let imageTag = ``; + + return `` + + (link ? `` : "") + + imageTag + + (link ? `` : "") + + (caption ? `
${caption}
` : "") + + ``; } return `` },