Skip to main content

Rich text

A rich-text field holds the markup the rich-text editor produces, which is not quite HTML, and building it by hand is the main thing this API saves you from. RichText builds it a block at a time, converts prose that was never meant for a rich-text field, and reads or rewrites what is already stored. Every call is listed with its signature under RichText in the reference.

Building markup​

The editor separates paragraphs with two <br/> tags and a single line break with one; it never writes a <p> tag. paragraph and line write exactly that, so what a script produces is what the editor would have produced.

1
const { RichText } = dari;
2
3
const body = new RichText()
4
.paragraph("Winds hit 60mph overnight.")
5
.paragraph(t => t
6
.text("The ")
7
.bold("harbor")
8
.text(" is closed until ")
9
.link("https://example.com/notice", "further notice")
10
.text("."))
11
.list(["Ferries cancelled", "Roads closed"]);
12
13
article.put("body", body);

Text you pass in is escaped, so a value from an API or an AI step cannot break the markup. Use html when you have markup you want written as-is.

CallProduces
paragraph(text)A block of text, separated from its neighbours by <br/><br/>
line(text)A line inside the current block, ending in a single <br/>
list(items) / orderedList(items)A bulleted or numbered list
element(tagName, options)One of your CMS's rich-text elements
elements(tagName)Every occurrence of that element in the markup, in order
rewrite(tagName, visitor)Replaces or removes every occurrence of that element
html(markup)Markup written unchanged
toPlainText() / isEmptyThe readable text, and whether there is any
fromPlainText(text)Plain prose converted, as below (static)

Inside paragraph, line and list, pass a function to mix formatting. It is given an InlineText with text, bold, italic, underline, strikethrough, code, link and html, and wrap(tag, text) for any other inline mark.

Text that was never meant for a rich-text field​

Prose that arrived from somewhere with no idea where it was going, an AI step's answer or an imported description, separates its paragraphs with blank lines. Assigning that string to a rich-text field stores something the editor never writes: the newlines aren't markup, so it renders as one run-on paragraph, and any < or & in it becomes markup the moment it's stored.

tip

Putting an AI Agent step's answer into a rich-text body is the most common case. Always pass it through RichText.fromPlainText(...):

1
article.put("body", RichText.fromPlainText(input.body));

Blank lines become paragraph breaks, single newlines become soft line breaks, and the text is escaped. It returns a RichText, so you can keep building on it if there's more to add.

Rich-text elements​

Anything richer than text and lists, a pull quote, an embedded image, an iframe, is a rich-text element your CMS defines. Add one by its tag name, giving it the attributes and body it expects:

1
body.element("bsp-pull-quote", {
2
attributes: { "data-align": "right", "data-attribution": "Dan" },
3
body: "Nothing like it in thirty years."
4
});

Each element decides for itself where its data goes, and that decision lives in the element's Java code, which cannot run inside a script. Some elements keep each field in its own attribute, some use the body, and some put their whole configuration in a single attribute as serialized state. So you write the attributes and body that element expects, rather than setting fields on it and hoping.

For the last kind, give the attribute a record and it is written as that record's state, which can hold a reference like any other field:

1
const { Record, RecordRef } = dari;
2
3
body.element("bsp-image", {
4
attributes: {
5
"data-state": new Record("com.example.ImageEnhancement")
6
.put("image", new RecordRef(input.imageId))
7
.put("caption", "The harbor at dawn")
8
}
9
});

The attribute name is the element's choice; data-state is a common one, not a rule.

Which elements a field allows depends on your CMS and on that field's toolbar. Ask a developer on your team which tag names apply, which attributes each expects, and whether it reads its body.

Reading and changing existing rich text​

1
const body = RichText.from(article.get("body"));
2
3
body.toPlainText(); // the readable text, markup removed
4
body.isEmpty;
5
body.elements("bsp-pull-quote"); // every pull quote in the body

rewrite visits every occurrence of one element. Return the element to keep it, another to replace it, or null to remove it:

1
body.rewrite("bsp-pull-quote", el => {
2
if (el.attribute("data-align") === "left") {
3
return null;
4
}
5
el.attribute("data-align", "center");
6
return el;
7
});
8
9
article.put("body", body);

An element you are given exposes exactly what is in the markup: tagName, body, and attribute(name). Where an attribute holds serialized state, record(name) reads it back as a record:

1
body.rewrite("bsp-image", el => {
2
const state = el.record("data-state");
3
state.put("caption", state.get("caption").toUpperCase());
4
return el.attribute("data-state", state);
5
});

Everything rewrite does not visit is left exactly as it was, so a script that changes one pull quote cannot disturb the rest of the body.

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.