iTranslated by AI

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

Syntax highlighting language names can vary between platforms

に公開1件

This might seem obvious, but I've written this article as a self-reminder of a mistake I made.

Issue

Engineering blog services (for developers) like Zenn and Qiita have a feature that provides syntax highlighting when you specify the language name as follows.

`​``rust
fn main() {
    println!("Hello World!");
}
`​``
fn main() {
    println!("Hello World!");
}

However, the libraries used for syntax highlighting are Prism.js for Zenn and Rouge (written in Ruby) for Qiita. Naturally, not only are the supported languages different, but their names can also vary.

Prism.js is used for syntax highlighting.
(Quoted from https://zenn.dev/zenn/articles/markdown-guide#コードブロック)

Rouge is used for syntax highlighting, and (omitted)
(Quoted from https://qiita.com/Qiita/items/e84f5aad7757afce82ba)

For example, when you want to indicate console input/output, Rouge allows names like console, terminal, shell_session, and shell-session. In contrast, Prism.js uses shell-session, sh-session, and shellsession, which are different except for shell-session. (On the other hand, it feels like syntax highlighting isn't being applied on the Rouge/Qiita side... it's clearly different from the official sample. Because I was used to that, I didn't feel any sense of oddity about it not being applied.)

$ echo ok

Additionally, for C++, while Rouge allows two names, cpp and c++, Prism.js only allows cpp. Generally, I have the impression that Rouge has more aliases.

Countermeasures

If you are using Zenn CLI, a log saying Language does not exist: hogehoge will be displayed in the console where you ran zenn preview when you save a file, so you can notice it immediately.
I haven't checked the web version because disconnecting the GitHub integration is a hassle, but if my memory serves me correctly and no changes have been made, I believe no particular warning was shown.

You can check the correct names at https://prismjs.com/#supported-languages, so if you feel like nothing has changed, it's a good idea to check there once.

About Plain Text

Regarding plain text, while Rouge allows you to specify it with plaintext or text, it seems Prism.js does not (probably), so I feel there is no good way to display only the file name.

For example, if you don't specify a language like this, the file name will be ignored.

`​``:Output
Hello World!
`​``
Output
Hello World!

Also, if you write a language name that Prism.js cannot recognize like the following, the file name will be displayed, but if you're using Zenn CLI, Language does not exist: text will be output many times during editing, which is a bit bothersome.

`​``text:Output
Hello World!
`​``
Output
Hello World!

Currently, I'm thinking of just using diff as long as the lines don't start with + or -. Please let me know if you know of any better way.

`​``diff:Output
Hello World!
`​``
Output
Hello World!
GitHubで編集を提案

Discussion

eduidleduidl

GitHub Markdownではsh-sessionとconsoleが動く.今のところsh-session推奨かな.