Code Syntax Block

外掛說明

Code Syntax Block extends the WordPress Block Editor by adding syntax highlighting support to the core code block using the Prism syntax highlighter.

The Prism syntax highlighter runs JavaScript loaded on the front-end to parse code blocks and apply syntax markup. A CSS file specifies the color and style to apply to that markup. See Prism syntax highlighter for full details.

Primary development and issues tracked on Github at: https://github.com/mkaz/code-syntax-block

螢幕擷圖

  • Example Syntax Highlighting
  • In Editor Example

安裝方式

After installing and activating the plugin, use in the Block Editor by creating a standard code block and selecting a language format.

When creating a new code block, select Code block, and then in the Inspector (Block Controls on the Right) select the language for the code. The code will not change within the editor, but you’ll see a small label with the selected language.

On the front-end when the post is being viewed, the code will be color syntax highlighted.

常見問題集

What languages are supported?

The Code Syntax Block plugin uses the Prism autoloader plugin to support all of the languages Prism supports. See the list of supported languages here.

How to customize the color scheme?

The default color theme is One Dark based off Atom’s One Dark theme . If you want to change the colors, you can download a new theme from the Prism themes repo or create your own.

The color theme is a single CSS file, there are a couple ways to customize:

  1. The plugin will check the current theme for the file: assets/prism/prism.css and use that file if it exists. Add your customize to a file in that location, and it will be used.

  2. If you do not like that file location, you can use the filter mkaz_prism_css_path and specify a path relative to your theme to use.

  3. If you would prefer specifying a full URL, you can use the filter mkaz_prism_css_url and specify a full URL to the stylesheet to use.

An example adding a filter to change the URL, add the following to your theme’s function.php

add_filter( 'mkaz_prism_css_url', function() {
    return 'https://raw.githubusercontent.com/PrismJS/prism-themes/master/themes/prism-hopscotch.css';
});

Note: if you use the title/filename feature and customize the CSS, you will need to apply your own style targeting the prism-titlename class.

How to customize the language list?

Use the filter mkaz_code_syntax_language_list to customize the list of languages to select displayed in the editor. By default the code syntax block shows a shorter list of popular languages, but Prism supports close to 200, see list.

Use this filter to extend to support the languages you need. Additionally you can use the filter to shorten the list to just the languages you use to make it easier to select.

Here is an example shortening the list to a smaller set:

add_filter( 'mkaz_code_syntax_language_list', function() {
    return array(
        "bash" => "Bash",
        "go" => "Go",
        "html" => "HTML",
        "javascript" => "JavaScript",
        "json" => "JSON",
        "markdown" => "Markdown",
        "php" => "PHP",
        "python" => "Python",
        "jsx" => "React JSX",
        "sass" => "Sass",
        "sql" => "SQL",
        "svg" => "SVG",
        "toml" => "TOML",
        "vim" => "vim",
        "xml" => "XML",
        "yaml" => "YAML",
    );
} );

Can I set a default language so I don’t have to select each time?

Yes, use the filter mkaz_code_syntax_default_lang to set a default language when inserting a code block. You can still change if you wish to show code not using the default language.

This example would set JavaScript as the default:

add_filter( 'mkaz_code_syntax_default_lang', function() { return 'javascript'; });

Can I override the conditional loading, so assets always load?

Yes, use the filter mkaz_code_syntax_force_loading to force always loading assets, otherwise it users has_block to check.

Example:

add_filter( 'mkaz_code_syntax_force_loading', '__return_true' );

使用者評論

2020 年 5 月 10 日
Great syntax highlighter plugin. It works like described. The language selector option is nice and we can set default language using filter hook.
2020 年 4 月 5 日
A very simple and intuitive plug-in without unnecessary frills. Thanks to the developer.
2020 年 3 月 24 日
The plugin does what it's supposed to do and is easy to customize. Great work.
閱讀全部 17 則使用者評論

參與者及開發者

以下人員參與了開源軟體〈Code Syntax Block〉的開發相關工作。

參與者

Code Syntax Block 外掛目前已有 2 個本地化語言版本。 感謝全部譯者為這個外掛做出的貢獻。

將 Code Syntax Block 外掛本地化為台灣繁體中文版

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

變更記錄

1.3.3

Fix block validation error for user roles with restricted permissions.

1.3.2

Fix issue with PHP warning when no posts

1.3.1

Fix compatibility issue with Gutenberg plugin 7.9
Removes in editor syntax preview added in 1.3.0

1.3.0

Upgrade Prism to 1.19
Add syntax preview in editor

1.2.4

Add filter to force loading assets, regardless of has_block

1.2.3

Conditionally load asset if has_blocks
Move Hack to front of default font list
Remove !important from font declaration
Change default font size to 15px for usability

1.2.2

Fix alignment issue with TwentyTwenty

1.2.1

Add Java and Kotlin to default language list

1.2.0

Upgrade Prism to 1.17.1

1.1.0

Add filters for customizing language list, and default language.

See full Release Notes on Github releases page