3.0 KiB
3.0 KiB
Markdown HTML Widget & Property Type
This document outlines the implementation of the markdown property type for widgets in the Polymech Pictures system. This feature allows widget developers to define properties that accept Markdown input, which is then rendered as HTML in the canvas and exported as HTML for emails.
1. Configuration Schema
To use the Markdown property type, update your widget's library.json definition. Use "type": "markdown" in the configSchema.
{
"name": "Text",
"template": "./text.html",
"configSchema": {
"content": {
"type": "markdown",
"label": "Content",
"default": "# Hello World\nThis is **markdown**.",
"description": "The main text content."
}
}
}
2. Widget Properties UI
The WidgetPropertiesForm component has been updated to handle the markdown type:
- Input: Renders a
Textareafor quick edits. - Fullscreen Editor: Includes a "Fullscreen" button that opens a modal with a rich Markdown editor (
MarkdownEditorEx). - Functionality:
- Supports splitting view (Editor/Preview).
- Integrates with
ImagePickerDialogfor inserting images.
3. Rendering Pipeline
The rendering of Markdown content occurs in two places:
A. Canvas Rendering (HtmlWidget.tsx)
- Dependency: Uses
markedlibrary for parsing. - Logic:
- The
HtmlWidgetchecks the widget definition (looked up viawidgetDefId) for any properties defined astype: "markdown". - If found, it asynchronously parses the markdown string into HTML using
marked.parse(). - The resulting HTML is injected into the template.
- The
- Inline Editing: Unlike standard text properties, Markdown properties are excluded from the inline
contenteditablewrappers. This prevents users from accidentally breaking the HTML structure by editing raw HTML output directly on the canvas.
B. Email Export (emailExporter.ts)
- Logic:
- During the export process (
generateEmailHtml), the system iterates through widget properties. - It checks the
configSchemaformarkdowntypes. - It converts the Markdown content to HTML before performing variable substitution in the template.
- This ensures that the final email HTML contains properly formatted tags (e.g.,
<h1>,<ul>,<strong>) instead of raw markdown characters.
- During the export process (
4. Technical Implementation Details
- Dependencies: Added
markedtopackage.json. - Layout Context:
LayoutContainerpasseswidgetDefIdto widget components to ensure correct schema lookup in the registry. - Registry: The
WidgetRegistryis the source of truth for property types, enabling the renderer to know which props to compile.
5. Usage Notes
- Preview Mode: The behavior in the canvas now matches the "Preview" mode more closely for markdown fields.
- Sanitization: Standard
markedparsing is used. Ensure trusted input if extending to user-generated content in the future.