このブログは Zola 使ってるんだけど、 Zola では新しい記事追加のコマンドは提供されていない。

そこで、今流行りの Deno を使用して新規記事を追加するスクリプトを書いてみた。 こういうのサクッと作れるのすごい。

Deno install

Deno のインストールは公式にあるように、 curl で簡単に。

curl -fsSL https://deno.land/x/install/install.sh | sh

PATH は自分で追加する。

  • .zshenv
# deno.
export DENO_INSTALL=$HOME/.deno

path=(
  $DENO_INSTALL/bin(N-/)

  $path
)

zola-new コマンド

新規スクリプトとして、 zola-new を作成してみる。

vim zola-new.ts
  • zola-new.ts
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 -->
`
);

これをコンパイルする。

deno compile --unstable --allow-write zola-new.ts

zola-new ファイルができているので、これを PATH の通った場所にコピー。

cp zola-new ~/.deno/bin/

これで、 zola-new コマンドが使えるようになる。 こんな感じ。

 zola-new
Enter new post title: New post !!
New post !!
Create new post: 'content/2021-02-28_New post !!.md'

べんり。

ちなみに、上記スクリプトは github に push してるので、以下のように URL からもインストールできるみたい。

deno install --allow-write https://raw.githubusercontent.com/yukimemi/deno-scripts/master/zola-new.ts

すごい!!!