Skip to content

Commit 22cd7c1

Browse files
authored
Merge pull request #85 from contentstack/next
DX | 31-07-2024 | Release
2 parents deef4d1 + d40c271 commit 22cd7c1

10 files changed

+79
-62
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
4+
## [1.3.10](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.10) (2024-07-08)
5+
- Enhancement: Update default node options
6+
37
## [1.3.9](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.9) (2024-07-08)
48
- Fix: SRE vulnerabilities fixed
59

__test__/gql/gql-json-to-html.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe('Node parser reference content', () => {
9191

9292
GQL.jsonToHTML({entry, paths})
9393

94-
expect(entry.single_rte).toEqual('<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>')
95-
expect(entry.multiple_rte).toEqual(['<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>'])
94+
expect(entry.single_rte).toEqual('<img src=\"/asset_uid_1/dummy.pdf\" alt=\"dummy.pdf\" />')
95+
expect(entry.multiple_rte).toEqual(['<img src=\"/asset_uid_1/dummy.pdf\" alt=\"dummy.pdf\" />'])
9696
done()
9797
})
9898

@@ -101,8 +101,8 @@ describe('Node parser reference content', () => {
101101

102102
GQL.jsonToHTML({entry, paths})
103103

104-
expect(entry[0].single_rte).toEqual('<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>')
105-
expect(entry[0].multiple_rte).toEqual(['<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>'])
104+
expect(entry[0].single_rte).toEqual('<img src=\"/asset_uid_1/dummy.pdf\" alt=\"dummy.pdf\" />')
105+
expect(entry[0].multiple_rte).toEqual(['<img src=\"/asset_uid_1/dummy.pdf\" alt=\"dummy.pdf\" />'])
106106
done()
107107
})
108108

