/**umb_name:H2*/
h2 {
font-size: 24px;
font-weight: bold;
}
allowed for visual styling in the RTE while combining appsettings for additional functionality, such as defining list types. However, this combination no longer works, forcing a choice between stylesheets or appsettings.
You can still use stylesheets to define colors and visual styles while omitting the Umbraco-specific attributes (e.g., /**umb_name:H2*/). This allows the colors to display in the RTE but applies configurations via the appsettings.json file instead. This workaround enables:
Here’s an example configuration for appsettings.json:
"RichTextEditor": {
"CustomConfig": {
"style_formats": "[{\"title\":\"Checked List\", \"selector\": \"ul\", \"classes\": \"list list-check\" },{\"title\":\"Numbered List\", \"selector\": \"ol\", \"classes\": \"list list-decimal\" },{\"title\":\"Heading Link\", \"selector\": \"a\", \"classes\": \"link-heading\" }]"
}
}
By separating stylesheets for colors and defining other attributes in appsettings.json, you can achieve a more flexible and user-friendly RTE setup.
To simplify the configuration, you can store the JSON for RTE styles in a separate file and link it to the appsettings dynamically. Below is a C# extension method to handle this:
using System.Text.Json;
namespace Umbraco.Website.Extensions
{
public static class IConfigurationExtensions
{
/// <summary>
/// Updates Umbraco RTE style formats from a JSON file.
/// </summary>
public static async void UpdateUmbracoRteStyleFormats(
this IConfiguration config,
string pathToUmbracoRtfStyleFormatsJsonFile
)
{
const string UmbracoRteStyleFormatsKey =
"Umbraco:CMS:RichTextEditor:CustomConfig:style_formats";
if (config is null) throw new ArgumentNullException(nameof(config));
if (pathToUmbracoRtfStyleFormatsJsonFile is null) throw new ArgumentNullException(nameof(pathToUmbracoRtfStyleFormatsJsonFile));
using var reader = new StreamReader(pathToUmbracoRtfStyleFormatsJsonFile);
var rteStyleFormatsJson = await JsonDocument.ParseAsync(reader.BaseStream);
var rteStyleFormatsAsString = JsonSerializer.Serialize(rteStyleFormatsJson);
config[UmbracoRteStyleFormatsKey] = rteStyleFormatsAsString;
}
}
}
When defining RTE styles in appsettings.json, you can specify additional parameters to control how they are applied. These parameters provide flexibility in how elements are styled in the RTE.
Parameter | Summary |
---|---|
inline | Tag name of the inline element to use as a wrapper, e.g., "span" to wrap the current selection. |
block | Tag name of the block element to use as a wrapper, e.g., "h1". Replaces existing block elements. |
selector | CSS3 selector pattern to find elements within the selection, e.g., applying classes to odd table rows. |
classes | Space-separated list of classes applied to the selected or newly created inline/block element. |
styles | Key/value object with CSS styles (e.g., color, backgroundColor, textDecoration) to apply. |
attributes | Key/value object with attributes to apply to the selected or newly created inline/block element. |
exact | Ensures the format is not merged with other wrappers having the same format, avoiding conflicts. |
wrapper | Indicates the format is a container for block elements, e.g., a div wrapper or blockquote. |
These parameters allow for precise control over how RTE styles are applied, making it possible to create highly customized and user-friendly editing experiences. By leveraging these settings, you can streamline your RTE setup while maintaining flexibility for various use cases.
This solution provides:
By implementing this approach, you can overcome the limitations introduced in Umbraco 12 and above, offering a cleaner and more flexible way to manage RTE configurations.
Note
For Umbraco 15+ the TinyMCE editor is replaced by TipTap. The Tiptap UI currently does not support using custom styles for your rich text.
See TipTap Editor