0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MarkdownのリストをチェックリストにするCLIコマンドを作った

Posted at

仕事をしているときにMarkdownの大量のリストをチェックリストに変換したくなったのでそのためのツールを作りました。"list to todo-list"の略で"litto"。

GitHubリポジトリはsosukesuzuki/littoです。

使い方

npmを使ってインストールできます。

$ npm install -g litto

ファイル名を指定して使うことができます。下のようなファイルを./hoge.mdとして作成します。

hoge.md
# ./hoge.md

- item1
- item2

ファイル名を指定してコマンドを叩くと結果が表示されます。

$ litto ./hoge.md
# ./hoge.md

-   [ ] item1
-   [ ] item2

なんかちょっと汚いのでPrettierで整形するためのオプションを用意しました。--formatオプションをつけるとフォーマットした結果が表示されます。

$ litto --write --format ./hoge.md
# ./hoge.md

- [ ] item1
- [ ] item2

--writeコマンドをつけるとファイルを上書きします。

$ litto --write ./hoeg.md
./hoge.md
Done.

仕組み

unifiedremarkというライブラリを使っています。これらのライブラリを使ってMarkdownをパースしてASTに変換します。詳しい使い方はそれぞれのドキュメントを読んでください。

これらのライブラリを使って生成されるASTはmdastに準拠しています。そしてListItemcheckedというプロパティの有無でリストかチェックリストかの判別をしているので、ASTを再帰的に走査してListItemのcheckedを(初期値の)falseにすることで変換を実装しています。

CLIコマンドとしてそれなりの出来にするためにcommanderを使っています。

感想

誰も使わなさそうですが、JavaScriptからもちゃんと叩けるようにしたいですね。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?