Skip to content

Commit

Permalink
Use metadata util in code block to keep id and name
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Feb 20, 2024
1 parent bb3b325 commit 7eb0fdb
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions packages/block-library/src/code/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
*/
import { createBlock } from '@wordpress/blocks';
import { create, toHTMLString } from '@wordpress/rich-text';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const { getTransformedMetadata } = unlock( blockEditorPrivateApis );

const transforms = {
from: [
Expand All @@ -14,17 +22,27 @@ const transforms = {
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) =>
createBlock( 'core/code', { content } ),
transform: ( { content, metadata } ) =>
createBlock( 'core/code', {
content,
metadata: getTransformedMetadata( metadata, [
'id',
'name',
] ),
} ),
},
{
type: 'block',
blocks: [ 'core/html' ],
transform: ( { content: text } ) => {
transform: ( { content: text, metadata } ) => {
return createBlock( 'core/code', {
// The HTML is plain text (with plain line breaks), so
// convert it to rich text.
content: toHTMLString( { value: create( { text } ) } ),
metadata: getTransformedMetadata( metadata, [
'id',
'name',
] ),
} );
},
},
Expand All @@ -51,8 +69,14 @@ const transforms = {
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) =>
createBlock( 'core/paragraph', { content } ),
transform: ( { content, metadata } ) =>
createBlock( 'core/paragraph', {
content,
metadata: getTransformedMetadata( metadata, [
'id',
'name',
] ),
} ),
},
],
};
Expand Down

0 comments on commit 7eb0fdb

Please sign in to comment.