From b8fdc293ef84e8d9e2c47ae0c6af9dc616200a46 Mon Sep 17 00:00:00 2001 From: Carlos Longarela Date: Wed, 19 Aug 2020 22:33:47 +0200 Subject: [PATCH 1/6] wp_kses_allowed_html allow data-lang attribute --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 3570bfb..52a6825 100644 --- a/index.php +++ b/index.php @@ -215,10 +215,10 @@ function mkaz_prism_theme_css_ver() { add_filter( 'wp_kses_allowed_html', function( $tags ) { if ( is_array( $tags['code'] ) ) { - $tags['code']['lang'] = array(); + $tags['code']['data-lang'] = array(); } else { $tags['code'] = array( - 'lang' => array(), + 'data-lang' => array(), ); } return $tags; From 8f843814589f470b7a534dec34b3293f99f63c3e Mon Sep 17 00:00:00 2001 From: Carlos Longarela Date: Wed, 19 Aug 2020 22:39:11 +0200 Subject: [PATCH 2/6] code attribute changed from lang to data-lang, accessibility --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b07e4e7..9c39c9e 100644 --- a/src/index.js +++ b/src/index.js @@ -28,7 +28,7 @@ const addSyntaxToCodeBlock = ( settings ) => { type: 'string', selector: 'code', source: 'attribute', - attribute: 'lang', + attribute: 'data-lang', }, lineNumbers: { type: 'boolean', From 83deb58d441a1b17aa7806893801d42eac6fccc5 Mon Sep 17 00:00:00 2001 From: Carlos Longarela Date: Wed, 19 Aug 2020 22:43:24 +0200 Subject: [PATCH 3/6] Changed lang attribute to data-lang, screen readers --- src/save.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/save.js b/src/save.js index fab95e3..5f4b6c6 100644 --- a/src/save.js +++ b/src/save.js @@ -28,7 +28,7 @@ const save = ( { attributes } ) => { // Backward compatibility < WP 5.6 return (
-			
+			
 				{ attributes.content }
 			
 		
From 08cf9bf78e63a2cc3eb3d8778d287945e6bfca07 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Mon, 7 Sep 2020 08:37:00 -0700 Subject: [PATCH 4/6] Add deprecated property - Adds the old attribute to deprecation for lang="javascript" this is supposed to migrate to data-lang="javascript" - Add old save function NOTE: This does not work, an old block still shows as invalid. --- index.php | 2 ++ src/index.js | 65 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/index.php b/index.php index 52a6825..69711b2 100644 --- a/index.php +++ b/index.php @@ -216,9 +216,11 @@ function mkaz_prism_theme_css_ver() { if ( is_array( $tags['code'] ) ) { $tags['code']['data-lang'] = array(); + $tags['code']['lang'] = array(); } else { $tags['code'] = array( 'data-lang' => array(), + 'lang' => array(), ); } return $tags; diff --git a/src/index.js b/src/index.js index 9c39c9e..4f28690 100644 --- a/src/index.js +++ b/src/index.js @@ -19,32 +19,61 @@ const addSyntaxToCodeBlock = ( settings ) => { return settings; } - const newCodeBlockSettings = { + const blockAttributes = { + ...settings.attributes, + language: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'data-lang', + }, + lineNumbers: { + type: 'boolean', + }, + title: { + type: 'string', + source: 'attribute', + selector: 'pre', + attribute: 'title', + }, + }; + + return { ...settings, - attributes: { - ...settings.attributes, - language: { - type: 'string', - selector: 'code', - source: 'attribute', - attribute: 'data-lang', - }, - lineNumbers: { - type: 'boolean', + attributes: blockAttributes, + + deprecated: [{ + // old attributes + attributes: { + ...blockAttributes, + language: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'lang', + }, }, - title: { - type: 'string', - source: 'attribute', - selector: 'pre', - attribute: 'title', + migrate: ( attributes ) => { return attributes; }, + // old save function + save: ( { attributes } ) => { + let cls = ''; + cls = ( attributes.language ) ? 'language-' + attributes.language : ''; + cls = ( attributes.lineNumbers ) ? cls + ' line-numbers' : cls; + return ( +
+						
+							{ attributes.content }
+						
+					
+ ); }, - }, + }], + edit, save, }; - return newCodeBlockSettings; }; // Register Filter From 62807ad5d03739e5366f0491d13af1f3a5a17547 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Tue, 20 Jul 2021 12:18:45 -0700 Subject: [PATCH 5/6] Another attempt at deprecation... --- package.json | 3 +++ src/block-attributes.js | 24 ++++++++++++++++++ src/deprecated.js | 40 ++++++++++++++++++++++++++++++ src/edit.js | 24 ++++++++---------- src/index.js | 54 +++-------------------------------------- src/save.js | 6 ++--- 6 files changed, 84 insertions(+), 67 deletions(-) create mode 100644 src/block-attributes.js create mode 100644 src/deprecated.js diff --git a/package.json b/package.json index bff75b9..4eac418 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "scripts": { "start": "wp-scripts start", "build": "wp-scripts build" + }, + "dependencies": { + "lodash.omit": "^4.5.0" } } diff --git a/src/block-attributes.js b/src/block-attributes.js new file mode 100644 index 0000000..77e575e --- /dev/null +++ b/src/block-attributes.js @@ -0,0 +1,24 @@ +const blockAttributes = { + content: { + type: 'string', + source: 'html', + selector: 'code', + }, + datalang: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'data-lang', + }, + lineNumbers: { + type: 'boolean', + }, + title: { + type: 'string', + source: 'attribute', + selector: 'pre', + attribute: 'title', + }, +}; + +export default blockAttributes; diff --git a/src/deprecated.js b/src/deprecated.js new file mode 100644 index 0000000..7b29924 --- /dev/null +++ b/src/deprecated.js @@ -0,0 +1,40 @@ +import omit from 'lodash.omit'; + +const deprecated = { + // old attributes + attributes: { + content: { + type: 'string', + source: 'html', + selector: 'code', + }, + language: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'lang', + }, + }, + migrate: ( attributes ) => { + return { + datalang: attributes.language, + content: attributes.content, + }; + }, + // old save function + save: ( { attributes } ) => { + let cls = ''; + cls = attributes.language ? 'language-' + attributes.language : ''; + cls = attributes.lineNumbers ? cls + ' line-numbers' : cls; + + return ( +
+				
+					{ attributes.content }
+				
+			
+ ); + }, +}; + +export default deprecated; diff --git a/src/edit.js b/src/edit.js index 859a500..f9df669 100644 --- a/src/edit.js +++ b/src/edit.js @@ -21,12 +21,12 @@ import { __ } from '@wordpress/i18n'; /* global mkaz_code_syntax_languages, mkaz_code_syntax_default_lang, Prism */ -const edit = ( { attributes, className, setAttributes } ) => { +const Edit = ( { attributes, className, setAttributes } ) => { useEffect( () => { - if ( ! attributes.language && mkaz_code_syntax_default_lang ) { - setAttributes( { language: mkaz_code_syntax_default_lang } ); + if ( ! attributes.datalang && mkaz_code_syntax_default_lang ) { + setAttributes( { datalang: mkaz_code_syntax_default_lang } ); } - }, [ attributes.language ] ); + }, [ attributes.datalang ] ); // Onload fetch color scheme option useEffect( () => { @@ -61,7 +61,7 @@ const edit = ( { attributes, className, setAttributes } ) => { ]; const updateColorScheme = ( colorScheme ) => { - let path = '/mkaz/code-syntax/v1/set/color-scheme/' + colorScheme; + const path = '/mkaz/code-syntax/v1/set/color-scheme/' + colorScheme; apiFetch( { path } ) .then( () => { setAttributes( { colorScheme } ); @@ -71,10 +71,6 @@ const edit = ( { attributes, className, setAttributes } ) => { } ); }; - let cls = ''; - cls = attributes.language ? 'language-' + attributes.language : ''; - cls = attributes.lineNumbers ? cls + ' line-numbers' : cls; - // shared props for text areas const textAreaProps = { value: attributes.content || '', @@ -96,7 +92,7 @@ const edit = ( { attributes, className, setAttributes } ) => { { } ) ) ) } - onChange={ ( language ) => - setAttributes( { language } ) + onChange={ ( datalang ) => + setAttributes( { datalang } ) } /> { { /* Language label, uses wp-block class to keep within editor bounds */ }
- { mkaz_code_syntax_languages[ attributes.language ] } + { mkaz_code_syntax_languages[ attributes.datalang ] }
); }; -export default edit; +export default Edit; diff --git a/src/index.js b/src/index.js index 4f28690..bdc7c6d 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,8 @@ import { addFilter } from '@wordpress/hooks'; /** * Internal dependencies */ +import blockAttributes from './block-attributes'; +import deprecated from './deprecated'; import edit from './edit'; import save from './save'; @@ -19,61 +21,13 @@ const addSyntaxToCodeBlock = ( settings ) => { return settings; } - const blockAttributes = { - ...settings.attributes, - language: { - type: 'string', - selector: 'code', - source: 'attribute', - attribute: 'data-lang', - }, - lineNumbers: { - type: 'boolean', - }, - title: { - type: 'string', - source: 'attribute', - selector: 'pre', - attribute: 'title', - }, - }; - return { ...settings, - - attributes: blockAttributes, - - deprecated: [{ - // old attributes - attributes: { - ...blockAttributes, - language: { - type: 'string', - selector: 'code', - source: 'attribute', - attribute: 'lang', - }, - }, - migrate: ( attributes ) => { return attributes; }, - // old save function - save: ( { attributes } ) => { - let cls = ''; - cls = ( attributes.language ) ? 'language-' + attributes.language : ''; - cls = ( attributes.lineNumbers ) ? cls + ' line-numbers' : cls; - return ( -
-						
-							{ attributes.content }
-						
-					
- ); - }, - }], - + attributes: { ...blockAttributes }, + deprecated: [ deprecated ], edit, save, }; - }; // Register Filter diff --git a/src/save.js b/src/save.js index 5f4b6c6..88391c4 100644 --- a/src/save.js +++ b/src/save.js @@ -5,7 +5,7 @@ import { RichText, useBlockProps } from '@wordpress/block-editor'; const save = ( { attributes } ) => { let cls = ''; - cls = attributes.language ? 'language-' + attributes.language : ''; + cls = attributes.datalang ? 'language-' + attributes.datalang : ''; cls = attributes.lineNumbers ? cls + ' line-numbers' : cls; // WP 5.6 / GB 9.2 @@ -17,7 +17,7 @@ const save = ( { attributes } ) => { @@ -28,7 +28,7 @@ const save = ( { attributes } ) => { // Backward compatibility < WP 5.6 return (
-			
+			
 				{ attributes.content }
 			
 		
