damienharper/adf-tools
PHP tools for Atlassian Document Format (ADF): build documents programmatically, parse ADF JSON, and export content. Includes schema-aligned nodes and helpers to work with Jira/Confluence-compatible ADF structures.
Installation Add the package via Composer:
composer require damienharper/adf-tools
No additional configuration is required—it’s a lightweight, dependency-free library.
Basic Usage
Import the ADF class and parse an Atlassian Document Format (ADF) string:
use DamienHarper\ADFTools\ADF;
$adfString = '{"blocks":[{"type":"paragraph","text":"Hello, ADF!"}]}';
$adf = ADF::parse($adfString);
// Convert back to JSON
echo $adf->toJson();
First Use Case: Converting ADF to HTML Render ADF to HTML for display in a Laravel Blade view:
$html = $adf->toHtml();
return view('editor', ['content' => $html]);
Parsing and Serializing
$adf = ADF::parse(file_get_contents('confluence-export.adf'));
$json = $adf->toJson();
Modifying ADF Content
$adf->addBlock(['type' => 'heading', 'text' => 'New Section']);
$adf->removeBlock(0); // Remove first block
$adf->getBlock(0)->setText('Updated text');
Integration with Laravel
$post->adf_content = $adf->toJson();
$post->save();
$adf = ADF::parse($post->adf_content);
Rich Text Editing
Custom Block Handling Extend the library for unsupported block types:
ADF::extendBlockType('custom', function ($block) {
return '<div class="custom-block">' . $block['text'] . '</div>';
});
Batch Processing Process multiple ADF strings in a loop:
foreach ($confluenceExports as $export) {
$adf = ADF::parse($export);
// Process or store each ADF
}
Validation Validate ADF structure before parsing:
if (ADF::isValid($adfString)) {
$adf = ADF::parse($adfString);
}
Malformed ADF JSON
ADF::isValid() or wrap parsing in a try-catch:
try {
$adf = ADF::parse($input);
} catch (\Exception $e) {
Log::error("Invalid ADF: " . $e->getMessage());
}
Unsupported Block Types
ADF::extendBlockType() or manually handle unsupported blocks in toHtml().HTML Injection Risks
toHtml() may expose XSS.e() or a whitelist:
{!! e($adf->toHtml()) !!}
Performance with Large ADF
ADF::parseFragment() for partial updates.Inspect ADF Structure
Use toArray() to debug block properties:
dd($adf->toArray());
Log Parsing Errors Enable debug mode for detailed exceptions:
ADF::setDebugMode(true);
Compare ADF Versions Check for breaking changes between Confluence/ADF versions in the ADF spec.
Custom Renderers
Override toHtml() for bespoke output:
class CustomADF extends ADF {
public function toHtml() {
// Custom logic
return parent::toHtml();
}
}
Plugin System Use events (if supported) or hooks to intercept block processing:
ADF::on('block.render', function ($block, $html) {
// Modify $html before output
});
Testing Mock ADF objects for unit tests:
$mockAdf = Mockery::mock(ADF::class);
$mockAdf->shouldReceive('toHtml')->andReturn('<p>Test</p>');
How can I help you explore Laravel packages today?