Skip to content

Commit 3b268ab

Browse files
authored
Merge pull request #215 from Automattic/add/add-language-support-raw-transform
Set code block language when pasting into editor
2 parents cc1b7e7 + a287c5a commit 3b268ab

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/code-block/transforms.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export default {
2121
node.children.length === 1 &&
2222
node.firstChild.nodeName === 'CODE'
2323
),
24+
transform( node ) {
25+
const content = node.firstChild.textContent;
26+
const [ startingMatch, language ] = content.match( /^```(\w+)?\n/ ) || [ null, null ];
27+
const attributes = language ? { language } : {};
28+
29+
// Extract content without backticks and (optionally) language.
30+
attributes.content = startingMatch ? content.replace( startingMatch, '' ).replace( /```\s*$/, '' ) : content;
31+
32+
return createBlock( 'syntaxhighlighter/code', attributes );
33+
},
2434
schema: {
2535
pre: {
2636
children: {

0 commit comments

Comments
 (0)