Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ function mkaz_prism_theme_css_ver() {
add_filter( 'wp_kses_allowed_html', function( $tags ) {

if ( is_array( $tags['code'] ) ) {
$tags['code']['data-lang'] = array();
$tags['code']['lang'] = array();
} else {
$tags['code'] = array(
'data-lang' => array(),
'lang' => array(),
);
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"scripts": {
"start": "wp-scripts start",
"build": "wp-scripts build"
},
"dependencies": {
"lodash.omit": "^4.5.0"
}
}
24 changes: 10 additions & 14 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( () => {
Expand Down Expand Up @@ -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 } );
Expand All @@ -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 || '',
Expand All @@ -96,7 +92,7 @@ const edit = ( { attributes, className, setAttributes } ) => {
<PanelBody title={ __( 'Settings' ) }>
<SelectControl
label={ __( 'Language' ) }
value={ attributes.language }
value={ attributes.datalang }
options={ [
{
label: __(
Expand All @@ -113,8 +109,8 @@ const edit = ( { attributes, className, setAttributes } ) => {
} )
)
) }
onChange={ ( language ) =>
setAttributes( { language } )
onChange={ ( datalang ) =>
setAttributes( { datalang } )
}
/>
<ToggleControl
Expand Down Expand Up @@ -204,11 +200,11 @@ const edit = ( { attributes, className, setAttributes } ) => {

{ /* Language label, uses wp-block class to keep within editor bounds */ }
<div className="wp-block mkaz-code-syntax-block__lang_label">
{ mkaz_code_syntax_languages[ attributes.language ] }
{ mkaz_code_syntax_languages[ attributes.datalang ] }
</div>
</>
</>
);
};

export default edit;
export default Edit;
54 changes: 47 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ const addSyntaxToCodeBlock = ( settings ) => {
return settings;
}

const newCodeBlockSettings = {
return {
...settings,

attributes: {
...settings.attributes,
language: {
content: {
type: 'string',
source: 'html',
selector: 'code',
},
datalang: {
type: 'string',
selector: 'code',
source: 'attribute',
attribute: 'lang',
attribute: 'data-lang',
},
lineNumbers: {
type: 'boolean',
Expand All @@ -40,11 +43,48 @@ const addSyntaxToCodeBlock = ( settings ) => {
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 (
<pre>
<code
lang={ attributes.language }
className={ cls }
>
{ attributes.content }
</code>
</pre>
);
},
},
],
edit,
save,
};

return newCodeBlockSettings;
};

// Register Filter
Expand Down
6 changes: 3 additions & 3 deletions src/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ const save = ( { attributes } ) => {
<RichText.Content
tagName="code"
value={ attributes.content }
lang={ attributes.language }
data-lang={ attributes.datalang }
className={ cls }
/>
</pre>
Expand All @@ -28,7 +28,7 @@ const save = ( { attributes } ) => {
// Backward compatibility < WP 5.6
return (
<pre className="wp-block-code" title={ attributes.title }>
<code lang={ attributes.language } className={ cls }>
<code data-lang={ attributes.datalang } className={ cls }>
{ attributes.content }
</code>
</pre>
Expand Down