Skip to content

Commit 42090b4

Browse files
committed
docs: 📝 Readme updated
1 parent 492a2b1 commit 42090b4

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,112 @@
22
![Coverage branches](./badges/badge-branches.svg)
33
![Coverage functions](./badges/badge-functions.svg)
44
![Coverage lines](./badges/badge-lines.svg)
5+
6+
# Contentstack JavaScript Utils SDK:
7+
8+
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.
9+
10+
This guide will help you get started with Contentstack JavaScript Utils SDK to build apps powered by Contentstack.
11+
12+
## Prerequisites
13+
14+
To get started with JavaScript, you will need the following:
15+
- Node.js 10 or later
16+
17+
## SDK Installation and Setup
18+
> Note: If you are using JavaScript Contentstack SDK, you don’t need to run the command as ‘@contentstack/utils’ is already imported in the SDK.
19+
20+
Use the following command to install Contentstack JavaScript Utils SDK:
21+
```sh
22+
npm i @contentstack/utils
23+
```
24+
## Usage
25+
Let’s learn how you can use Utils SDK to render embedded items.
26+
27+
### Create Render Option
28+
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:
29+
```js
30+
const renderOptions = {
31+
//to render block-type embedded items
32+
block: {
33+
'product': (entry, metadata) => {
34+
`<div>
35+
<h2 >${entry.title}</h2>
36+
<img src=${entry.product_image.url} alt=${entry.product_image.title}/>
37+
<p>${entry.price}</p>
38+
</div>`
39+
},
40+
//to render the default
41+
'$default': (entry, metadata) => {
42+
`<div>
43+
<h2>${entry.title}</h2>
44+
<p>${ntry.description}</p>
45+
</div>`
46+
}
47+
},
48+
//to display inline embedded items
49+
inline: {
50+
'$default': (entry) => {
51+
`<span><b>${entry.title}</b> - ${entry.description}</span>`
52+
}
53+
},
54+
//to display embedded items inserted via link
55+
link: (entry, metadata) => {
56+
`<a href="${metadata.attributes.href}">${metadata.text}</a>`
57+
},
58+
// to display assets
59+
display: (asset, metadata) => {
60+
`<img src=${metadata.attributes.src} alt=${metadata.alt} />`
61+
}
62+
}
63+
```
64+
65+
## Basic Queries
66+
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
67+
68+
### Fetch Embedded Item(s) from a Single Entry
69+
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the includeEmbeddedItems and Contentstack.Utils.render functions as shown below:
70+
```js
71+
import * as Contentstack from 'contentstack'
72+
const stack = Contentstack.Stack({
73+
api_key: '<API_KEY>',
74+
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
75+
environment: '<ENVIRONMENT>'})
76+
77+
stack.ContentType('<CONTENT_TYPE_UID>')
78+
.Entry('<ENTRY_UID>')
79+
.toJSON()
80+
.includeEmbeddedItems() // include embedded items
81+
.fetch()
82+
.then(entry => {
83+
Contentstack.Utils.render({ entry, renderOption })
84+
})
85+
```
86+
If you have multiple RTE fields in an entry and want to fetch the embedded items from a particular RTE field, you need to provide a path of those RTE fields.
87+
88+
Refer to the example code below:
89+
```js
90+
//code to render embedded item from an RTE field and from another RTE field nested within a group field
91+
Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rtefieldUID"], renderOption })
92+
```
93+
### Fetch Embedded Item(s) from Multiple Entries
94+
To get embedded items from multiple entries, you need to provide the content type UID. You can also use the path variable in case the entries have multiple RTE fields.
95+
```js
96+
import Contentstack from 'contentstack'
97+
const stack = Contentstack.Stack({
98+
api_key: '<API_KEY>',
99+
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
100+
environment: '<ENVIRONMENT>'})
101+
102+
stack.ContentType('<CONTENT_TYPE_UID>')
103+
.Query()
104+
.toJSON()
105+
.where('title', '<entry_title_to_search>')
106+
.includeEmbeddedItems() // include embedded items
107+
.find()
108+
.then(result => {
109+
result.forEach(entry => {
110+
Contentstack.Utils.render({ entry, path: ['rte', 'group.rtefieldUID'], renderOption })
111+
})
112+
})
113+
```

badges/badge-branches.svg

Lines changed: 1 addition & 1 deletion
Loading

src/render-embedded-objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { findRenderContent } from './helper/find-render-content';
77
/**
88
*
99
* @param {EntryEmbedable| EntryEmbedable[]} entry - Objects that contains RTE with embedded objects
10-
* @param {string[]} keyPaths - Key paths for RTE contents in Entry object
10+
* @param {string[]} paths - Key paths for RTE contents in Entry object
1111
* @param {RenderOption?} renderOption - Optional render options to render content
1212
*/
1313
export function render(option: {

0 commit comments

Comments
 (0)