Shopifyのブログ記事にコードのシンタックスハイライト機能を付ける Tsun Inc.

Add code syntax highlighting to Shopify blog posts

小笠原 京平

My name is Ogasawara and I am CTO at Tsun Inc.!
Recently, we have been developing Shopify apps and themes.
In this blog, we will share information useful for Shopify development.
I would be happy if this would be helpful for Shopify engineers!

Why did you decide to make it?

When writing tech blog articles, I often include code, but code is hard to read in Shopify blog posts as is.

What you created

Based on Dawn, I added syntax highlighting for code in blog posts.
Libraries that implement syntax highlighting include Prism, highlight.js, and others, but this time I used Prism.

Below, I will explain how to add syntax highlighting to a Dawn theme using Prism!
If you would like to see the completed work directly, please refer to the Tsun Inc. repository.

Prerequisites

Target audience

  • You can change the theme of Shopify
  • Have basic knowledge of HTML, CSS, JavaScript, and Liquid

What to prepare

  • Dawn theme (Operation was confirmed with v5.0.0, but there should be no problem with previous versions.)
  • Blog article

Implementation

*This implementation method involves directly editing the Shopify theme code, so be sure to make a backup beforehand.

In this implementation

  • Creating Prism JS and CSS files
  • Load Prism with Liquid files for blog posts

There are two steps.

Creating Prism JS and CSS files

If you just want to try out the syntax highlighting feature, you can skip this step.

From Tsun Inc.'s GitHub repository prepared as a sample

Please download the following files.

If you would like to prepare the Prism file yourself, please do the following.

  1. https://prismjs.com/download.html Go to and check the required languages ​​and plugins

    Prism keeps things lightweight by generating JS and CSS files that include only the features you need.
    I checked the languages I thought I might need when writing future articles, along with the Line Highlight, Line Numbers, Unescaped Markup, Toolbar, Copy to Clipboard Button, and Diff Highlight plugins.

  2. Press the DOWNLOAD JS button and DOWNLOAD CSS button at the bottom to download prism.js and prism.css

Load Prism with Liquid files for blog posts

I think you have prism.js and prism.css ready, so let's incorporate them into the Shopify theme.

  1. From Shopify admin, go to Online Store > Themes and select the theme
  2. Actions > Edit code
  3. Assets > Select Add new asset
  4. Select and upload prism.js from Upload file
  5. Upload prism.css in the same way
  6. Open main-article.liquid in Sections
  7. Edit the code as below
  8. Select Save and save

Once you've done this, just write code tags in your blog post and Prism will automatically add syntax highlighting!
For how to write the tags, please refer to https://prismjs.com/index.html#basic-usage.

Points to note when using Script tags

If the code contains HTML tags such as span tags or script tags, they will be escaped by Shopify when saving the blog post, so please be careful because you need to prevent them from being escaped.

<pre><code class="language-css"><!--
<script src="{{ 'table-of-contents.js' | asset_url }}" defer="defer" />
--></code></pre>

It is OK if you write it like this.

Summary

This time, we introduced how to add code syntax highlighting to Shopify blog articles.
Prism is lightweight and multifunctional, so feel free to customize it to suit your needs!

Tsun Inc. also supports customization of Shopify themes.
If you would like to customize your Shopify theme, please contact Tsun Inc.

If you haven't used Shopify's blogging feature yet, please refer to the article below that explains Shopify's blogging feature.

Take full advantage of Shopify's blogging features | Introducing features and extension apps

👆️目次はShopifyアプリ「RuffRuff 目次作成」を利用

👆️Table of contents by the Shopify app RuffRuff Table of Contents

