Rich Text
Contents
In this chapter you’ll render rich text content, including its links and images.
Intro
Enonic has a rich text form item which, unlike basic text fields, may contain more complex concepts such as tables, images, links and custom components. Each of these typically needs to be processed during rendering to provide a contextually correct URL, ideal image sizes, etc.
Sample content
The sample data set contains everything you need to get going. The Bio field for persons is a rich text form item, and some persons are even populated with relevant content, like you see below:
Task: Render rich text
Follow these steps to implement rendering of the Bio field in Next.js:
-
Update the Person query - To properly render rich text, you need access to additional metadata (such as links and images) within it.
Next.XP provides a
richTextQueryhelper function that generates the query for you. This way you don’t have to repeat the query for every rich text field.Add a new query file to your project with the following content:
src/components/queries/getPersonWithBio.tsimport { APP_NAME_UNDERSCORED, imageUrlQuery, pageUrlQuery, richTextQuery } from '@enonic/nextjs-adapter'; const getPersonWithBio = () => ` query { guillotine(scopeKey: $scopeKey, branch: $branch, project: $project) { get(key:$path) { displayName ... on ${APP_NAME_UNDERSCORED}_Person { data { ${richTextQuery('bio')} dateofbirth photos { ... on media_Image { ${imageUrlQuery({ scale: 'width(500)' })} attachments { name } } } } } parent { _path ${pageUrlQuery()} } } } }`; export default getPersonWithBio;Since ${richTextQuery('bio')}depends on the registered macros, thegetPersonWithBioquery is a function! This guarantees that it is executed when querying for data, and all the macros are already registered.RichTextViewrewrites mediasrc,srcsetandhrefattributes to absolute XP media URLs and content links to locale-prefixed Next.js routes, so no URL handling is needed in your views.The processed HTML from Guillotine 9 contains placeholders. The adapter matches
data-link-refanddata-image-refwith the structured links and images, preserving page-link query strings and fragments. Apps providing macros and image styles must be configured on the selected site or project.For further insight, visit the rich text chapter in the Developer 101 tutorial, or have a look at the React SPA tutorial, which covers Enonic’s standard React components.
-
Add the RichTextView component - With the data in place, use the
RichTextViewcomponent, which knows how to render it. Pass the value of thebiofield from the query response to theRichTextViewcomponent.Create a new component with the following content:
src/components/views/PersonWithBio.tsximport React from 'react'; import { FetchContentResult, I18n, imageUrl, pageUrl } from '@enonic/nextjs-adapter'; import Link from 'next/link'; import RichTextView from '@enonic/nextjs-adapter/views/RichTextView'; import styles from './PersonWithBio.module.css'; const PersonWithBio = (props: FetchContentResult) => { const {displayName, data, parent} = props.data?.get as any; const {bio, photos} = data; const meta = props.meta; return ( <> <div className={styles.person}> <h2>{displayName}</h2> <RichTextView className={styles.bio} data={bio} meta={meta}></RichTextView> { photos.length && <h4 className={styles.photosheader}>Photos</h4> } <div className={styles.photos}> { photos.filter((p: any) => !!p).map((photo: any, i: number) => ( <img key={i} src={imageUrl(photo.imageUrl, meta)} title={getTitle(photo, displayName)} alt={getTitle(photo, displayName)} width="500" /> )) } </div> </div> <p><Link href={pageUrl(parent.pageUrl, meta)} data-content-path={parent._path}>{I18n.localize( 'back')}</Link></p> </> ) } export default PersonWithBio; function getTitle(photo: any, displayName: string) { return (photo.attachments || [])[0].name || displayName; }Add styling to the new component:
src/components/views/PersonWithBio.module.css.person, .person .bio { font-family: sans-serif; } .person { display: flex; flex-direction: column; } .bio { line-height: 24px; } .bio figcaption { font-size: 12px; } .bio figure > img { width: 100%; height: auto; } .photos { padding: 0 20px; display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-gap: 50px; gap: 50px; } .photos img { width: 100%; height: auto; /* Ensures images scale properly */ } @media screen and (max-width: 720px) { .bio figure { width: auto !important; } } -
Update the mapping - Update the mappings to use the new query and view.
Replace the
getPersonquery withgetPersonWithBioand thePersonview withPersonWithBio. It should look something like this:src/components/_mappings.tsimport getPersonWithBio from './queries/getPersonWithBio'; import PersonWithBio from './views/PersonWithBio'; ComponentRegistry.addContentType(`${APP_NAME}:person`, { query: getPersonWithBio, view: PersonWithBio }); -
Verify the rendering - The resulting page for Lea Seydough should look like this:
Summary
You’ve added a query that uses richTextQuery, and a view that renders the bio field with RichTextView, including its links and images.
As the screenshot shows, one component in the bio is still missing. Next, you’ll fix this by rendering macros.