HTML juice plugin for Plate that inlines CSS properties into style attributes
npx @tessl/cli install tessl/npm-udecode--plate-juice@49.0.0Plate Juice Plugin provides HTML processing functionality for the Plate rich text editor framework. It integrates the juice library to automatically inline CSS properties into HTML style attributes during HTML parsing, making it ideal for contexts that don't support external stylesheets like email templates or embedded content.
npm install @udecode/plate-juiceimport { JuicePlugin } from "@udecode/plate-juice";For CommonJS:
const { JuicePlugin } = require("@udecode/plate-juice");import { createPlateEditor } from "@udecode/plate";
import { JuicePlugin } from "@udecode/plate-juice";
// Create editor with juice plugin
const editor = createPlateEditor({
plugins: [
// ... other plugins
JuicePlugin,
],
});
// The plugin automatically processes HTML content during parsing
// CSS in <style> tags gets inlined into style attributesThe Plate Juice Plugin integrates with Plate's plugin architecture through these key components:
createSlatePlugin to create a standard Plate pluginjuice library to convert CSS rules to inline stylesMain plugin that provides CSS inlining functionality for HTML content in Plate editors.
/**
* Plate plugin that inlines CSS properties into HTML style attributes
* Uses the juice library to process HTML content during parsing
* Created via createSlatePlugin with configuration for HTML parsing integration
*/
export const JuicePlugin: PlatePlugin<'juice'>;The plugin configuration includes:
interface JuicePluginConfig {
/** Plugin identifier key */
key: 'juice';
/** Only active in edit mode, not render mode */
editOnly: true;
/** Plugin injection configuration */
inject: {
plugins: {
/** HTML plugin integration */
[KEYS.html]: {
parser: {
/** HTML transformation function */
transformData: ({ data }: { data: string }) => string;
};
};
};
};
}The plugin processes HTML content through the following steps:
<!-- markers that appear after <style> tags using the regex pattern /<style>\s*<!--/g (workaround for juice library limitation)juice() function to convert CSS rules in <style> tags to inline style attributesInput Example:
<style>
.highlight { background-color: yellow; }
</style>
<p class="highlight">This text will be highlighted</p>Output Example:
<p class="highlight" style="background-color: yellow;">This text will be highlighted</p>The plugin integrates with these Plate framework components:
/** Plate plugin creation utility from @udecode/plate */
function createSlatePlugin<K extends string>(
config: PlatePluginConfig & { key: K }
): PlatePlugin<K>;
/** Plate plugin keys constants from @udecode/plate */
const KEYS: PlateKeys;
/** CSS inlining library from juice package */
function juice(html: string): string;import { createPlateEditor } from "@udecode/plate";
import { HtmlPlugin } from "@udecode/plate-html";
import { JuicePlugin } from "@udecode/plate-juice";
const editor = createPlateEditor({
plugins: [
// HTML plugin must be included for juice plugin to work
HtmlPlugin,
JuicePlugin,
// ... other plugins
],
});The plugin handles common CSS processing issues:
<!-- patterns that interfere with CSS parsingjuice library's error handling for malformed CSS/** Main plugin export type */
type JuicePlugin = PlatePlugin<'juice'>;
/** HTML transformation function signature */
type TransformDataFunction = ({ data }: { data: string }) => string;
/** Plugin configuration structure */
interface PlatePluginConfig {
key: string;
editOnly?: boolean;
inject?: {
plugins?: Record<string, any>;
};
}
/** Plate plugin base type */
interface PlatePlugin<K extends string = string> {
key: K;
editOnly?: boolean;
inject?: {
plugins?: Record<string, any>;
};
}
/** Plate KEYS constants type */
interface PlateKeys {
juice: 'juice';
html: 'html';
[key: string]: string | string[];
}