Contentstack utilities for Javascript
The @contentstack/utils package is an essential toolkit for developers working with Contentstack, a leading headless CMS that provides robust API-first content management solutions. This node module significantly simplifies interactions with Contentstack by providing utility functions that help manage and manipulate content delivery across various platforms. Whether you are developing a web application, mobile app, or any other digital product, this package can enhance your project's ability to handle rich text rendering, modular blocks, and linked content resolution seamlessly. It makes content operations more efficient, thereby allowing developers to focus more on creating high-quality user experiences rather than dealing with the complexities of content management.
To incorporate these utilities into your project, you can easily get started by running the command 'npm install @contentstack/utils'. This installation process integrates the utilities into your node environment, setting the stage for you to leverage its functions for content management tasks. Once installed, developers can utilize this package to render rich text and images stored in Contentstack, convert JSON to HTML or markdown, and resolve links and entries automatically. This not only speeds up the development process but also ensures consistency and reliability in how content is handled across different parts of your application.
The @contentstack/utils package is continuously updated to align with the latest advancements in web technology and Contentstack’s features. This ensures that developers have access to the most current tools needed for effective content management. The utilities provided by the package are designed to be flexible and adaptable, making it an ideal choice for developers looking to implement a headless CMS architecture. By reducing the time and effort required to code complex content structures, this package helps in maintaining a cleaner codebase and a more scalable application structure.
Contentstack provides the Live Preview SDK to establish a communication channel between the various...
Read moreThe Content Management API is used to manage the content of your Contentstack...
Read moreThis Package converts Html Document to Json and vice-versa...
Read moreGatsby source plugin for building websites using Contentstack as a data...
Read moreThe Extensions SDK allows you to extend Contentstack’s UI by helping you create Custom Fields,...
Read moreGatsby source plugin for building websites using Contentstack as a data...
Read moreThe Contentstack App SDK allows you to customize your Contentstack applications...
Read moreCore dependencies of this npm package and its dev dependencies.
cheerio, dompurify, @babel/preset-env, @commitlint/cli, @commitlint/config-conventional, @types/dompurify, @types/jest, babel-core, babel-jest, babel-loader, babel-preset-es2015, commitizen, eslint, husky, jest, jest-coverage-badges, jest-environment-jsdom, jest-html-reporters, jest-junit, jsdom, jsdom-global, prettier, rollup, rollup-plugin-json, rollup-plugin-node-resolve, rollup-plugin-sourcemaps, rollup-plugin-typescript2, ts-jest, ts-node, tslint, tslint-config-prettier, typescript
A README file for the @contentstack/utils code repository. View Code
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 RTE embedded items and Supercharged RTE Json to HTML.
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 renderOption = {
// to render Supercharged RTE NodeType content like paragraph, link, table, order list, un-order list and more.
p: (node, next) => {
return `<p class='class-id'>${next(node.children)}</p>` // you will need to call next function with node children contents
},
h1: (node, next) => {
return `<h1 class='class-id'>${next(node.children)}</h1>` // you will need to call next function with node children contents
},
// to render Supercharged RTE MarkType content like bold, italic, underline, strikethrough, inlineCode, subscript, and superscript
bold: (text) => {
return `<b>${next(node.children)}</b>`
},
// to render block-type embedded items
block: {
'product': (item, metadata) => {
return `<div>
<h2 >${item.title}</h2>
<img src=${item.product_image.url} alt=${item.product_image.title}/>
<p>${item.price}</p>
</div>`
},
// to render the default
'$default': (item, metadata) => {
return `<div>
<h2>${item.title}</h2>
<p>${item.description}</p>
</div>`
}
},
// to display inline embedded items
inline: {
'$default': (item, metadata) => {
return `<span><b>${item.title}</b> - ${item.description}</span>`
}
},
// to display embedded items inserted via link
link: (item, metadata) => {
return `<a href="${metadata.attributes.href}">${metadata.text}</a>`
},
// to display assets
display: (item, metadata) => {
return `<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 a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use Contentstack.Utils.jsonToHTML
function 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()
.fetch()
.then(entry => {
Contentstack.Utils.jsonToHTML({
entry,
path: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use
includeEmbeddedItems
function.
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
})
})
})
To get a multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use Contentstack.Utils.jsonToHTML
function 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>')
.Query()
.toJSON()
.where('title', '<entry_title_to_search>')
.find()
.then(result => {
result.forEach(entry => {
Contentstack.Utils.jsonToHTML({
entry,
path: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
})
Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use
includeEmbeddedItems
function.