Description
Shawn Code Highlighter brings back syntax highlighting for code that lost it: SyntaxHighlighter blocks left behind after that plugin was removed, pre blocks imported from another platform, and plain pre blocks with no language hint. It works on the rendered page only, so stored post content is never rewritten.
Code is shown in a dark palette inspired by Xcode’s default dark editor theme. This plugin is not affiliated with or endorsed by Apple.
Supported markup:
- SyntaxHighlighter blocks (
wp:syntaxhighlighter/code), using the language saved in the block. pre > codewithlanguage-*,lang-*orbrush:*classes.- Migrated
pre.source-codeblocks. - CodeMirror-exported blocks (
div.cm-lineper line) andpreblocks that use<br>for line breaks. - Plain
preblocks, using constrained language auto-detection.
Long lines wrap inside the code block instead of forcing a horizontal scrollbar.
Assets load only on single posts that contain a pre element or a SyntaxHighlighter block. There are no settings, no tracking and no external requests: all files are bundled with the plugin.
The plugin highlights code inside the post content area of block themes (.wp-block-post-content) and classic themes (.entry-content).
Third-party software
This plugin bundles Highlight.js 11.12.0, distributed under the BSD 3-Clause License (see assets/vendor/LICENSE).
- Source code: https://github.com/highlightjs/highlight.js
- The bundled
highlight.min.jsis the unmodified minified release file. - The color theme (
assets/xcode-dark.css) is original to this plugin and licensed GPLv2 or later.
FAQ
Can I set a default language for code without a language hint?
Yes. Use the shawn_code_highlighter_default_language filter. For example, to treat unlabelled code in a “swift” category as Swift:
add_filter( 'shawn_code_highlighter_default_language', function ( $language, $post ) {
return ( $post && has_category( 'swift', $post ) ) ? 'swift' : $language;
}, 10, 2 );
Can another plugin load the highlighter on its posts?
Yes. A block that renders its code with JavaScript has no pre in the saved post, so it can opt in with the shawn_code_highlighter_load_assets filter:
add_filter( 'shawn_code_highlighter_load_assets', function ( $load, $post ) {
return $load || has_block( 'my/code-block', $post );
}, 10, 2 );
Highlight.js is then available as window.hljs for that block to use.
Does the plugin change my posts?
No. Language hints are added to the rendered HTML only; post content in the database is never modified.
Changelog
1.2.1
- Update the bundled Highlight.js to 11.12.0.
1.2.0
- Add the
shawn_code_highlighter_load_assetsfilter so blocks that render code at runtime can load the highlighter.
1.1.0
- Add the
shawn_code_highlighter_default_languagefilter in place of site-specific category rules. - Support classic themes that use
.entry-content. - Replace the Atom One Dark theme with an Xcode-inspired dark palette.
- Wrap long lines instead of scrolling horizontally.
- Keep line breaks in CodeMirror-exported and
<br>-separated code blocks.
1.0.1
- Prefer explicit Swift, shell, JSON, YAML, XML, and C++ signals before automatic language detection.
1.0.0
- Initial compatibility release.