Skip to content

Commit

Permalink
Merge pull request #215 from Automattic/add/add-language-support-raw-…
Browse files Browse the repository at this point in the history
…transform

Set code block language when pasting into editor
  • Loading branch information
donnapep authored Jun 7, 2021
2 parents cc1b7e7 + a287c5a commit 3b268ab
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/code-block/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export default {
node.children.length === 1 &&
node.firstChild.nodeName === 'CODE'
),
transform( node ) {
const content = node.firstChild.textContent;
const [ startingMatch, language ] = content.match( /^```(\w+)?\n/ ) || [ null, null ];
const attributes = language ? { language } : {};

// Extract content without backticks and (optionally) language.
attributes.content = startingMatch ? content.replace( startingMatch, '' ).replace( /```\s*$/, '' ) : content;

return createBlock( 'syntaxhighlighter/code', attributes );
},
schema: {
pre: {
children: {
Expand Down

0 comments on commit 3b268ab

Please sign in to comment.