iTranslated by AI
Vim Commands to Make Creating Zenn Articles Slightly Easier
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
はじめまして、突然すみません。
ただ今個人的にZennのVim Pluginを作成中の者なのですがこちらの関数(及びSlugのデフォルトの命名のアイデア)を拝借させていただいてもよろしいでしょうか?
はじめまして.記事をご覧くださりありがとうございます.
もちろん大歓迎です!
もしそのプラグインをOSSとして公開される予定であれば,ぜひ私も使わせていただきたい/可能ならcontributeさせていただきたいと思います.
ご返事及び許可を下さりありがとうございます. とりあえずこちらにRepositoryを作ってみました. きちんとしたPluginを作ったことがなく不慣れなこともあり、まだ最低限のCommandのみですが、この記事のようなUtilitiesや非同期での処理の呼び出しを搭載する予定です.
ありがとうございます!使ってみます.
教えていただいたプラグインを(もし差し支えなければ)vim-jp Slack コミュニティで紹介したいのですが,大丈夫でしょうか?
まだ未完成なベーパーウェアですが、それでも大丈夫でしたら拡散,紹介はご自由にしていただいて大丈夫です.ありがとうございます!
今更になりますが、上述のPlugin導入後に
を設定し
:ZennCreateArticle {slug}でこの記事と同様の動作をするようになったかと思います。取り敢えず普通に需要がなさそうなpluginになってしまった気もするので、保守メインにしていく予定ですが、もしよければお試しください。
(slackでのご紹介ありがとうございます。自分がビビりなので紹介大変ありがたかったです。ROM専で申し訳なく思っていますが、#plugin で頂いた指摘を受け名前を
zennにしました。もしお使いになられる場合はzennとして落としてください。)ありがとうございます!記事の機能を取り込んでいただけてとても嬉しいです.
またZennの記事を書くときは,以下の記事も参考にしつつぜひ使ってみようと思います.