Linux 软件免费装

Coywolf Code Block Enhancer

开发者 jonhenshaw
更新时间 2026年6月15日 08:28
PHP版本: 7.4 及以上
WordPress版本: 7.0
版权: GPLv2 or later
版权网址: 版权信息

标签

gutenberg code syntax highlighting prism copy code

下载

1.0.57

详情介绍:

Coywolf Code Block Enhancer extends the built-in core/code block. In the editor it adds a "Code language" dropdown to the block sidebar; on the front end it highlights the code with Prism.js using a custom token palette, prints the language name as a small label on the block, and pins a copy-to-clipboard button to the top-right corner. How it works The chosen language is stored as a language block attribute on core/code, which lives in the block delimiter comment rather than the saved markup. That means blocks without a language stay valid and existing content is never migrated. On render, the plugin uses WP_HTML_Tag_Processor to add data-language to the <pre> and language-xxx to the <code> server-side — so KSES won't strip data-* attributes for non-admin authors, and there is no block-validation churn. Prism core and the per-language grammars are bundled under assets/prism/ at v1.30.0 (MIT — see assets/prism/LICENSE.txt). They register as deferred scripts with explicit dependency ordering (e.g. markup-templating before php, clike before languages that extend it). The copy-button script depends on the last grammar in the chain, so all of Prism is present before the copy UI is wired up. Reading code.textContent returns the original source even after Prism wraps tokens in spans, so the copied text is unaffected by highlighting. Self-hosting Prism (rather than loading from a public CDN) keeps the third-party-script supply chain off the plugin's surface and means the plugin works on sites with strict CSPs or no external egress.

安装:

  1. Upload the code-block-enhancer folder to /wp-content/plugins/, or upload the .zip via Plugins → Add New → Upload Plugin.
  2. Activate the plugin.
  3. Edit a post or page, add (or open) a Code block, and pick a language from the "Code language" panel in the block sidebar. The code is highlighted on the front end and a copy button appears in the top-right of the block.

屏幕截图:

  • A syntax-highlighted JSON-LD code block rendered on the front end of a Coywolf Guides article, showing a Schema.org Article example with @context, @type, headline, description, and a nested author Person object.

常见问题:

Which languages are supported out of the box?

Bash/Shell, CSS, HTML/Markup, JavaScript, JSON, PHP, Python, SQL, and YAML. The dropdown also includes "None (plain text)" to render a block without highlighting.

How do I add another language?

Open Tools → Code Blocks → Language packs and enable the pack that contains the language you need. The baseline 9 languages are always available; the other 40 grammars are grouped into packs you can toggle on. Anything you enable shows up in the Code block's "Code language" dropdown automatically — no code editing required.

Will this break my existing code blocks?

No. The language is stored as a block attribute, not baked into the saved markup, so blocks without a language stay valid and the front-end language class is applied at render time. Existing content is not migrated and is unaffected.

Does Prism load on every page?

No. The token CSS only enqueues on singular posts/pages where has_block( 'core/code' ) is true, and the Prism scripts are only enqueued from inside the render_block filter for core/code — so a page with no code block ships none of these assets. Prism is also loaded with the defer strategy so it never blocks rendering.

My site has a Content Security Policy. What do I need to allow?

Nothing extra. Prism and the copy-button script are bundled with the plugin and served from your own origin, so a script-src 'self' policy is enough. There is no external CDN call from the front end.

How do I change the theme?

Go to Tools → Code Blocks in WP Admin. The "Code block theme" dropdown lists every bundled theme in three groups: Coywolf (Default — Auto / Always light / Always dark), Prism (built-in), and Prism Themes (community). Below the dropdown a live preview pane re-renders a sample PHP snippet in the highlighted theme on every change — your site keeps the previously saved theme until you click Save Changes. The selected theme's stylesheet is enqueued only on posts that contain a code block; only one theme file is ever loaded per request.

How do I lock code blocks to light or dark mode for everyone?

If you're on the bundled Default — Auto theme (the first-install default), switch to Default — Always light or Default — Always dark in Tools → Code Blocks. Picking any of the Prism themes also locks the appearance — those themes are static and don't react to OS dark mode. The lock is implemented in CSS — there is no inline <style> injected per request — so it composes cleanly with caching plugins.

Where do the Prism themes come from?

The 8 stock themes are bundled from PrismJS/prism at v1.30.0 (MIT — see assets/themes/LICENSE-prism.txt). The 37 community themes are bundled from PrismJS/prism-themes (MIT — see assets/themes/LICENSE-prism-themes.txt). They are served from your own origin alongside the rest of the plugin's assets — no external request at runtime.

Why is the language label not appearing on a particular block?

The label only renders when a language is set (the CSS rule is .wp-block-code[data-language]::before). If the block was created before the plugin was installed, open it in the editor and pick a language from the sidebar so the language attribute is saved.

更新日志:

1.0.57 1.0.56 1.0.55 1.0.54 1.0.53 1.0.52 1.0.51 1.0.50 1.0.49 1.0.48 1.0.47 1.0.46 1.0.45 1.0.44 1.0.43 1.0.42 1.0.41 1.0.40 1.0.39 1.0.38 1.0.37 1.0.36 1.0.35 1.0.34 1.0.33 1.0.32 1.0.31 1.0.30 1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 1.0.18 1.0.17 1.0.16 1.0.15 1.0.14 1.0.13 1.0.12 1.0.11 1.0.10 1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 1.0.0