From a538fb82cd66121bb7c7639c664ecfe679194ca8 Mon Sep 17 00:00:00 2001 From: Donna Peplinskie Date: Thu, 3 Jun 2021 11:09:10 -0400 Subject: [PATCH 1/2] Set code block language when pasting into editor --- src/code-block/transforms.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/code-block/transforms.js b/src/code-block/transforms.js index fc1a42a..243fdde 100644 --- a/src/code-block/transforms.js +++ b/src/code-block/transforms.js @@ -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( /```$/, '' ) : content; + + return createBlock( 'syntaxhighlighter/code', attributes ); + }, schema: { pre: { children: { From a287c5a38c1d79c7948c1d1af721dfb2d27806c7 Mon Sep 17 00:00:00 2001 From: Donna Peplinskie Date: Mon, 7 Jun 2021 14:40:00 -0400 Subject: [PATCH 2/2] Account for possible whitespace after ending backticks Co-authored-by: Peter Kiss --- src/code-block/transforms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code-block/transforms.js b/src/code-block/transforms.js index 243fdde..8da9c09 100644 --- a/src/code-block/transforms.js +++ b/src/code-block/transforms.js @@ -27,7 +27,7 @@ export default { const attributes = language ? { language } : {}; // Extract content without backticks and (optionally) language. - attributes.content = startingMatch ? content.replace( startingMatch, '' ).replace( /```$/, '' ) : content; + attributes.content = startingMatch ? content.replace( startingMatch, '' ).replace( /```\s*$/, '' ) : content; return createBlock( 'syntaxhighlighter/code', attributes ); },