iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚚

Integrating a Gatsby Blog with Zenn

に公開
2021/09/22

Adapting for Zenn

I have set up a configuration to multi-post from my personal blog (Gatsby blog) to Zenn.
The canonical_url is pointed toward Zenn.

Result repository: elzup/anozon-blog

I have been trying to write at least once a week, and before I knew it, I had written 91 articles.
I want to further increase my motivation to write by benefiting from the Zenn editor and format, and by receiving feedback on Zenn.

As mentioned in the official article, I will adapt the structure, Markdown, and filenames to support the GitHub integration provided by Zenn.

Deployment Settings (Export Process)

I will export the files without changing the current file structure and push them to a Zenn-specific branch using GitHub Actions.
I will omit the settings on the Zenn side as they can be done almost with a single button.

Directory Location

The current location of the articles is at the following path:

./content/blog/2021/2021-09-18___blog-title.md

Zenn has a fixed structure as follows. It is generated by running zenn-init with zenn-cli. It is very simple.

/articles
/books

I will temporarily export it using the /out_zenn directory as the root.

I created a script to copy /content/blog/**/*-slug.md to /out_zenn/articles/slug.md.
Code: anozon-blog/export_zenn.js at master · elzup/anozon-blog

I could have used a shell script, but I wrote it in Node for extensibility.

When I ran zenn preview in out_zenn, it was displayed properly.

zenn-local-preview

Creating Zenn export GitHub Actions

s0/git-publish-subdir-action was useful for committing to a specific branch in GitHub Actions.

Add scripts to packages.json

packages.json
  "scripts": {
    "zenn:export": "node export_zenn.js",
  }

Create .github/workflows/zenn.yml.

zenn.yml
# Omitted
steps:
  - uses: actions/checkout@master
  - uses: actions/setup-node@v2
    with:
      node-version: ${{ matrix.node-version }}
  - name: Push to build branch
    uses: s0/git-publish-subdir-action@master
    env:
      REPO: self
      BRANCH: zenn
      FOLDER: out_zenn
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Fixing to Zenn's File Format

Errors appeared when I opened the articles in the preview, so I will address them. It's very helpful because it's easy to understand 😎.
zenn-error

Supporting Markdown Metadata

Since I have no particular attachment to the existing naming, I will migrate everything to the keywords used by Zenn.

topics, published, and type support

  • topics -> topics
  • status: draft -> published: false
  • Added type: tech

I batch-replaced the additional metadata using regular expressions.
s/( -.*\n)---/$1type: tech\npublished: true\n---
zenn-add-tag

Parts related to the Gatsby-side code also need to be addressed. I updated topics, status, etc.

Adding emojis

I only set these for my favorite articles. Optionally, it sets a random emoji (perhaps from a hash?).
It looks quite decent even if it's random.

Fixing slugs that are too short

I will adjust the slug (the keyword in the filename) to be between 12 and 50 characters.
There were some shorter than 12 characters, so I fixed them. It was a bit of a hassle.
Naming was easy since I wasn't particularly picky about them.

Handling the prohibition of symbols in topics

On Zenn, registered Topic tags are mapped to icons and labels for display.
In existing articles, I had included symbols like spaces and dots, so I fixed them.
(In the first place, I was writing them with symbols without even knowing if they were correct as metadata notation.)
I appreciate the kindness in how it handles case sensitivity.

Modifying Markdown Body

Handling diff-type code blocks

For some reason, diff-type code blocks didn't work properly.
I got something like a JSON parse Error.
I addressed this by replacing ```diff.* with ```.

Deleting the Table of Contents (gatsby-remark-table-of-contents)

I was using gatsby-remark-table-of-contents | Gatsby for the table of contents.

```toc
# This code block gets replaced with the TOC
```

It was not ideal to have to insert this into every article where I wanted a table of contents, so I deleted it.

canonical url

I configured this using gatsby-plugin-react-helmet-canonical-urls | Gatsby.

I also tried gatsby-plugin-canonical-urls, but since it couldn't handle a siteUrl that includes a path (it would result in https://zenn.dev/slug), I decided against it.

Completion 🎉

I was able to import 91 articles.

Other things left for later

  • Removing things like 【JavaScript】 from article titles.
  • Support for code block titles
    • Since it depends on Gatsby, it's currently in a slightly awkward format like ```js title=hoge.js.

Regarding Zenn CLI preview and editing

You can see the Zenn preview immediately by adding it to npm scripts and running yarn zenn.

package.json
    "zenn": "yarn zenn:export && (cd out_zenn && npx zenn)",

Enabling the Zenn Editor VSCode Plugin

vscode-zenn-editor

Since it cannot be used without the articles and books directory structure, I achieved this using symbolic links.

mkdir zenn_tmp
ln -s ../content/blog/2021/ ./articles
code zenn_tmp

I tried it out of curiosity to see how the Markdown would be displayed, but I probably won't use it much (if at all) in the future.

A few challenges

Currently, once GitHub integration is enabled, I can't test out what the writing UI on the Zenn web platform is like.
I have some interest in writing specifically "books" on the web.

Thoughts

Overall, it was good because I ended up addressing parts of the setup that I had previously found unsatisfactory.
The error messages in Zenn's local preview were extremely clear, providing a high-quality experience.

GitHubで編集を提案

Discussion