From 96f0c42e9ae26841a879e3b47e1c2940b29bf73b Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Fri, 23 Jul 2021 09:09:23 -0700 Subject: [PATCH 6/6] Dont use extra files --- src/block-attributes.js | 24 --------------- src/deprecated.js | 40 ------------------------- src/index.js | 65 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 68 deletions(-) delete mode 100644 src/block-attributes.js delete mode 100644 src/deprecated.js diff --git a/src/block-attributes.js b/src/block-attributes.js deleted file mode 100644 index 77e575e..0000000 --- a/src/block-attributes.js +++ /dev/null @@ -1,24 +0,0 @@ -const blockAttributes = { - content: { - type: 'string', - source: 'html', - selector: 'code', - }, - datalang: { - type: 'string', - selector: 'code', - source: 'attribute', - attribute: 'data-lang', - }, - lineNumbers: { - type: 'boolean', - }, - title: { - type: 'string', - source: 'attribute', - selector: 'pre', - attribute: 'title', - }, -}; - -export default blockAttributes; diff --git a/src/deprecated.js b/src/deprecated.js deleted file mode 100644 index 7b29924..0000000 --- a/src/deprecated.js +++ /dev/null @@ -1,40 +0,0 @@ -import omit from 'lodash.omit'; - -const deprecated = { - // old attributes - attributes: { - content: { - type: 'string', - source: 'html', - selector: 'code', - }, - language: { - type: 'string', - selector: 'code', - source: 'attribute', - attribute: 'lang', - }, - }, - migrate: ( attributes ) => { - return { - datalang: attributes.language, - content: attributes.content, - }; - }, - // old save function - save: ( { attributes } ) => { - let cls = ''; - cls = attributes.language ? 'language-' + attributes.language : ''; - cls = attributes.lineNumbers ? cls + ' line-numbers' : cls; - - return ( -
-				
-					{ attributes.content }
-				
-			
- ); - }, -}; - -export default deprecated; diff --git a/src/index.js b/src/index.js index bdc7c6d..d2d1199 100644 --- a/src/index.js +++ b/src/index.js @@ -11,8 +11,6 @@ import { addFilter } from '@wordpress/hooks'; /** * Internal dependencies */ -import blockAttributes from './block-attributes'; -import deprecated from './deprecated'; import edit from './edit'; import save from './save'; @@ -23,8 +21,67 @@ const addSyntaxToCodeBlock = ( settings ) => { return { ...settings, - attributes: { ...blockAttributes }, - deprecated: [ deprecated ], + attributes: { + content: { + type: 'string', + source: 'html', + selector: 'code', + }, + datalang: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'data-lang', + }, + lineNumbers: { + type: 'boolean', + }, + title: { + type: 'string', + source: 'attribute', + selector: 'pre', + attribute: 'title', + }, + }, + deprecated: [ + { + // old attributes + attributes: { + ...settings.attributes, + language: { + type: 'string', + selector: 'code', + source: 'attribute', + attribute: 'lang', + }, + }, + migrate: ( attributes ) => { + return { + ...attributes, + datalang: attributes.language, + }; + }, + // old save function + save: ( { attributes } ) => { + let cls = ''; + cls = attributes.language + ? 'language-' + attributes.language + : ''; + cls = attributes.lineNumbers ? cls + ' line-numbers' : cls; + + return ( +
+							
+								{ attributes.content }
+							
+						
+ ); + }, + }, + ], edit, save, };