Back to blog
  • Shopifyで使える決済方法一覧|手数料・導入サービス・選び方

    [Complete Guide] A Roundup of Payment Methods A...

    Tsunライターチーム

    This article gives a detailed explanation of the many payment methods available on Shopify and their respective payment processing fees and transaction fees. We also introduce handy Shopify apps that...

    [Complete Guide] A Roundup of Payment Methods A...

    Tsunライターチーム

    This article gives a detailed explanation of the many payment methods available on Shopify and their respective payment processing fees and transaction fees. We also introduce handy Shopify apps that...

  • GMOイプシロンの手数料・決済方法・入金サイクル|Shopifyの条件も解説

    GMO Epsilon Fees, Payment Methods, and Payout C...

    Tsun ライターチーム

    GMO Epsilon payment methods and fees, organized by contract type. Covers Shopify-specific terms, payouts, authorization and capture deadlines, refunds, and cancellation. Also introduces ways to use apps to control which...

    GMO Epsilon Fees, Payment Methods, and Payout C...

    Tsun ライターチーム

    GMO Epsilon payment methods and fees, organized by contract type. Covers Shopify-specific terms, payouts, authorization and capture deadlines, refunds, and cancellation. Also introduces ways to use apps to control which...

  • Shopify向けSBペイメントサービス|対応決済・手数料・導入時の注意点

    SB Payment Service for Shopify: Supported Payme...

    Tsun ライターチーム

    This article organizes the payment methods and pricing available with SBPS for Shopify. Compare cost estimates that include fixed costs, authentication fees, and Shopify fees, along with capture deadlines and...

    SB Payment Service for Shopify: Supported Payme...

    Tsun ライターチーム

    This article organizes the payment methods and pricing available with SBPS for Shopify. Compare cost estimates that include fixed costs, authentication fees, and Shopify fees, along with capture deadlines and...

  • KOMOJUとShopifyの連携|決済方法・手数料・オーソリ期限

    Integrating KOMOJU with Shopify: Payment Method...

    Tsunライターチーム

    This guide lists the KOMOJU payment methods available on Shopify and their fees. It covers rates for physical goods and digital content, additional Shopify fees, setup steps, authorization periods, payouts,...

    Integrating KOMOJU with Shopify: Payment Method...

    Tsunライターチーム

    This guide lists the KOMOJU payment methods available on Shopify and their fees. It covers rates for physical goods and digital content, additional Shopify fees, setup steps, authorization periods, payouts,...

  • Shopifyのセキュリティ対策|決済・アプリ要件・バグバウンティ

    Shopify Security: Payments, App Requirements, a...

    Tsun ライターチーム

    For businesses considering Shopify, this article explains security safeguards for communications, customer information, payments, and apps, along with third-party assessments. Review the scope of protection, compensation requirements, operational preparations, and...

    Shopify Security: Payments, App Requirements, a...

    Tsun ライターチーム

    For businesses considering Shopify, this article explains security safeguards for communications, customer information, payments, and apps, along with third-party assessments. Review the scope of protection, compensation requirements, operational preparations, and...

  • Shopifyサーバーの費用・稼働率・障害時の対処法

    Shopify server costs, uptime, and how to respon...

    Tsun ライターチーム

    Shopify web hosting is included in the plan fee. This article organizes costs and the scope of management, the difference between official uptime figures and contractual guarantees, the checks to...

    Shopify server costs, uptime, and how to respon...

    Tsun ライターチーム

    Shopify web hosting is included in the plan fee. This article organizes costs and the scope of management, the difference between official uptime figures and contractual guarantees, the checks to...

1 of 6
  • AI仮想顧客の改善ツール「SimGym」を使ったら売上はどうなった?実行から3か月後までの記録

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

  • 【ストアインタビュー後編】やりながら育てるShopify。「野菜をMOTTO」の試行錯誤:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

  • 【ストアインタビュー前編】 ECが主戦場でも、ECが目的ではない。「野菜をMOTTO」が貫くEC戦略:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

1 of 3
  • Our Editorial Process

    For readers new to e-commerce and Shopify, we write our articles with care to explain difficult commerce and marketing terms, along with how everything works from building a store through to selling, as “gently,” “clearly,” and “accurately” as we can.

    Learn more 
  • About the information on this site

    The information published here is based on Tsun Inc.’s own experience and research. While we make every effort to publish information that is as “current” and “accurate” as possible, we cannot fully guarantee its contents. Tsun Inc. accepts no liability whatsoever for any damages arising from this site. If you find an error in the information published here, we would be grateful if you would contact Tsun Inc.