Abusing the #split helper for "short codes" in Ghost

First I built a #split helper. Then I decided to see how far I could push it.

Abusing the #split helper for "short codes" in Ghost

I was working on a fairly complicated page layout, and it really seemed like a place that short codes would be useful. Can we do that in Ghost?

🔮
The #split helper was one of my 2025 contributions to Ghost. If you'd like to fund more open-source contributions to Ghost, here's my Github Sponsors page.

Officially, there's no shortcode support. Unofficially, well, that #split helper looks a lot like it might be able to parse at least simple shortcodes.

All I needed was a post on the forum asking about it to cause me to go off on this tangent. (Yeah, it doesn't take much. It's a bug and a feature.)

Here's some code:

{{#split html separator="[[rss-only]]" as |parts|}}
  {{#foreach parts}} 
    {{!-- check if the chunk contains the closing tag --}}
    {{#match this "~" "[[/rss-only]]"}} 
       {{#split this separator="[[/rss-only]]" as |innerParts|}}
          {{#foreach innerParts}}
             {{#unless @first}} {{!-- skip the first part, which precedes the closing tag --}} 
              {{this}} 
             {{/unless}}
          {{/foreach}}
        {{/split}}
    {{else}} 
        {{this}}
    {{/match}}
  {{/foreach}}
{{/split}}

So let's assume we have a post, with content like this:

possible intro text
[[rss-only]] something only for the feed [[/rss-only]]
something else 
[[rss-only]] something only for the feed [[/rss-only]]
possible ending text 

The outer split cuts it ("parts"):

possible intro text

 something only for the feed [[/rss-only]]
something else 

 something only for the feed [[/rss-only]]
possible ending text 

And then the inner split (making "innerParts") removes anything ending in [[/rss-only]], which effectively clips off the closing tags and the content within the tag.

Major caveat: This works on web, and can also work for RSS, if you're using a custom RSS feed. It isn't going to work in email, nor on ActivityPub, nor if you're retrieving content via the API. There, you'll see your shortcodes exactly as they appear in the editor, alas.

Having said that, there are definitely times where I've wished I could change Ghost's behavior with card rendering, and this is potentially a way to do that, again with the caveats about not working outside web and rss. But, if you've ever wanted a bookmark card that didn't show the site or author name, or product cards that used a different icon than stars, or some other interesting shenanigans done with the content field of the post, this is a possible way to make that happen.


Hey, before you go... If your finances allow you to keep this tea-drinking ghost and the freelancer behind her supplied with our hot beverage of choice, we'd both appreciate it!