iTranslated by AI

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

Vim Commands to Make Creating Zenn Articles Slightly Easier

に公開
2022/12/24
7件

Zenn is a recently launched information-sharing site for programmers. It has been generating quite a buzz even on the vim-jp Slack.
The fact that you can easily write articles in your favorite local editor and preview them is very appealing to Vimmers. I am writing this article in Vim (specifically, Neovim) as well. It's fun!

A Vim command to make article creation just a little bit easier

To make it even slightly more comfortable to start writing Zenn articles in Vim, I've created the following ZennCreateArticle command. This assumes that the Zenn CLI is already installed.

function! s:create_zenn_article(article_name) abort
  let date = strftime("%Y-%m-%d")
  let slug = date . "-" . a:article_name
  call system("npx zenn new:article --slug " . slug )
  let article_path = "articles/" . slug . ".md"
  exe "edit " . article_path
endfunction

command! -nargs=1 ZennCreateArticle call <s>create_zenn_article("<args>")

With this, for example, if you open Vim from the project root on September 17, 2020, and type ZennCreateArticle vim-zenn-command in command-line mode, a new Markdown file named 2020-09-17-vim-zenn-command.md will be created under the articles/ directory.
Previously, you had to type:

npx zenn new:article --slug 2020-09-18-vim-zenn-command

But now, you can do it just with ZennCreateArticle vim-zenn-command! Awesome!

As for the implementation, it simply calls npx zenn new:article internally with a specified slug. There are no options or anything.
What happens if an error occurs in the called command? I don't know. Hmm, it feels very simplistic.
Please let me excuse myself by saying I made this on the fly even though I'm not yet used to Vim script at all... I might make various improvements later. It would be best if I could turn it into a plugin.

Why this slug name?

When you create an article with the CLI, you can freely decide the string called the slug at the end of the article's URL. Refer to the official What is a Zenn slug? for the role and naming conventions of slugs.

  • The slug becomes the file name.
  • The slug must be unique.
  • The slug must be within the pattern range of /[-a-z0-9]{12,50}/ (12 to 50 alphanumeric characters and hyphens).

Therefore, it seems better to make the following efforts for the slug:

  • Give it a name that makes the content easy to guess for visibility.
  • Want a mechanism to avoid duplication.

So, for the time being, I decided to use the naming convention yyyy-mm-dd-(content) for articles.
The constraint of 12 characters or more goes quite well with the prefix yyyy-mm-dd-, and by adding the date the article was created, you can also prevent duplication to some extent.
This is a matter of preference, and there is a possibility of specification changes in the future, so use it as you like.

Discussion

kkiyama117kkiyama117

はじめまして、突然すみません。
ただ今個人的にZennのVim Pluginを作成中の者なのですがこちらの関数(及びSlugのデフォルトの命名のアイデア)を拝借させていただいてもよろしいでしょうか?

1
monaqamonaqa

はじめまして.記事をご覧くださりありがとうございます.
もちろん大歓迎です!
もしそのプラグインをOSSとして公開される予定であれば,ぜひ私も使わせていただきたい/可能ならcontributeさせていただきたいと思います.

1
kkiyama117kkiyama117

ご返事及び許可を下さりありがとうございます.
https://github.com/kkiyama117/zenn-vim
とりあえずこちらにRepositoryを作ってみました. きちんとしたPluginを作ったことがなく不慣れなこともあり、まだ最低限のCommandのみですが、この記事のようなUtilitiesや非同期での処理の呼び出しを搭載する予定です.

1
monaqamonaqa

ありがとうございます!使ってみます.
教えていただいたプラグインを(もし差し支えなければ)vim-jp Slack コミュニティで紹介したいのですが,大丈夫でしょうか?

1
kkiyama117kkiyama117

まだ未完成なベーパーウェアですが、それでも大丈夫でしたら拡散,紹介はご自由にしていただいて大丈夫です.ありがとうございます!

1
kkiyama117kkiyama117

今更になりますが、上述のPlugin導入後に

  let g:zenn#article#slug = {
    \ "use_template": v:true,
    \ "format": "%F"
    \}
  command! -nargs=* ZennCreateArticle call zenn#new_article(<f-args>)

を設定し :ZennCreateArticle {slug} でこの記事と同様の動作をするようになったかと思います。
取り敢えず普通に需要がなさそうなpluginになってしまった気もするので、保守メインにしていく予定ですが、もしよければお試しください。
(slackでのご紹介ありがとうございます。自分がビビりなので紹介大変ありがたかったです。ROM専で申し訳なく思っていますが、#plugin で頂いた指摘を受け名前をzenn にしました。もしお使いになられる場合は zenn として落としてください。)