# block-delimiter
**Repository Path**: mirrors_Automattic/block-delimiter
## Basic Information
- **Project Name**: block-delimiter
- **Description**: [READ ONLY] Efficiently work with block structure This repository is a mirror, for issue tracking and development head to: https://github.com/automattic/jetpack
- **Primary Language**: Unknown
- **License**: GPL-2.0
- **Default Branch**: trunk
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-05-29
- **Last Updated**: 2026-11-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Block Delimiter
Efficiently work with block structure
## How to install block-delimiter
To use this package in your WordPress plugin, you can require both this package and the [Jetpack Autoloader](https://packagist.org/packages/automattic/jetpack-autoloader) in your project's `composer.json` file.
## Overview
The Block Delimiter package provides an efficient, streaming parser for working with WordPress block structure without the memory overhead of `parse_blocks()`. It's designed for scenarios where you need to inspect, find, or modify specific blocks without parsing the entire block tree.
The package includes two classes:
- **`Block_Scanner`** (recommended): A high-performance, mutable scanner that provides the best CPU performance while maintaining near-zero memory overhead.
- **`Block_Delimiter`** (legacy): A generator-based approach that returns new instances for each delimiter, suitable for existing code that relies on the `scan_delimiters()` interface.
## Block_Scanner (Recommended)
The `Block_Scanner` class is modeled after the WordPress HTML API and provides the best performance characteristics. It mutates itself during scanning and requires a new instance for each document scan.
### Basic Block Scanning
Find and iterate through all block delimiters in a document:
```php
use Automattic\Block_Scanner;
$post_content = '
Hello world!
';
$scanner = Block_Scanner::create( $post_content );
while ( $scanner->next_delimiter() ) {
echo "Found block: " . $scanner->get_block_type() . "\n";
echo "Type: " . $scanner->get_delimiter_type() . "\n";
}
```
**Output:**
```
Found block: core/paragraph
Type: opener
Found block: core/paragraph
Type: closer
Found block: core/image
Type: opener
Found block: core/image
Type: closer
```
### Finding Specific Block Types
Efficiently find blocks of a specific type without parsing everything:
```php
use Automattic\Block_Scanner;
$post_content = '
Welcome to my blog!
...
';
$scanner = Block_Scanner::create( $post_content );
// Find the first image block
while ( $scanner->next_delimiter() ) {
if ( ! $scanner->opens_block( 'image' ) ) {
continue;
}
$attributes = $scanner->allocate_and_return_parsed_attributes();
if ( isset( $attributes['id'] ) ) {
echo "Found image with ID: " . $attributes['id'];
break;
}
}
```
**Output:**
```
Found image with ID: 456
```
### Extracting Block Attributes
Parse JSON attributes only when needed:
```php
use Automattic\Block_Scanner;
$post_content = '
This paragraph has a large font size.
This paragraph has no custom font size.
This paragraph has a small font size and primary color.
';
$scanner = Block_Scanner::create( $post_content );
while ( $scanner->next_delimiter() ) {
if ( $scanner->opens_block( 'paragraph' ) ) {
$attributes = $scanner->allocate_and_return_parsed_attributes();
if ( isset( $attributes['fontSize'] ) ) {
echo "Paragraph with font size: " . $attributes['fontSize'] . "\n";
}
}
}
```
**Output:**
```
Paragraph with font size: large
Paragraph with font size: small
```
### Counting Block Types
Get a count of all block types in a document:
```php
use Automattic\Block_Scanner;
$post_content = '
';
foreach ( Block_Delimiter::scan_delimiters( $post_content ) as $where => $delimiter ) {
// $where is an array: [ byte_offset, byte_length ]
// $delimiter is a Block_Delimiter instance
echo "Found block: " . $delimiter->allocate_and_return_block_type() . "\n";
echo "Type: " . $delimiter->get_delimiter_type() . "\n";
}
```
**Output:**
```
Found block: core/paragraph
Type: opener
Found block: core/paragraph
Type: closer
Found block: core/image
Type: opener
Found block: core/image
Type: closer
```
### Finding Specific Block Types
Efficiently find blocks of a specific type without parsing everything:
```php
use Automattic\Block_Delimiter;
$post_content = '
Welcome to my blog!
...
';
// Find the first image block
foreach ( Block_Delimiter::scan_delimiters( $post_content ) as $delimiter ) {
if ( ! $delimiter->is_block_type( 'image' ) ) {
continue;
}
if ( Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
$attributes = $delimiter->allocate_and_return_parsed_attributes();
if ( isset( $attributes['id'] ) ) {
echo "Found image with ID: " . $attributes['id'];
break;
}
}
}
```
**Output:**
```
Found image with ID: 456
```
### Extracting Block Attributes
Parse JSON attributes only when needed:
```php
use Automattic\Block_Delimiter;
$post_content = '
This paragraph has a large font size.
This paragraph has no custom font size.
This paragraph has a small font size and primary color.
';
foreach ( Block_Delimiter::scan_delimiters( $post_content ) as $delimiter ) {
if ( $delimiter->is_block_type( 'core/paragraph' ) &&
Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
$attributes = $delimiter->allocate_and_return_parsed_attributes();
if ( isset( $attributes['fontSize'] ) ) {
echo "Paragraph with font size: " . $attributes['fontSize'] . "\n";
}
}
}
```
**Output:**
```
Paragraph with font size: large
Paragraph with font size: small
```
### Counting Block Types
Get a count of all block types in a document:
```php
use Automattic\Block_Delimiter;
$post_content = '