Kody for Craft CMS

FAQ

What does it cost?

Nothing. Kody is free. There are no editions, no licence key and no licensing code in the plugin.

Isn't there already a code field for Craft?

There was. luwes/craft3-codemirror was the last one, and it stopped in August 2022 without a Craft 4 or Craft 5 version. Until Kody, a Craft 5 site wanting to store code had two options: a plain textarea, where Tab moves focus out of the field, or a rich-text field that quietly mangles the code.

How is this different from nystudio107's CodeEditor?

CodeEditor is a library, not a field. It bundles Monaco and provides the Twig and Craft API autocomplete, and plugins such as Retour and SEOmatic use it for their own settings screens. It ships an input, though, not a field type you can put on an entry. Kody is built on it and adds the field, the snippet library and the front-end highlighting.

Why highlight on the server?

So visitors never download a highlighter. The page arrives already coloured: there's no flash of plain code, no highlighting pass in the browser, and it works with JavaScript off, in view-source and in an RSS reader. Highlighting is cached by content, so the same code on a thousand pages is tokenised once.

The only JavaScript Kody puts on a page is the copy button, and you can turn that off.

Can I still use Prism or highlight.js?

Yes. Print entry.snippet.code inside your own <pre><code class="language-…"> and Kody stays out of the way. Over GraphQL, code gives you the raw source for the same reason.

Does copying a block pick up the line numbers?

No. Line numbers are drawn with CSS counters, so they're never part of the text. The copy button copies the source exactly, byte for byte.

Why aren't snippets localised?

Because code isn't. An install command has no French version, and pretending it might would mean keeping one copy of every snippet per site. The caption and title around a snippet are yours to translate in the template.

Is .eval() safe?

It's off by default, and it takes two switches to turn on, one of which can be locked in config/kody.php. Once it's on, code either runs in Craft's Twig sandbox, or, without the sandbox, only runs if an admin last saved it. An author who can edit an entry but can't edit templates can't use a Kody field to read your database password.

Does Kody make any network requests?

No. Monaco is served from your own cpresources, the highlighter is plain PHP, and nothing phones home.

Which languages does it support?

49 in the editor, 46 of them highlighted on the front end. Run php craft kody/languages/list for the full table, or add your own with Languages::EVENT_REGISTER_LANGUAGES. See Configuration.

Does it work with GraphQL?

Yes. A Code field resolves to KodyCode, with the raw code, the language, the highlighted spans and the finished html block. A Snippets field resolves to KodySnippet.

More in Development

Pairs well with Kody

For the developer on the project. Migrations in and between sites, a code editor field, schema docs, content inventories, reports and email testing.