I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go

Introduction

Even if you create a blog, it doesn't make much sense unless you read it, and in that sense, an eye-catching image when sharing SNS such as Twitter Card is important. So, this time I tried to create OGP using a library called tcardgen made by Go. tcardgen ↑ It's the one who makes a Twittter Card like this.

Preparation

install

Install with go get

$ go get github.com/Ladicle/tcardgen

Download because README.md tells you to use the kinto font

ookamiinc/kinto: Equal — Kinto is a Japanese font family adapted to match size & balance with Latin characters in user interfaces. A project based off Google Noto fonts.

This time I hope it can be used to create this image, so save it in a directory like static/fonts/Kinto_Sans

template

Create the original background image template. There are various sample images in example of the tcardgen repository. The size is 1200 x 628 (px) Create with.

After that, I used the iPad's Affinity Designer to make the sample image transparent and create a nice design.

Preparation

markdown setting

For tcardgen, it is necessary to set some items of category and tags. Therefore, add these items to the article you want to create. Also, make sure that you can draw the path of the created images. (Details will be described later.)

#Settings for Twitter card gen
author: ["Sardines"]
categories: ["Tech"]
tags: ["tcardgen", "Hugo", "OGP"] # tag
ogimage: "images/og/tcardgen-hugo.png " #Set the image generated by tcardgen as an OGP image
url: "/blog/tcardgen-hugo/" #Set the path for the auto-generated script in tcardgen It also means fixed routing
carduse: true #Whether to use Twitter Card If false, the default image will be applied

First, when writing an article

$ hugo new ./content/blog/{Japanese}.md

However, if this is left as it is, the path will be in Japanese and the URL will be dirty, so when creating it, create .md in the alphabet appropriately and change the title to Japanese later. I decided to do it.

At this time, the path is (originally) determined based on this reset title, so if you change it to Japanese, the path will also be in Japanese, but if you specify as follows, this will be given priority. Will give you. Also, this url is automatically generated from {} .md at the time of article creation.


url: "/blog/tcardgen-hugo/"

yaml settings

The style of the image to be created can be set in tcardge.yaml. This can be used as there is a sample in tcardgen.

In my case, I decided the layout based on the sample image, so I used it as it is.

template: static/ogp/template.png
title:
  start:
    px: 113
    pY: 252
  fgHexColor: "#FFFFFF"
  fontSize: 68
  fontStyle: Bold
  maxWidth: 1000
  lineSpacing: 10
category:
  start:
    px: 113
    py: 211
  fgHexColor: "#E5B52A"
  fontSize: 42
  fontStyle: Regular
info:
  start:
    px: 223
    py: 120
  fgHexColor: "#A0A0A0"
  fontSize: 38
  fontStyle: Regular
tags:
  start:
    px: 120
    py: 500
  fgHexColor: "#FFFFFF"
  bgHexColor: "#7F7776"
  fontSize: 22
  fontStyle: Medium
  boxAlign: Left
  boxSpacing: 6
  boxPadding:
    top: 6
    right: 10
    bottom: 6
    left: 8

Create

[Hugo] Automatically generate OGP images using tcardgen --michimani.net

Use this article as a reference to create and execute a script.

