Skip to content

Commit a593333

Browse files
committed
Have the counting example actually count
See #44158 (comment)
1 parent 600db1c commit a593333

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

projects/packages/block-delimiter/README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Paragraph with font size: small
128128

129129
### Counting Block Types
130130

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

133133
```php
134134
use Automattic\Block_Scanner;
@@ -149,33 +149,33 @@ $post_content = '<!-- wp:heading {"level":2} -->
149149
<ul><li>Item 1</li><li>Item 2</li></ul>
150150
<!-- /wp:list -->';
151151

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

156156
while ( $scanner->next_delimiter() ) {
157157
if ( $scanner->opens_block() ) {
158-
$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;
159160
}
160161
}
161162

162-
$block_types = array_keys( $block_types );
163-
sort( $block_types );
164-
return $block_types;
163+
ksort( $block_counts );
164+
return $block_counts;
165165
}
166166

167-
$block_types = get_block_types_in( $post_content );
168-
print_r( $block_types );
167+
$block_counts = count_block_types_in( $post_content );
168+
print_r( $block_counts );
169169
```
170170

171171
**Output:**
172172
```
173173
Array
174174
(
175-
[0] => core/heading
176-
[1] => core/image
177-
[2] => core/list
178-
[3] => core/paragraph
175+
[core/heading] => 1
176+
[core/image] => 1
177+
[core/list] => 1
178+
[core/paragraph] => 2
179179
)
180180
```
181181

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

342342
### Counting Block Types
343343

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

346346
```php
347347
use Automattic\Block_Delimiter;
@@ -362,32 +362,32 @@ $post_content = '<!-- wp:heading {"level":2} -->
362362
<ul><li>Item 1</li><li>Item 2</li></ul>
363363
<!-- /wp:list -->';
364364

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

368368
foreach ( Block_Delimiter::scan_delimiters( $html ) as $delimiter ) {
369369
if ( Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
370-
$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;
371372
}
372373
}
373374

374-
$block_types = array_keys( $block_types );
375-
sort( $block_types );
376-
return $block_types;
375+
ksort( $block_counts );
376+
return $block_counts;
377377
}
378378

379-
$block_types = get_block_types_in( $post_content );
380-
print_r( $block_types );
379+
$block_counts = count_block_types_in( $post_content );
380+
print_r( $block_counts );
381381
```
382382

383383
**Output:**
384384
```
385385
Array
386386
(
387-
[0] => core/heading
388-
[1] => core/image
389-
[2] => core/list
390-
[3] => core/paragraph
387+
[core/heading] => 1
388+
[core/image] => 1
389+
[core/list] => 1
390+
[core/paragraph] => 2
391391
)
392392
```
393393

0 commit comments

Comments
 (0)