Download the PHP package dealnews/datocms-structured-text-to-html-string without Composer
On this page you can find all versions of the php package dealnews/datocms-structured-text-to-html-string. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dealnews/datocms-structured-text-to-html-string
More information about dealnews/datocms-structured-text-to-html-string
Files in dealnews/datocms-structured-text-to-html-string
Package datocms-structured-text-to-html-string
Short Description Convert DatoCMS Structured Text field to HTML string
License BSD-3-Clause
Informations about the package datocms-structured-text-to-html-string
DatoCMS Structured Text to HTML String (PHP)
Convert DatoCMS Structured Text (DAST format) to HTML strings. PHP port of the official JavaScript library.
Requirements
- PHP 8.2+
- Composer
Installation
Basic Usage
Features
- ✅ Converts DAST documents to HTML strings
- ✅ Supports custom node and mark rendering rules
- ✅ Handles blocks, inline blocks, inline items, and item links
- ✅ Text transformation support
- ✅ Newline handling (converts to
<br>tags) - ✅ Type-safe with full PHPDoc coverage
Advanced Usage
Custom Text Transformation
Custom Node Rules
Change how specific node types are rendered:
Custom Mark Rules
Change how text marks (bold, italic, etc.) are rendered:
Rendering Blocks and Links
API Reference
Renderer::render($structured_text_or_node, ?RenderSettings $settings = null): ?string
Main rendering function. Converts structured text to HTML.
Parameters:
$structured_text_or_node- Structured text from DatoCMS or a document node$settings- Optional rendering configuration
Returns: HTML string or null if input is null
Throws: RenderError if rendering fails
RenderSettings
Configuration object for customizing rendering behavior.
Properties:
custom_node_rules- Array of custom node rendering rulescustom_mark_rules- Array of custom mark rendering rulesrender_inline_record- Callback for rendering inline itemsrender_link_to_record- Callback for rendering item linksrender_block- Callback for rendering blocksrender_inline_block- Callback for rendering inline blocksrender_text- Callback for transforming textrender_node- Custom node renderer (advanced)render_fragment- Custom fragment renderer (advanced)meta_transformer- Function to transform link meta into attributes
Utility Functions
Rule Builders
Error Handling
The library throws RenderError exceptions when:
- Required render callbacks are missing (e.g.,
render_blockwhen blocks are present) - Referenced records cannot be found in the links/blocks arrays
- Document structure is invalid
Default Mark Rendering
| Mark | HTML Tag |
|---|---|
strong |
<strong> |
code |
<code> |
emphasis |
<em> |
underline |
<u> |
strikethrough |
<s> |
highlight |
<mark> |
Edge Cases
- Newlines in text: Converted to
<br />tags automatically - Empty nodes: Rendered as self-closing tags (e.g.,
<hr />) - Null renderers: Returning
nullfrom custom renderers skips that node - Missing records: Throws
RenderErrorto prevent silent failures
Development
Requirements
- PHP 8.2+
- Composer
- PHPUnit 11.5+
Running Tests
Running Examples
Testing
The library includes mocks-friendly dependency injection:
License
BSD 3-Clause License - see LICENSE file for details
Credits
This is a PHP port of the official DatoCMS Structured Text to HTML String JavaScript library.
Ported and maintained by DealNews.