
Product
Socket Brings Supply Chain Security to skills.sh
Socket is now scanning AI agent skills across multiple languages and ecosystems, detecting malicious behavior before developers install, starting with skills.sh's 60,000+ skills.
@charlietango/umbraco-rich-text
Advanced tools
React component for working with rich text from the Umbraco Content Delivery API.
Takes the JSON rich text output from the Umbraco Content Delivery API and renders it with React.
[!IMPORTANT]
You need to enable theRichTextOutputAsJsonoption in the Content Delivery API. See Content Delivery API configuration for details.
Install the @charlietango/umbraco-rich-text package with your package manager
of choice.
npm install @charlietango/umbraco-rich-text
<UmbracoRichText>element: The rich text property from the Umbraco Content Delivery API.renderBlock: Render a specific block type. Return null to skip rendering
the node.renderNode: Overwrite the default rendering of a node. Return undefined to
render the default node. Return null to skip rendering the node.
meta: A function you can call to retrieve metadata describing the current
element’s ancestor, children, previous, and next siblings. The
values are built lazily when you invoke the function, keeping memory lower
when renderNode doesn’t need the metadata for a given node.htmlAttributes: Default attributes to set on the defined HTML elements.
These will be used, unless the element already has the attribute set. The only
exception is the className attribute, which will be merged with the default
value.stripStyles: Remove inline style attributes from HTML elements. Accepts:
true: Removes all inline styles from all HTML elementstags: Array of HTML tags from which to strip styles. If not provided,
styles are stripped from all tags.except: Array of HTML tags that should keep their styles, even if they
are in the tags array.false (all inline styles are preserved).When passing the renderBlock and renderNode props, consider making them
static functions (move them outside the consuming component) to avoid
unnecessary re-renders.
import {
UmbracoRichText,
RenderBlockContext,
RenderNodeContext,
} from "@charlietango/umbraco-rich-text";
import Image from "next/image";
import Link from "next/link";
function renderNode({ tag, children, attributes }: RenderNodeContext) {
switch (tag) {
case "a":
return <Link {...attributes}>{children}</Link>;
case "p":
return (
<p className="text-lg" {...attributes}>
{children}
</p>
);
default:
// Return `undefined` to render the default HTML node
return undefined;
}
}
function renderBlock({ content }: RenderBlockContext) {
switch (content?.contentType) {
// Switch over your Umbraco document types that can be rendered in the Rich Text blocks
case "imageBlock":
return <Image {...content.properties} />;
default:
return null;
}
}
function RichText({ data }) {
return (
<UmbracoRichText
data={data.richText}
renderNode={renderNode}
renderBlock={renderBlock}
htmlAttributes={{ p: { className: "mb-4" } }}
stripStyles={{
// Strip styles from all tags except the following:
except: ["img"], // Keep styles on `img` tags
}}
/>
);
}
You can augment the renderBlock method with the generated OpenAPI types from
Umbraco Content Delivery API. That way you can correctly filter the blocks you
are rendering, based on the contentType, and get the associated properties.
Create types/umbraco-rich-text.d.ts, and augment the UmbracoBlockItemModel
interface with your applications definition for ApiBlockItemModel.
To generate the types, you'll want to use the Delivery Api Extensions package, alongside a tool to generate the types from the OpenAPI schema, like openapi-typescript.
types/umbraco-rich-text.d.ts
// Import the `components` generated by OpenAPI TypeScript.
import { components } from "./umbraco-openapi";
// Define the intermediate interface
type ApiBlockItemModel = components["schemas"]["ApiBlockItemModel"];
declare module "@charlietango/umbraco-rich-text" {
interface UmbracoBlockItemModel extends ApiBlockItemModel {}
}
richTextToPlainTextA utility function to convert an Umbraco RichText element to plain text. This can be useful for generating meta descriptions or other text-based properties.
data (RichTextElementModel): The rich text element to be converted.options (Options, optional): An object to specify additional options.
firstParagraph (boolean, optional): If true, only the first
paragraph with text content will be returned.maxLength (number, optional): The maximum length of the returned text.
If the text exceeds this length, it will be truncated to the nearest word
and an ellipsis will be added.ignoreTags (Array<string>, optional): An array of tags to be ignored
during the conversion.string: The plain text representation of the rich text element.import { richTextToPlainText } from "@charlietango/umbraco-rich-text";
const plainText = richTextToPlainText(richTextData);
// Just the first paragraph
const firstParagraph = richTextToPlainText(richTextData, {
firstParagraph: true,
});
// Just the first 100 characters, truncated at the nearest word with an ellipsis
const first100Characters = richTextToPlainText(richTextData, {
maxLength: 100,
});
// Ignore certain tags, skipping their content
const ignoreTags = richTextToPlainText(richTextData, {
ignoreTags: ["h1", "h2", "ol", "figure"],
});
renderNode and renderBlockYou can pass additional values to the renderNode and renderBlock functions,
by making an inline function that returns the renderNode or renderBlock
function. This can be useful if you need to pass extra context or props to the
rendering functions. E.g. sizes for images, translations, or other data that
is not part of the rich text element.
import {
UmbracoRichText,
RenderBlockContext,
RenderNodeContext,
} from "@charlietango/umbraco-rich-text";
import Image from "next/image";
import Link from "next/link";
function renderNode(
{ tag, children, attributes }: RenderNodeContext,
extra: { sizes: string },
) {
switch (tag) {
case "img":
return <img {...attributes} sizes={extra.sizes} />;
default:
return undefined;
}
}
function RichText({ data }) {
return (
<UmbracoRichText
data={data.richText}
renderNode={(node) => {
return renderNode(node, { sizes: "720vw" });
}}
/>
);
}
FAQs
React component for working with rich text from the Umbraco Content Delivery API.
We found that @charlietango/umbraco-rich-text demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket is now scanning AI agent skills across multiple languages and ecosystems, detecting malicious behavior before developers install, starting with skills.sh's 60,000+ skills.

Product
Socket now supports PHP with full Composer and Packagist integration, enabling developers to search packages, generate SBOMs, and protect their PHP dependencies from supply chain threats.

Security News
An AI agent is merging PRs into major OSS projects and cold-emailing maintainers to drum up more work.