__test__/json-to-html.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
unorderListJson2,
3232
orderListJson2,
3333
testJsonRte,
34-
testJsonAsset} from './mock/json-element-mock'
34+
testJsonAsset,
35+
embeddedAssetAsLinkJsonEntry} from './mock/json-element-mock'
3536
import {
3637
blockquoteHtml,
3738
codeHtml,
@@ -116,7 +117,7 @@ describe('Node parser paragraph content', () => {
116117

117118

118119
describe('Node parser reference content', () => {
119-
it.skip('Should render reference asset to html from Entry', done => {
120+
it('Should render reference asset to html from Entry', done => {
120121
const entry = {...embeddedAssetJsonEntry}
121122

122123
jsonToHTML({entry, paths: ['rich_text_editor', 'rte']})
@@ -126,7 +127,7 @@ describe('Node parser reference content', () => {
126127
done()
127128
})
128129

129-
it.skip('Should render reference asset to html from Entries', done => {
130+
it('Should render reference asset to html from Entries', done => {
130131
const entry = [{ ...embeddedAssetJsonEntry }]
131132

132133
jsonToHTML({entry, paths: ['rich_text_editor', 'rte']})
@@ -136,7 +137,7 @@ describe('Node parser reference content', () => {
136137
done()
137138
})
138139

139-
it.skip('Should render reference asset to html from Entry with custom render option', done => {
140+
it('Should render reference asset to html from Entry with custom render option', done => {
140141
const entry = {...embeddedAssetJsonEntry}
141142

142143
jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption})
@@ -146,7 +147,7 @@ describe('Node parser reference content', () => {
146147
done()
147148
})
148149

149-
it.skip('Should render reference asset to html from Entries with custom render option', done => {
150+
it('Should render reference asset to html from Entries with custom render option', done => {
150151
const entry = [{ ...embeddedAssetJsonEntry }]
151152

152153
jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption})

__test__/mock/json-element-mock-result.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const classAndIdAttrsHtml = "<a class=\"class_a\" id=\"id_p\" href=\"LINK.com\">
2222
const styleObjHtml = "<h1 style=\"text-align:justify;\">heading1</h1><h2 style=\"text-align:left;\">heading2</h2><h3 style=\"text-align:right;\">heading3</h3><h4 style=\"text-align:justify;\">heading4</h4><h5 style=\"text-align:justify;\">heading5</h5><h6 style=\"text-align:justify;\">heading6</h6>"
2323
const referenceObjHtml = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-3\" target=\"_blank\">Open entry as a link in new tab</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-2\" target=\"_self\">Bold entry</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-4\" target=\"_blank\"><strong>Bold entry open in new tab</strong></a></p>"
2424
const referenceObjHtmlBlock = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/Test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Embed entry as a link open in new tab</a></p><ul><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\">Entry as a link</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Open entry as a link in new tab</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\"><strong><u>Entry as a link bold</u></strong></a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"khjgf\" target=\"_blank\"><strong><u>Open bold entry as a link in new tab </u></strong></a></li><li><a href=\"https://\" target=\"_self\"><strong><u>Link URL</u></strong></a></li><li><a href=\"https://\" target=\"_blank\"><strong><u>Open link in new tab</u></strong></a></li></ul>"
25-
const imagetags = "<figure style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\"><a href=\"https://batman.com\" target=\"_blank\"><img asset_uid=\"bltb87e0bd5764c421e\" src=\"https://images.contentstack.io/v3/assets/***REMOVED***/bltb87e0bd5764c421e/64abd49b7b26dfaeede17525/batman.png\" alt=\"batman\" target=\"_blank\" style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\" /></a><figcaption>The Batman</figcaption></figure>"
25+
26+
const imagetags = "<figure style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\"><a href=\"https://batman.com\" target=\"_blank\"><img asset_uid=\"asset-UID\" class=\"embedded-asset\" src=\"https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png\" alt=\"batman\" target=\"_blank\" style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\" /></a><figcaption>The Batman</figcaption></figure>"
27+
2628
export {
2729
h1Html,
2830
h2Html,

__test__/mock/json-element-mock.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,41 @@ const paragraphJsonArrayEntry = {
11381138

11391139
}
11401140

1141+
const embeddedAssetAsLinkJsonEntry = {
1142+
uid: 'entry_uid',
1143+
rte_data: {
1144+
type: 'doc',
1145+
attrs: {},
1146+
uid: 'rte_uid',
1147+
children: [
1148+
{
1149+
type: 'p',
1150+
uid: 'p_uid',
1151+
attrs: {},
1152+
children: [
1153+
{ text: '' },
1154+
{
1155+
uid: 'ref_uid',
1156+
type: 'reference',
1157+
attrs: {
1158+
'display-type': 'link',
1159+
type: 'asset',
1160+
'class-name': 'embedded-entry redactor-component undefined-entry',
1161+
'asset-uid': 'asset_uid_2',
1162+
'content-type-uid': 'sys_assets',
1163+
target: '_self',
1164+
href: 'https://picsum.photos/200'
1165+
},
1166+
children: [ { text: 'Door matching', bold: true, underline: true } ]
1167+
},
1168+
{ text: '' }
1169+
]
1170+
}
1171+
],
1172+
_version: 4
1173+
}
1174+
}
1175+
11411176
const embeddedAssetJsonEntry = {
11421177
uid: 'entry_uid',
11431178
rich_text_editor: {...assetReferenceJson},
@@ -2258,16 +2293,16 @@ const testJsonAsset={
22582293
"json_rte":{
22592294
"type": "doc",
22602295
"attrs": {},
2261-
"uid": "ddec1e08f4634eaca512b113ba4da946",
2296+
"uid": "UID",
22622297
"children": [
22632298
{
2264-
"uid": "7dd9bbe6b18449b8b83cabe084b02da4",
2299+
"uid": "UID",
22652300
"type": "reference",
22662301
"attrs": {
22672302
"display-type": "display",
2268-
"asset-uid": "bltb87e0bd5764c421e",
2303+
"asset-uid": "asset-UID",
22692304
"content-type-uid": "sys_assets",
2270-
"asset-link": "https://images.contentstack.io/v3/assets/***REMOVED***/bltb87e0bd5764c421e/64abd49b7b26dfaeede17525/batman.png",
2305+
"asset-link": "https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png",
22712306
"asset-name": "batman.png",
22722307
"asset-type": "image/png",
22732308
"type": "asset",
@@ -2328,9 +2363,10 @@ export {
23282363
blockquoteJson,
23292364
paragraphEntry,
23302365
unorderListJson,
2331-
assetReferenceJson,
2366+
assetReferenceJson,
23322367
paragraphJsonArrayEntry,
23332368
embeddedAssetJsonEntry,
2369+
embeddedAssetAsLinkJsonEntry,
23342370
embeddedEntryJsonEntry,
23352371
entryReferenceLinkJson,
23362372
entryReferenceBlockJson,

__test__/reference-to-html.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ describe('Reference Node To HTML', () => {
152152
}
153153

154154
const resultHTML = referenceToHTML(node, renderOption)
155-
expect(resultHTML).toEqual('<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>')
155+
expect(resultHTML).toEqual('<img src=https://image.url/11.jpg>')
156156
done()
157157
})
158158
it('Should return image for undefined node asset from default node option', done => {
159159
const node = assetReferenceJson.children[0] as unknown as Node
160160

161161
const resultHTML = referenceToHTML(node, defaultNodeOption)
162-
expect(resultHTML).toEqual('<figure><img asset_uid=\"asset_uid_1\" src=\"https://image.url/11.jpg\" /></figure>')
162+
expect(resultHTML).toEqual('<figure><img asset_uid=\"asset_uid_1\" class=\"embedded-asset\" src=\"https://image.url/11.jpg\" /></figure>')
163163
done()
164164
})
165165

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/utils",
3-
"version": "1.3.9",
3+
"version": "1.3.10",
44
"description": "Contentstack utilities for Javascript",
55
"main": "dist/index.es.js",
66
"types": "dist/types/index.d.ts",

src/helper/enumerate-entries.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,47 +77,6 @@ export function referenceToHTML(
7777
renderOption: RenderOption,
7878
renderEmbed?: (metadata: Metadata) => EmbeddedItem | EntryNode,
7979
): string {
80-
if ( node.children!== undefined && node.attrs.type === 'asset' && node.attrs['content-type-uid'] === 'sys_assets') {
81-
82-
// Extract image information
83-
const src = node.attrs['asset-link'];
84-
const alt = node.attrs?.['redactor-attributes']?.['alt'];
85-
const link = node.attrs.link;
86-
const target = node.attrs.target || "";
87-
const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || "";
88-
const style = node.attrs.style;
89-
const asset_uid= node.attrs['asset-uid'];
90-
91-
// Build img tag with optional attributes
92-
let imageTag = `<img `;
93-
94-
if(node.attrs['asset-uid']){
95-
imageTag += `asset_uid="${asset_uid}"`;
96-
}
97-
if(node.attrs['asset-link']){
98-
imageTag += ` src="${src}"`;
99-
}
100-
if (node.attrs?.['redactor-attributes']?.['alt']) {
101-
imageTag += ` alt="${alt}"`;
102-
}
103-
if (node.attrs.target === "_blank") {
104-
imageTag += ` target="_blank"`;
105-
}
106-
if (node.attrs.style) {
107-
imageTag += ` style="${style}"`;
108-
}
109-
110-
imageTag += " />";
111-
112-
const figureTag = `<figure${style ? ` style="${style}"` : ''}>` +
113-
(link ? `<a href="${link}" target="${target || ""}">` : "") +
114-
imageTag +
115-
(link ? `</a>` : "") +
116-
(caption ? `<figcaption>${caption}</figcaption>` : "") +
117-
`</figure>`;
118-
return figureTag;
119-
}
120-
12180
if (node.attrs.type === 'entry' && node.attrs['display-type'] === 'link') {
12281
const entryText = node.children ? nodeChildrenToHTML(node.children, renderOption, renderEmbed) : '';
12382
if (node.attrs.target) {

src/options/default-node-options.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,22 @@ export const defaultNodeOption: RenderOption = {
105105

106106
['reference']:(node: Node, next: Next) => {
107107
if (node.attrs.type === 'asset') {
108-
return `<img${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${sanitizeHTML(node.attrs['class-name'])}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} src="${sanitizeHTML(node.attrs['asset-link'])}" />`
108+
const src = node.attrs['asset-link'];
109+
const alt = node.attrs?.['redactor-attributes']?.['alt'];
110+
const link = node.attrs.link;
111+
const target = node.attrs.target || "";
112+
const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || "";
113+
const style = node.attrs.style;
114+
const asset_uid= node.attrs['asset-uid'];
115+
116+
let imageTag = `<img${asset_uid ? ` asset_uid="${asset_uid}"` : `` }${node.attrs['class-name'] ? ` class="${sanitizeHTML(node.attrs['class-name'])}"`: ``}${src ? ` src="${sanitizeHTML(src)}"` : ``}${alt ? ` alt="${alt}"` : `` }${target === "_blank" ? ` target="_blank"` : `` }${style ? ` style="${style}"` : `` } />`;
117+
118+
return `<figure${style ? ` style="${style}"` : ''}>` +
119+
(link ? `<a href="${link}" target="${target || ""}">` : "") +
120+
imageTag +
121+
(link ? `</a>` : "") +
122+
(caption ? `<figcaption>${caption}</figcaption>` : "") +
123+
`</figure>`;
109124
}
110125
return ``
111126
},

0 commit comments

Comments
 (0)