PHP code example of dealnews / datocms-html-to-structured-text

1. Go to this page and download the library: Download dealnews/datocms-html-to-structured-text library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dealnews / datocms-html-to-structured-text example snippets



DealNews\HtmlToStructuredText\Converter;

// Create converter instance
$converter = new Converter();

// Simple HTML
$html = '<h1>DatoCMS</h1><p>The best <strong>headless CMS</strong>.</p>';

$dast = $converter->convert($html);
// Returns:
// [
//     'schema' => 'dast',
//     'document' => [
//         'type' => 'root',
//         'children' => [...]
//     ]
// ]

use DealNews\HtmlToStructuredText\Converter;
use DealNews\HtmlToStructuredText\Options;
use DealNews\HtmlToStructuredText\Handlers;

$converter = new Converter();
$options = new Options();

// Custom h1 handler - adds prefix to all h1 headings
$options->handlers['h1'] = function (
    callable $create_node,
    \DOMNode $node,
    $context
) {
    // Use default handler
    $result = Handlers::heading($create_node, $node, $context);
    
    // Modify result
    if (isset($result['children'][0]['value'])) {
        $result['children'][0]['value'] = '★ ' . $result['children'][0]['value'];
    }
    
    return $result;
};

$html = '<h1>Important</h1>';
$dast = $converter->convert($html, $options);
// H1 will have "★ Important" as text

$options = new Options();

// Convert all <div> tags to <p> tags
$options->preprocess = function (\DOMDocument $doc): void {
    $divs = [];
    foreach ($doc->getElementsByTagName('div') as $div) {
        $divs[] = $div;
    }
    
    foreach ($divs as $div) {
        $p = $doc->createElement('p');
        while ($div->firstChild) {
            $p->appendChild($div->firstChild);
        }
        $div->parentNode->replaceChild($p, $div);
    }
};

$html = '<div>Content</div>';
$dast = $converter->convert($html, $options);
// Div becomes paragraph in DAST

$options = new Options();
$options->allowed_blocks = ['paragraph', 'list']; // Only paragraphs and lists

$html = '<h1>Title</h1><p>Text</p>';
$dast = $converter->convert($html, $options);
// H1 will be converted to paragraph

$options = new Options();
$options->allowed_marks = ['strong']; // Only bold

$html = '<p><strong>Bold</strong> and <em>italic</em></p>';
$dast = $converter->convert($html, $options);
// Only strong mark will be applied, emphasis ignored

$options = new Options();
$options->allowed_heading_levels = [1, 2]; // Only H1 and H2

$html = '<h1>H1</h1><h3>H3</h3>';
$dast = $converter->convert($html, $options);
// H3 will be converted to paragraph

class Options {
    // Whether to preserve newlines in text
    public bool $newlines = false;
    
    // Custom handler overrides
    public array $handlers = [];
    
    // Preprocessing function
    public $preprocess = null;
    
    // Allowed block types
    public array $allowed_blocks = [
        'blockquote', 'code', 'heading', 'link', 'list'
    ];
    
    // Allowed mark types
    public array $allowed_marks = [
        'strong', 'code', 'emphasis', 'underline', 
        'strikethrough', 'highlight'
    ];
    
    // Allowed heading levels (1-6)
    public array $allowed_heading_levels = [1, 2, 3, 4, 5, 6];
}

$html = '<pre><code class="language-javascript">const x = 1;</code></pre>';
$dast = $converter->convert($html);
// Result will have: ['type' => 'code', 'language' => 'javascript', 'code' => 'const x = 1;']

$html = '<a href="https://example.com" target="_blank" rel="noopener">Link</a>';
$dast = $converter->convert($html);
// Result will have meta array: [['id' => 'target', 'value' => '_blank'], ...]

$html = '<span style="font-weight: bold">Bold via style</span>';
$dast = $converter->convert($html);
// Creates span with strong mark

$html = '<base href="https://example.com/"><a href="/page">Link</a>';
$dast = $converter->convert($html);
// Link URL will be resolved to: https://example.com/page

use DealNews\HtmlToStructuredText\ConversionError;

try {
    $dast = $converter->convert($html);
} catch (ConversionError $e) {
    echo "Conversion failed: " . $e->getMessage();
    $node = $e->getNode(); // Get problematic DOM node if available
}

$html = '<ul><li>Item<ul><li>Nested</li></ul></li></ul>';
// Converts correctly to nested list structure

$html = '<a href="#"><span>Inline</span><p>Block</p></a>';
// Properly splits into separate nodes
bash
php examples/basic.php
php examples/custom_handlers.php
php examples/preprocessing.php