Skip to content

Commit 54c4804

Browse files
chore: Updated rendorOption in readme file (#123)
1 parent a2aa957 commit 54c4804

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Changelog
2+
## [1.3.16](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.16) (2024-12-11)
3+
- Fix: Updated rendorOption code block in reaadme file
24

35
## [1.3.15](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.15) (2024-11-18)
46
- Fix: Added Table merge cell functionality

README.md

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,61 @@ Let’s learn how you can use Utils SDK to render RTE embedded items and Superch
2323
To render embedded items on the front-end, use the renderOptions function, and define the UI elements you want to show in the front-end of your website, as shown in the example below:
2424
```js
2525
const renderOption = {
26-
// to render Supercharged RTE NodeType content like paragraph, link, table, order list, un-order list and more.
27-
p: (node, next) => {
28-
return `<p class='class-id'>${next(node.children)}</p>` // you will need to call next function with node children contents
29-
},
30-
h1: (node, next) => {
31-
return `<h1 class='class-id'>${next(node.children)}</h1>` // you will need to call next function with node children contents
32-
},
33-
// to render Supercharged RTE MarkType content like bold, italic, underline, strikethrough, inlineCode, subscript, and superscript
34-
bold: (text) => {
35-
return `<b>${next(node.children)}</b>`
36-
},
37-
// to render block-type embedded items
38-
block: {
39-
'product': (item, metadata) => {
40-
return `<div>
41-
<h2 >${item.title}</h2>
42-
<img src=${item.product_image.url} alt=${item.product_image.title}/>
43-
<p>${item.price}</p>
44-
</div>`
45-
},
46-
// to render the default
47-
'$default': (item, metadata) => {
48-
return `<div>
49-
<h2>${item.title}</h2>
50-
<p>${item.description}</p>
51-
</div>`
52-
}
53-
},
54-
// to display inline embedded items
55-
inline: {
56-
'$default': (item, metadata) => {
57-
return `<span><b>${item.title}</b> - ${item.description}</span>`
58-
}
59-
},
60-
// to display embedded items inserted via link
61-
link: (item, metadata) => {
62-
return `<a href="${metadata.attributes.href}">${metadata.text}</a>`
63-
},
64-
// to display assets
65-
display: (item, metadata) => {
66-
return `<img src=${metadata.attributes.src} alt=${metadata.alt} />`
67-
}
26+
// To render paragraph nodes
27+
p: (node, next) => `<p class="class-id">${next(node.children)}</p>`,
28+
// To render heading level 1 nodes
29+
h1: (node, next) => `<h1 class="class-id">${next(node.children)}</h1>`,
30+
// To render bold text
31+
bold: (text) => `<b>${text}</b>`,
32+
// To render block-type embedded items
33+
block: {
34+
product: (item) =>
35+
`<div>
36+
<h2>${item.title}</h2>
37+
<img src="${item.product_image.url}" alt="${item.product_image.title}" />
38+
<p>${item.price}</p>
39+
</div>`,
40+
$default: (item) =>
41+
`<div>
42+
<h2>${item.title}</h2>
43+
<p>${item.description}</p>
44+
</div>`,
45+
},
46+
// To render inline embedded items
47+
inline: {
48+
$default: (item) =>
49+
`<span><b>${item.title}</b> - ${item.description}</span>`,
50+
},
51+
// To render link (a) nodes
52+
a: (node) => {
53+
const { attrs, children } = node;
54+
const childText = children.map((child) => child.text || '').join('');
55+
return `<a href="${attrs.url}" target="${attrs.target || '_self'}">${childText}</a>`;
56+
},
57+
// To render embedded assets
58+
display: (item, metadata) => {
59+
const { attributes } = metadata;
60+
return `<img src="${attributes.src}" alt="${attributes.alt || 'Asset'}" />`;
61+
},
62+
// To render embedded entry & asset references
63+
reference: (node) => {
64+
const { attrs, children } = node;
65+
const childText = children.map((child) => child.text || '').join('');
66+
if (attrs.type === 'entry') {
67+
return `<a href="${attrs.href}" class="${attrs['class-name'] || ''}">${childText}</a>`;
68+
} else if (attrs.type === 'asset') {
69+
if (attrs['display-type'] === 'link') {
70+
return `<a href="${attrs['asset-link']}" class="${attrs['class-name'] || ''}" target="_blank">${attrs['asset-name'] || 'View Asset'}</a>`;
71+
}
72+
73+
if (attrs['display-type'] === 'display') {
74+
return `<img src="${attrs['asset-link']}" alt="${attrs['asset-alt'] || attrs['asset-name']}" class="${attrs['class-name'] || ''}" />`;
75+
}
76+
}
77+
return childText; // Fallback for unknown references
78+
},
79+
// Default handler for unknown nodes
80+
default: (node, next) => `<div>${next(node.children)}</div>`,
6881
}
6982
```
7083

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.15",
3+
"version": "1.3.16",
44
"description": "Contentstack utilities for Javascript",
55
"main": "dist/index.es.js",
66
"types": "dist/types/index.d.ts",

0 commit comments

Comments
 (0)