Resolve rich text content
Your content creators can put all sorts of content in rich text elements. This is where most of your longer-form content lives, and it must be displayed correctly. Learn how to resolve Kontent.ai-specific entities, such as content items and components in rich text, so that your content always looks as intended.
What can go in the rich text?
When working in the Kontent.ai rich text editor, your content creators need the freedom to add various types of content. This spans native options such as formatted text structured with headings, lists, and tables to custom content specific to your project. For example, content such as disclaimers, tweets, or videos is something you define yourself. To add these kinds of custom content, content creators can insert either new components or existing content items within the rich text element. Then, based on the content types of the components or content items, your app can decide how to display them. The same goes for linking to other content items within the rich text element. Your app needs to decide how to resolve each link. For example, you might want to use specific routing logic for different types of content items.Resolve inserted content items and components
Let’s see how to resolve tweets inserted into rich text in articles. Rest assured, you can use this approach for any other type of structured content.If you don’t adjust your application, any content item or component inserted in rich text elements resolves to an empty object reference, which won’t be rendered on the page.
For the final HTML, you can also work with embedded tweets, such as by calling the Twitter API
.
1. Implement a tweet resolver
To ensure your tweet is displayed exactly as you want, create a Tweet resolver.// Tip: Find more about Java SDK at https://kontent.ai/learn/java
public class TweetInlineContentItemsResolver extends InlineContentItemsResolver<Tweet> {
@Override
String resolve() {
return "<blockquote class=\"twitter-tweet\" data-lang=\"en\" data-theme=\"" + tweet.theme[0].codename + "\"><a href=\"" + tweet.tweetLink.url + "\"></a></blockquote>"
}
}
2. Register the resolver
Register the resolver to ensure it’s used.// Tip: Find more about Java SDK at https://kontent.ai/learn/java
DeliveryClient client = new DeliveryClient("KONTENT_AI_ENVIRONMENT_ID");
client.registerInlineContentItemsResolver(new TweetInlineContentItemsResolver());
3. Retrieve the rich text
When your app knows how to resolve strings with tweets in them, it automatically resolves the tweet in the article body.// Tip: Find more about Java SDK at https://kontent.ai/learn/java
SimpleArticle item = client.getItem("my_article", SimpleArticle.class);
String description = item.toCompletableFuture().get().getBodyCopy();
Resolve content item links
Let’s see how to resolve links to content items used within the body of articles. With proper resolution logic, any content item link will resolve to the right URL.
When building the resolver logic, you can use the
The content item link in the rich text element is now correctly resolved to a valid URL.
1. Define a model
If you’re using strongly typed models, add a model for each type of content item you want to link within rich text.2. Implement a resolver
To ensure your links are resolved correctly, you need to implement a resolver with two methods:- The first method will return the URL of an available item.
- The second method will return a 404 Not found error when the linked item is not available.
// Tip: Find more about Java SDK at https://kontent.ai/learn/java
public class CustomContentLinkUrlResolver implements ContentLinkUrlResolver {
@Override
String resolveLinkUrl(Link link) {
// Resolves URLs to content items based on the Article content type
if ("my_article".equals(link.getCodename())) {
return String.format("/articles/%s", link.getUrlSlug());
}
}
}
public class CustomBrokenContentLinkUrlResolver implements BrokenLinkUrlResolver {
@Override
String resolveBrokenLinkUrl() {
// Resolves URLs to unavailable content items
return "/404";
}
}
link
parameter to get more information about the linked content items.
3. Register the resolver
Register the resolver to ensure it’s used.// You can also register lambdas with the DeliveryClient as the resolvers are functional interfaces: https://kontent.ai/learn/java-register-resolver
// Sets the resolver as an optional dependency of the DeliveryClient
DeliveryClient client = new DeliveryClient("KONTENT_AI_ENVIRONMENT_ID");
client.setContentLinkUrlResolver(new CustomContentLinkUrlResolver());
client.setBrokenLinkUrlResolver(new CustomBrokenContentLinkUrlResolver());
4. Retrieve the article
Once your app knows how to resolve links, you can retrieve an article, and the content item links will be included inside the article body.// Tip: Find more about Java SDK at https://kontent.ai/learn/java
SimpleArticle item = client.getItem("my_article", SimpleArticle.class);
String description = item.toCompletableFuture().get().getBodyCopy();
<p>The used coffee grounds are retained in the filter, while the <a href="/articles/which-brewing-fits-you" data-item-id="65832c4e-8e9c-445f-a001-b9528d13dac8">brewed coffee</a> is collected in a vessel such as a carafe or pot.</p>