Skip to content

Commit dbef981

Browse files
jehervematticbot
authored andcommitted
Block delimiter: revert changes to parsed attributes, and update documentation (#44365)
* Scanner: revert change from #44158 See Automattic/jetpack#44158 (comment) * Update docs Automattic/jetpack#44158 (comment) * Have the counting example actually count See Automattic/jetpack#44158 (comment) * Add changelog --------- Co-authored-by: Dennis Snell <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/16374699829 Upstream-Ref: Automattic/jetpack@d44540d
1 parent d4a49d2 commit dbef981

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.3.1-alpha] - unreleased
9+
10+
This is an alpha version! The changes listed here are not final.
11+
12+
### Changed
13+
- Scanner: revert changes to returned parsed attributes.
14+
815
## [0.3.0] - 2025-07-14
916
### Added
1017
- Add Block_Scanner class as a replacement to Block_Delimiter. [#44158]
@@ -21,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2128
### Added
2229
- Initial release.
2330

31+
[0.3.1-alpha]: https://github.com/Automattic/block-delimiter/compare/v0.3.0...v0.3.1-alpha
2432
[0.3.0]: https://github.com/Automattic/block-delimiter/compare/v0.2.1...v0.3.0
2533
[0.2.1]: https://github.com/Automattic/block-delimiter/compare/v0.2.0...v0.2.1
2634
[0.2.0]: https://github.com/Automattic/block-delimiter/compare/v0.1.0...v0.2.0

README.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,14 @@ $scanner = Block_Scanner::create( $post_content );
7474

7575
// Find the first image block
7676
while ( $scanner->next_delimiter() ) {
77-
if ( ! $scanner->is_block_type( 'image' ) ) {
77+
if ( ! $scanner->opens_block( 'image' ) ) {
7878
continue;
7979
}
8080

81-
if ( $scanner->opens_block() ) {
82-
$attributes = $scanner->allocate_and_return_parsed_attributes();
83-
if ( isset( $attributes['id'] ) ) {
84-
echo "Found image with ID: " . $attributes['id'];
85-
break;
86-
}
81+
$attributes = $scanner->allocate_and_return_parsed_attributes();
82+
if ( isset( $attributes['id'] ) ) {
83+
echo "Found image with ID: " . $attributes['id'];
84+
break;
8785
}
8886
}
8987
```
@@ -130,7 +128,7 @@ Paragraph with font size: small
130128

131129
### Counting Block Types
132130

133-
Get a summary of all block types in a document:
131+
Get a count of all block types in a document:
134132

135133
```php
136134
use Automattic\Block_Scanner;
@@ -151,33 +149,33 @@ $post_content = '<!-- wp:heading {"level":2} -->
151149
<ul><li>Item 1</li><li>Item 2</li></ul>
152150
<!-- /wp:list -->';
153151

154-
function get_block_types_in( string $html ): array {
155-
$block_types = [];
152+
function count_block_types_in( string $html ): array {
153+
$block_counts = [];
156154
$scanner = Block_Scanner::create( $html );
157155

158156
while ( $scanner->next_delimiter() ) {
159157
if ( $scanner->opens_block() ) {
160-
$block_types[ $scanner->get_block_type() ] = true;
158+
$block_type = $scanner->get_block_type();
159+
$block_counts[ $block_type ] = ( $block_counts[ $block_type ] ?? 0 ) + 1;
161160
}
162161
}
163162

164-
$block_types = array_keys( $block_types );
165-
sort( $block_types );
166-
return $block_types;
163+
ksort( $block_counts );
164+
return $block_counts;
167165
}
168166

169-
$block_types = get_block_types_in( $post_content );
170-
print_r( $block_types );
167+
$block_counts = count_block_types_in( $post_content );
168+
print_r( $block_counts );
171169
```
172170

173171
**Output:**
174172
```
175173
Array
176174
(
177-
[0] => core/heading
178-
[1] => core/image
179-
[2] => core/list
180-
[3] => core/paragraph
175+
[core/heading] => 1
176+
[core/image] => 1
177+
[core/list] => 1
178+
[core/paragraph] => 2
181179
)
182180
```
183181

@@ -343,7 +341,7 @@ Paragraph with font size: small
343341

344342
### Counting Block Types
345343

346-
Get a summary of all block types in a document:
344+
Get a count of all block types in a document:
347345

348346
```php
349347
use Automattic\Block_Delimiter;
@@ -364,32 +362,32 @@ $post_content = '<!-- wp:heading {"level":2} -->
364362
<ul><li>Item 1</li><li>Item 2</li></ul>
365363
<!-- /wp:list -->';
366364

367-
function get_block_types_in( string $html ): array {
368-
$block_types = [];
365+
function count_block_types_in( string $html ): array {
366+
$block_counts = [];
369367

370368
foreach ( Block_Delimiter::scan_delimiters( $html ) as $delimiter ) {
371369
if ( Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
372-
$block_types[ $delimiter->allocate_and_return_block_type() ] = true;
370+
$block_type = $delimiter->allocate_and_return_block_type();
371+
$block_counts[ $block_type ] = ( $block_counts[ $block_type ] ?? 0 ) + 1;
373372
}
374373
}
375374

376-
$block_types = array_keys( $block_types );
377-
sort( $block_types );
378-
return $block_types;
375+
ksort( $block_counts );
376+
return $block_counts;
379377
}
380378

381-
$block_types = get_block_types_in( $post_content );
382-
print_r( $block_types );
379+
$block_counts = count_block_types_in( $post_content );
380+
print_r( $block_counts );
383381
```
384382

385383
**Output:**
386384
```
387385
Array
388386
(
389-
[0] => core/heading
390-
[1] => core/image
391-
[2] => core/list
392-
[3] => core/paragraph
387+
[core/heading] => 1
388+
[core/image] => 1
389+
[core/list] => 1
390+
[core/paragraph] => 2
393391
)
394392
```
395393

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@automattic/block-delimiter",
4-
"version": "0.3.0",
4+
"version": "0.3.1-alpha",
55
"description": "Efficiently work with block structure",
66
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/block-delimiter/#readme",
77
"bugs": {

src/class-block-scanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ public function allocate_and_return_parsed_attributes(): ?array {
808808
}
809809

810810
$json_span = substr( $this->source_text, $this->json_at, $this->json_length );
811-
$parsed = json_decode( $json_span, true, 512, JSON_OBJECT_AS_ARRAY | JSON_INVALID_UTF8_SUBSTITUTE );
811+
$parsed = json_decode( $json_span, null, 512, JSON_OBJECT_AS_ARRAY | JSON_INVALID_UTF8_SUBSTITUTE );
812812

813813
$last_error = json_last_error();
814814
$this->last_json_error = $last_error;

0 commit comments

Comments
 (0)