LoginSignup
67
53

More than 5 years have passed since last update.

Hugoで新規記事を作成するときのTips的なメモ

Last updated at Posted at 2016-06-09

静的サイトジェネレーターのHugoを使って新規記事作成時のあれこれ

記事のテンプレートを作成したい

通常はhugo newで記事を新規作成します。

$ hugo new post/hello.md

で作成すると

post/hello.md
+++
date = "2016-06-09T03:15:52+09:00"
draft = true
title = "hello"

+++

みたいなものが自動生成されます。このデフォルトのFront Matter(+++で囲まれてるページ独自の変数)部分をカスタマイズしてみます。HugoのArchetypesのドキュメントページにも書いてますがarchetypes/default.mdを作成します。

archetypes/default.md
+++
categories = ["", ""]
date = ""
description = ""
draft = true
image = ""
tags = ["", ""]
title = ""
author = ""
+++

これを作った上でもう一度記事を作成してみましょう。

$ hugo new post/world.md
post/world.md
+++
author = ""
categories = ["", ""]
date = "2016-06-09T03:22:03+09:00"
description = ""
draft = true
image = ""
tags = ["", ""]
title = "world"

+++

こんな感じで、titledateの項目は自動で入力してくれました。draft=trueなどデフォルト値で固定で入れたい場合はarchetypes/default.mdに記載しておきましょう。

テンプレートを複数用意したい

テンプレートはarchetypes/default.md以外にも準備可能です。
例えば、archetypes/blog.mdを用意するとblogディレクトリ配下の投稿のテンプレートを作ることができます。

こんな例です。

archetypes/blog.md
+++
categories = ["", ""]
date = ""
description = ""
draft = true
image = ""
tags = ["", ""]
title = ""
author = ""
+++

## はじめに

## ~~~とは

## ~~~の紹介

## まとめ

実際に記事を作ってみます。

$ hugo new blog/test.md
blog/test.md
+++
author = ""
categories = ["", ""]
date = "2016-06-09T03:32:08+09:00"
description = ""
draft = true
image = ""
tags = ["", ""]
title = "test"

+++

## はじめに

## ~~~とは

## ~~~の紹介

## まとめ

Front Matterのフォーマット指定を指定したい(-f フラグ)

hugo newのフラグ指定は

$ hugo new パス フラグ

という使い方です。

デフォルトはTOMLですが、JSONにしたいときは以下のようにすれば大丈夫です。

hugo new blog/hello.md -f JSON

で指定するとこのような形で

test-blog.md
{
   "author": "",
   "categories": [
      "",
      ""
   ],
   "date": "2016-06-09T03:46:15+09:00",
   "description": "",
   "draft": true,
   "image": "",
   "tags": [
      "",
      ""
   ],
   "title": "hello"
}

エディタを指定して書き始める (--editor フラグ)

これが個人的にイチオシです。 --editor="エディタ名"です。
僕はAtomをよく使うのでこんな感じ

$ hugo new blog/hello.md --editor="atom"

これで指定するとそのままエディタ画面で編集開始できて便利です。

参考: hugo newで作られる記事ファイルのfront matterをカスタマイズする

雑感

もう少し細かく指定できる方法ってないのかな

hugo new post/hello.md -var hoge=huga

post/hello.md
+++
date = "2016-06-09T03:15:52+09:00"
draft = true
title = "hello"
hoge = "huga"
+++

みたいなことやりたい

67
53
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
67
53