Import rich text
In this lesson, you learn how to import rich text content and use it in a content item.
What you'll import
Imagine you want to import a rich text field that looks something like this:
This is composed of a few elements:
- A header
- Some paragraphs with:
- An external link (to Wikipedia)
- An internal link (to
second-page.html
within this website)
- A button
- An unordered list
- An image
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur <a href="https://wikipedia.org">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<a href="https://kontent.ai" class="cta">Buy me</a>
<p> Ut enim ad minim veniam, <a href="second-page.html">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
<ul><li>Lorem ipsum dolor sit amet</li><li>Consectetur adipiscing elit</li><li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li> </ul>
<img src="https://kontent.ai/img/general/logos/kai-black-text.svg" alt="Kontent.ai" />
Setting up your content types
To import this rich text, you'll need to have at least two content types in your project. The first is a type to hold the rich text, for which you can experiment with any rich text in your project or create a very simple type in the UI or via Management API, as in the following examples.// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript
// Using ES6 syntax
import { ManagementClient } from '@kontent-ai/management-sdk';
const client = new ManagementClient({
environmentId: '<YOUR_ENVIRONMENT_ID>',
apiKey: '<YOUR_API_KEY>'
});
const response = await client
.addContentType()
.withData((builder) => {
return {
external_id: 'simple-rich-text',
name: 'Simple Rich Text',
elements: [
{
name: 'Rich Text',
type: 'rich_text',
external_id: 'rich-text'
}
]
};
})
.toPromise();
// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript
// Using ES6 syntax
import { ManagementClient, ElementModels } from '@kontent-ai/management-sdk';
const client = new ManagementClient({
environmentId: '<YOUR_ENVIRONMENT_ID>',
apiKey: '<YOUR_API_KEY>'
});
const response = await client.addContentType()
.withData(
{
external_id: "button",
name: "Button",
elements: [
{
name: "Text",
type: ElementModels.ElementType.Text,
externalId: "button-text"
},
{
name: "Link",
type: ElementModels.ElementType.Text,
external_id: "button-link"
}
]
}
)
.toPromise();
Getting your rich text ready
Now that you have a place to put your rich text, it's time to get the text itself ready. The first step to getting it ready to be used as a string is to take the original HTML and escape line breaks and quotes."<h1>Lorem Ipsum</h1>\n<p>Lorem ipsum dolor sit amet, consectetur <a href=\"https://wikipedia.org\">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n<a href=\"https://kontent.ai\" class=\"cta\">Buy me</a>\n<p>Ut enim ad minim veniam, <a href=\"second-page.html\">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>\n<ul>\n <li>Lorem ipsum dolor sit amet</li>\n <li>Consectetur adipiscing elit</li>\n <li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li>\n</ul><img src=\"https://kontent.ai/img/general/logos/kai-black-text.svg\" alt=\"Kontent.ai\" />"
<a>
nor <img>
tags are allowed as top level elements in rich text.
For the <img>
tag, you can easily convert it to an asset in Kontent.ai by changing it to a <figure>
tag with an external ID: <figure data-asset-external-id=\"rich-text-asset\"></figure>
. As you know if you've imported linked content, Kontent.ai can store references to external IDs even for items that haven't been created yet. So you can always add in the asset later.
For the <a>
tag, you'll want to turn this into a component. This gives you the benefit of having a clear structure for your button that you can use repeatedly. You can then use the structured data for any presentation depending on the context instead of having a hard-coded link that can't work in all situations.
So instead of an <a>
tag, you'll use an <object>
tag defined based on components in Kontent.ai with a unique GUID you can generate yourself. The result will be something like <object type=\"application/kenticocloud\" data-type=\"component\" data-id=\"a2ee7bac-15ff-4dce-a244-012b9f98dd7b\"></object>
.
Linking to other content items
If you were to import your rich text string as it is (with<a href=\"second-page.html\">
), the link would be assumed to be an external link and http://
would be added as a prefix (resulting in <a href=\"http://second-page.html\">
). To fix this, you want to change the link from an external link to an internal one.
You can again take advantage of references to external IDs while creating your internal link so that you can reference an item you'll create later. Just write your <a>
tag as follows: <a data-item-external-id=\"second-page\">
) (or whatever you want the other page to have as an external ID).
Your prepared rich text string
If you've followed the steps above, the string for your rich text should be something like this:"<h1>Lorem Ipsum</h1>\n<p>Lorem ipsum dolor sit amet, consectetur <a href=\"https://wikipedia.org\">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n<object type=\"application/kenticocloud\" data-type=\"component\" data-id=\"a2ee7bac-15ff-4dce-a244-012b9f98dd7b\"></object>\n<p>Ut enim ad minim veniam, <a data-item-external-id=\"second-page\">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>\n<ul>\n <li>Lorem ipsum dolor sit amet</li>\n <li>Consectetur adipiscing elit</li>\n <li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li>\n</ul><figure data-asset-external-id=\"rich-text-asset\"></figure>"
Filling in your component
You have a reference to a component in your rich text string, but you don't yet have a matching component. You need to create an object that you can enter into thecomponents
property of your rich text.
Your button has two elements: its text and the link to be opened. You created a Button content type to hold such components, so now you can use that type to hold your data. Use the external IDs you created when adding the type. A single component
object using the GUID from the rich text would look like this:
{
"id": "a2ee7bac-15ff-4dce-a244-012b9f98dd7b",
"type": {
"external_id": "button"
},
"elements": [
{
"element": {
"external_id":"button-text"
},
"value": "Buy me"
},
{
"element": {
"external_id": "button-link"
},
"value": "https://kontent.ai"
},
]
}
Importing your rich text
Sign in with your Kontent.ai credentials or sign up for free to unlock the full lesson, track your progress, and access exclusive expert insights and tips!