prismic-dom

Replaced by @prismicio/helpers

prismic-dom is deprecated and replaced by @prismicio/helpers v2. All functions from prismic-dom have been moved into the more general @prismicio/helpers package.

See the @prismicio/helpers v2 Migration Guide to learn how to upgrade your project.

Overview

prismic-dom is one of two fundamental Prismic packages for creating web apps with Prismic and JavaScript. It is primarily responsible for rendering data from the Prismic API as HTML. The other is @prismicio/client, which helps with querying the API.

Dependencies & Requirements

This package works with the Prismic API V2. You can tell if you're using V2 if your endpoint URL includes prismic.io/api/v2.

Installation

  • npm
  • Yarn
npm
Copy
npm install prismic-dom
Yarn
Copy
yarn add prismic-dom

Example

In this example, we will use @prismicio/client to query content from a Prismic repository, and prismic-dom to render the content we receive.

Copy
const PrismicDom = require('prismic-dom')
const Prismic = require('@prismicio/client')

const endpoint = 'https://your-repo-name.cdn.prismic.io/api/v2'
const client = Prismic.client(endpoint)

const init = async () => {
  const { data } = await client.getByUID('page', 'quickstart')
  console.log(PrismicDom.RichText.asHtml(data.description))
  // <p>This <em>should</em> log your formatted text.</p>
}

init()

Usage

This section assumes you have required prismic-dom in your project and stored it in the variable PrismicDom, and that you have a document from the Prismic API stored in the variable document (using @prismicio/client):

Copy
const PrismicDom = require('prismic-dom')

async () => {
  const document = await client.getByUID('page', 'example-document')
  // all operations happen here
}

For the following examples, the document has this structure:

Copy
{
  uid: 'example-document',
  type: 'page',
  // document metadata
  // ...
  data: {
    date: '2018-01-16' // A date field
    link: {...} // An internal link to a blog post with the UID "vacation"
    title: {...} // An h2 title field with the content "This is a Title"
    description: {...} // A block of rich text with formatting
  }
}

prismic-dom offers four methods for rendering HTML:

Date

PrismicDom.Date() takes a Date field from the Prismic and converts it to an ISO date.

A Date object is a built-in JavaScript object for storing date and time. To learn more about the JavaScript date object, see this tutorial by Tania Rascia.

By default, Prismic dates are formatted: '2020-11-19T16:45:39+0000'.

Copy
PrismicDom.Date(document.data.date)
// 2018-01-16T13:46:15.000Z

PrismicDom.Date(document.data.date).getFullYear()
// 2018

Link.url

PrismicDom.Link.url() takes a link from the API and a Link Resolver function and returns a URL. (See above for an explanation of Link Resolvers.) Works for Prismic document links, media links, and web links.

Copy
const linkResolver = (document) => {
  if (document.type === 'blog') return "/post/" + document.uid;
  else return "/" + document.uid;
};

PrismicDOM.Link.url(document.data.link, linkResolver)
// /blog/vacation

What is a Link Resolver?

A Link Resolver is a function that formats internal links based on document data. For instance, a document of type "blog_post" with a UID of "vacation" could have the URL path /blog/vacation.

A Link Resolver would look like this:

const linkResolver = (document) => "/" + document.data.uid

That example will return the document's UID as the URL path.

RichText.asText

PrismicDom.RichText.asText() takes a Rich Text array from the Prismic API and converts it to plain text.

Copy
PrismicDOM.RichText.asText(document.title)
// "This is a Title"

RichText.asHtml

PrismicDom.RichText.asHtml() takes a Rich Text array from the Prismic API, a Link Resolver function, and an HTML Serializer function, and returns rendered HTML. (See above for an explanation of Link Resolvers.)

Copy
PrismicDom.RichText.asHtml(document.data.description, linkResolver, htmlSerializer)
// <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p>Ut enim ad minim veniam.</p>

Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.