
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@contentstack/utils
Advanced tools
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.
This guide will help you get started with Contentstack JavaScript Utils SDK to build apps powered by Contentstack.
To get started with JavaScript, you will need the following:
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.
Use the following command to install Contentstack JavaScript Utils SDK:
npm i @contentstack/utils
Let’s learn how you can use Utils SDK to render embedded items.
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:
const renderOptions = {
//to render block-type embedded items
block: {
'product': (entry, metadata) => {
`<div>
<h2 >${entry.title}</h2>
<img src=${entry.product_image.url} alt=${entry.product_image.title}/>
<p>${entry.price}</p>
</div>`
},
//to render the default
'$default': (entry, metadata) => {
`<div>
<h2>${entry.title}</h2>
<p>${ntry.description}</p>
</div>`
}
},
//to display inline embedded items
inline: {
'$default': (entry) => {
`<span><b>${entry.title}</b> - ${entry.description}</span>`
}
},
//to display embedded items inserted via link
link: (entry, metadata) => {
`<a href="${metadata.attributes.href}">${metadata.text}</a>`
},
// to display assets
display: (asset, metadata) => {
`<img src=${metadata.attributes.src} alt=${metadata.alt} />`
}
}
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
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:
import * as Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})
stack.ContentType('<CONTENT_TYPE_UID>')
.Entry('<ENTRY_UID>')
.toJSON()
.includeEmbeddedItems() // include embedded items
.fetch()
.then(entry => {
Contentstack.Utils.render({ entry, renderOption })
})
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.
Refer to the example code below:
//code to render embedded item from an RTE field and from another RTE field nested within a group field
Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rtefieldUID"], renderOption })
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.
import Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})
stack.ContentType('<CONTENT_TYPE_UID>')
.Query()
.toJSON()
.where('title', '<entry_title_to_search>')
.includeEmbeddedItems() // include embedded items
.find()
.then(result => {
result.forEach(entry => {
Contentstack.Utils.render({ entry, path: ['rte', 'group.rtefieldUID'], renderOption })
})
})
1.0.0 (2021-04-05)
FAQs
Contentstack utilities for Javascript
We found that @contentstack/utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.