if [ $# != 1 ] || [ $1 = "" ]; then
    echo "One parameters are required"
    echo ""
    echo "string: path to markdown file of target post"
    echo ""
    echo "example command"
    echo "\t$ sh ./makeogp.sh ./content/post/test/test.md"
    exit
fi

TARGET_POST_PATH=$1

tcardgen \
    --fontDir ./static/fonts/Kinto_Sans \
    --output static/images/og \
    --template static/ogp/template.png \
    --config tcardgen.yaml \
    $TARGET_POST_PATH

The usage example command is

$ ./makeogp.sh ./content/blog/tcardgen-hugo.md    

Specify the article you want to create with the argument.

The created one is output to `` `--output static/images/og``` specified in the shell.

I was able to create it!

Not over yet

Register as ogp

Well, the image is done, but this is not the end. That's right. Must be specified in the head tag.

So, add the following to `layouts/partials/head.html`.

  {{"<!--Set up a Twitter Card for your blog-->" |safeHTML}}

  <meta name="twitter:image:src" content="https://biwashi.github.io/blog/images/iwashilong_w.jpg ">

  <meta property="og:image" content="https://biwashi.github.io/blog/images/iwashilong_w.jpg " />
  
  {{ if eq true .Params.carduse }}
  {{"<!--Since it is a blog, display custom ones-->" |safeHTML}}
  <meta property="og:image" content="{{ .Site.BaseURL }}{{ .Params.ogimage }}"> 
  <meta name="twitter:image:src" content="{{ .Site.BaseURL }}{{ .Params.ogimage }}">
  {{ else }}
  {{"<!--Since it is Home, the default one is displayed-->" |safeHTML}}
  {{ end }}

Here, we will use various parameters that we installed in markdown earlier.

I will explain in order.

  <meta name="twitter:image:src" content="https://biwashi.github.io/blog/images/iwashilong_w.jpg ">

  <meta property="og:image" content="https://biwashi.github.io/blog/images/iwashilong_w.jpg " />

First of all, this is the default ogp image. Normally specified for sites other than blogs.

  {{ if eq true .Params.carduse }}
  {{"<!--Since it is a blog, display custom ones-->" |safeHTML}}
  <meta property="og:image" content="{{ .Site.BaseURL }}{{ .Params.ogimage }}"> 
  <meta name="twitter:image:src" content="{{ .Site.BaseURL }}{{ .Params.ogimage }}">
  {{ else }}
  {{"<!--Since it is Home, the default one is displayed-->" |safeHTML}}
  {{ end }}

Well, here is the if.

I specified carduse: xxx in the previous markdon. That is because there is a conditional branch here.

If carduse is true, it is judged that it is a blog, and the ogp created earlier is specified. The path at this time is also the ogimage specified earlier. Let's look at markdown again.

#Settings for Twitter card gen"]
author: ["Sardines"]
categories: ["Tech"]
tags: ["tcardgen", "Hugo", "OGP"] # tag
ogimage: "images/og/tcardgen-hugo.png " #Set the image generated by tcardgen as an OGP image
url: "/blog/tcardgen-hugo/" #Set the path for the auto-generated script in tcardgen It also means fixed routing
carduse: true #Whether to use Twitter Card If false, the default image will be applied

So, if carduse: false, do not specify it again as anything other than a blog.

Now you can set ogp for your blog and separate it from non-blogs.

At the end

I was doing this work at the same time as creating the blog, but I didn't write an article easily.

I hope you find it helpful.

reference

This time too, the beginning was Sanposhi-san.

-I made a quick self-made blog on Hugo-Sanposhi's walk

Recommended Posts

I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go
I tried to automatically generate a password with Python3
I tried to make a mechanism of exclusive control with Go
I tried to automatically generate a port management table from Config of L2SW
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to automatically create a report with Markov chain
A memorandum when I tried to get it automatically with selenium
I tried to create a list of prime numbers with python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to communicate with a remote server by Socket communication with Python.
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to automatically extract the movements of PES players with software
I tried to discriminate a 6-digit number with a number discrimination application made with python
[Outlook] I tried to automatically create a daily report email with Python
I tried to verify the result of A / B test by chi-square test
I tried to generate a random character string
I tried to make a simple mail sending application with tkinter of Python
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I made a tool to automatically back up the metadata of the Salesforce organization
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to read and save automatically with VOICEROID2 2
I made a vim learning game "PacVim" with Go
I tried to draw a route map with Python
I tried to automatically read and save with VOICEROID2
I tried to generate ObjectId (primary key) with pymongo
[Go + Gin] I tried to build a Docker environment
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to automate the article update of Livedoor blog with Python and selenium.
How to test the current time with Go (I made a very thin library)
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
I tried to implement a volume moving average with Quantx
I tried to find the entropy of the image with python
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I made a LINE BOT that returns parrots with Go
I tried to find the average of the sequence with TensorFlow
I made a package to filter time series with python
I want to make a blog editor with django admin
I made a class to get the analysis result by MeCab in ndarray with python
I want to automatically generate a modern metal band name
I made a command to generate a table comment in Django
I made a function to check the model of DCGAN
I tried to solve a combination optimization problem with Qiskit
I made a server with Python socket and ssl and tried to access it from a browser
I tried to get started with Hy ・ Define a class
I tried to classify MNIST by GNN (with PyTorch geometric)
I tried to implement ListNet of rank learning with Chainer
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
I made a tool to automatically generate a simple ER diagram from the CREATE TABLE statement
I tried to sort a random FizzBuzz column with bubble sort.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried a stochastic simulation of a bingo game with Python
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
I tried to divide with a deep learning language model
I tried using PI Fu to generate a 3D model of a person from one image
I tried to predict the sales of game software with VARISTA by referring to the article of Codexa
I tried to unlock the entrance 2 lock sesame with a single push of the AWS IoT button