WordPress plug-in for Swift syntax highlighting?

Does anyone know of a WordPress plug-in that provides good syntax-highlighting for Swift code snippets?

I've tried about a dozen, spanning every library I can find - Prism.js, Enlighter.js, Highlight.js, VS Code, etc, but they're all pretty terrible; they just don't correctly parse even basic Swift syntax like property and function declarations. Prism.js via the Code Syntax Block plug-in seems to be the least bad of them all.

1 Like

I've tried to find the same, and haven't been really happy with any of the results either. I don't have any suggestions, but would love to know if you find anything that you like.

Ugh, this ended up taking an absurd amount of my time. The fruits of that dubious labour are available now, though: Swift code syntax highlighting in WordPress.

TL;DR: they pretty much all do a poor job, although Code Block Pro has the most promise, and SyntaxHighlighter Evolved gets an honourable mention as a relatively lightweight "dumb" plug-in that does a surprisingly passable job nonetheless.

If folks in our community, like @jtbandes, continue doing the hard work to improve Swift's syntax-highlighting support in Visual Studio Code (and on GitHub), then those benefits should eventually make it into Code Block Pro as well, as they all share the same language grammar definitions. So there's perhaps room for optimism that the situation will get better.


Have you looked into DocC? It uses Highlight.js, with some modifications:

I also rely on Highlight.js for my publishing work.

There's another syntax highlighting tool I've found recently, Shikiji. It produces the same highlighting result as VS Code.

This is a swift example I tried.

I didn't find any working WordPress plug-ins that use highlight.js specifically, but several plug-ins that I tested use highlight.php - nominally a pretty direct port between the languages, although I don't know how well-synchronised they are. In any case, highlight.php performed worse than most of the other engines.

It's a fork of Shiki, which Code Block Pro uses. As far as I can tell there aren't any WordPress plug-ins which utilise Shikiji specifically, but I'd expect the results to be equivalent anyway.

As I noted, Shiki seems like the most promising engine because it shares grammar and themes with prominent tools, even though it doesn't actually do that much better a job today than some of the other options, like Prism.js. Until just a few weeks ago the best TextMate grammar definition available for Swift was one that hadn't been updated since the Swift 4 era.

Theming is also a big ugly spot with Shiki et al. "TextMate grammars" - meaning, the derivative grammars used by Visual Studio Code et al - don't reliably use a standardised intermediary representation, and compounding that is that most themes don't support the most generic intermediary representation anyway - typically they "hard-code" for the particulars of just a couple of languages, like Java or JavaScript - which is in large part why the end results with Swift are so surprisingly poor with all the popular Visual Studio Code themes, even moreso than they need to be based on the limits of the grammar file.

I spent several days trying to create a proper Visual Studio Code theme for Swift, and while it took barely an hour to make one far better than any existing ones I could find, it was clear that to complete it would take way more time again. I might return to it at some point, perhaps after the Swift grammar definition is improved and stabilised (because of the unfortunate aforementioned close-coupling between themes and grammars).