• Stars
    star
    319
  • Rank 131,491 (Top 3 %)
  • Language
    CSS
  • Created over 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A WordPress plugin which extends Gutenberg adding color syntax highlighting to the code block.

Code Syntax Highlighting Block

A WordPress plugin which extends the WordPress Block Editor by adding syntax highlighting to the core code block.

Example:

screen shot

Install

Available on WordPress Plugin Directory at: https://wordpress.org/plugins/code-syntax-block/

You can install the code-syntax-block plugin from your WordPress plugins page, or download a zip from the releases page then upload and activate on your site.

Usage

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.

Customize

Colors

The default color theme is A11y Dark optimized for accessibility. If you want to change the colorscheme, you can select from the plugin settings page a few different themes shipped with the plugin. You can also use a 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. An example, creating a prism-theme.css file at the top-level of my theme directory:

	// Use local prism theme
	add_filter( 'mkaz_prism_css_path', function() {
		return '/prism-theme.css';
	});
  1. 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 customize the theme and use the title/filename option, you will likely need to update your CSS adjusting the style targeting prism-titlename class.

Languages

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 even simpler 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/Shell",
		"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",
	);
} );

You can also set a default language on the plugin settings page. If you set a default language when inserting a code block the language will be selected by default, you can still change if you wish to show code not using the default language. For the default language value, use an alias from the supported languages list.

Conditional Loading

By default the plugin will check if the current loop has_blocks and will only load the assets if the blocks are detected. If you need to override this, and force loading of assets using the following filter in your theme:

add_filter( 'mkaz_code_syntax_force_loading', '__return_true' );

Alternative

For server-side rendering, consider using Weston Ruter's Syntax-highlighting Code Block, it was initially forked from this block. Weston's block changes the parsing engine to use highlight.php that renders server-side. My block uses PrismJS which renders on the front-end and requires loading additional JS file.

Colophon

License

Copyright (c) 2018-2020 Marcus Kazmierczak.

Licensed under GPL 2.0 or later .

More Repositories

1

termgraph

a python command-line tool which draws basic graphs in the terminal
Python
3,131
star
2

working-with-go

A set of example golang code to start learning Go
1,152
star
3

lanyon

markdown web server
CSS
508
star
4

hastie

A static site generator in Python
Python
179
star
5

tsblock

An example WordPress block written in Typescript
TypeScript
25
star
6

fswatch

Python script that monitors a Mac OS X folder and on change syncs with remote site
Python
20
star
7

jot

A simple command-line tool to jot notes
Rust
20
star
8

wpsync

A utility to publish posts and media to your WordPress
Go
10
star
9

templets

WordPress plugin to make it easier to create block patterns.
PHP
10
star
10

juxtapose-block

A block for WordPress that enables drop in side-by-side image comparison
JavaScript
10
star
11

dotfiles

My configuration scripts
Vim Script
10
star
12

php-doge

A basic PHP library for interacting with dogecoind
PHP
9
star
13

oneliner

A command-line tool for saving one line notes in plain text files.
Python
7
star
14

chester-colors

Repo for collecting up Chester color scheems
HTML
7
star
15

ephemeral

A program that delete tweets
Go
5
star
16

qrcode-block

QRCode Block for Gutenberg editor - see step-by-step screencasts at https://mkaz.blog/
JavaScript
5
star
17

mkaz-block-template

A block template
Mustache
4
star
18

mcbain

A WordPress theme for mkaz.blog
PHP
4
star
19

fetcher

A client library to help make HTTP requests a little easier in Go.
Go
4
star
20

carbon

A WordPress theme using React
PHP
3
star
21

light.wtf

Photography Exposure Calculators
JavaScript
3
star
22

tasks

Command-line task todo list
Python
2
star
23

ctrl-alt-vim

A fork of vim-anywhere
Vim Script
2
star
24

neojot

A Neovim plugin for jotting notes
Python
2
star
25

test-block

A block for testing.
JavaScript
2
star
26

copyright-block

A copyright block for WordPress
PHP
2
star
27

octoack

Selectively query GitHub and post to Slack
Rust
2
star
28

gutenpride

A simple example block displaying Gutenberg pride
JavaScript
2
star
29

asciinema-block

A block for WordPress to play asciinema recordings
CSS
1
star
30

wpscrub

A WordPress plugin to remove a few bits
PHP
1
star
31

anim

Simple scripts for creating 2D animation using a camera
Python
1
star
32

browser-pad

A simple HTML page that turns your browser into a basic editor.
HTML
1
star
33

wprs

A toy WordPress client written in Rust
Rust
1
star