このブログは Zola 使ってるんだけど、 Zola では新しい記事追加のコマンドは提供されていない。
そこで、今流行りの Deno を使用して新規記事を追加するスクリプトを書いてみた。 こういうのサクッと作れるのすごい。
Deno install
Deno のインストールは公式にあるように、 curl で簡単に。
bash
1
curl -fsSL https://deno.land/x/install/install.sh | shPATH は自分で追加する。
- .zshenv
zsh
1
2
3
4
5
6
7
8
# deno.
export DENO_INSTALL=$HOME/.deno
path=(
$DENO_INSTALL/bin(N-/)
$path
)zola-new コマンド
新規スクリプトとして、 zola-new を作成してみる。
bash
1
vim zola-new.ts- zola-new.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { formatISO } from "https://deno.land/x/date_fns/index.js";
import * as color from "https://deno.land/std/fmt/colors.ts";
import * as path from "https://deno.land/std/path/mod.ts";
const input = async (): Promise<string> => {
const title = await prompt("Enter new post title:");
if (title === null) {
return input();
}
return title;
};
const title = await input();
console.log(color.cyan(title));
const base = "./content";
const today = formatISO(new Date(), { representation: "date" });
const todayIso = formatISO(new Date(), {});
const post = path.join(base, `${today}_${title}.md`);
console.log(color.green(`Create new post: '${post}'`));
await Deno.writeTextFile(
post,
`+++
title = "${title}"
date = ${todayIso}
draft = true
[taxonomies]
tags = []
+++
<!-- more -->
`
);これをコンパイルする。
bash
1
deno compile --unstable --allow-write zola-new.tszola-new ファイルができているので、これを PATH の通った場所にコピー。
bash
1
cp zola-new ~/.deno/bin/これで、 zola-new コマンドが使えるようになる。
こんな感じ。
bash
1
2
3
4
❯ zola-new
Enter new post title: New post !!
New post !!
Create new post: 'content/2021-02-28_New post !!.md'べんり。
ちなみに、上記スクリプトは github に push してるので、以下のように URL からもインストールできるみたい。
bash
1
deno install --allow-write https://raw.githubusercontent.com/yukimemi/deno-scripts/master/zola-new.tsすごい!!!