Fryboyter

Hugo - Remove content from sitemap

Google Search Console has complained that two links on my site are blocked by robots.txt even though they appear in the sitemap. How do I get them from the sitemap that Hugo creates automatically?

The solution is actually quite simple. First edit the related articles and add sitemap_exclude: true to the metadata.

---
title: Secret Article
date: 2019-06-11T19:12:45+0200
sitemap_exclude: true
slug: secret-article
---

Now it is necessary to adapt the template with which the sitemap is created. Therefore create the file sitemap.xml in the theme directory under layouts/_default and fill it with the following content.

{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  {{ range .Data.Pages }}
  <url>
    <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
    <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
    <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
    <priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
  </url>
  {{ end }}
</urlset>

This corresponds initially to the standard template for the sitemap. Now change line 4 as follows.

{{ range .Data.Pages }}{{ if ne .Params.sitemap_exclude true }}

Finally add another {{ end }} to line 21.

{{ end }}{{ end }}

If the page is rebuilt now, all contents where sitemap_exclude: true is specified should no longer appear in the sitemap.

General | OSBN

Some articles are now offered in multiple languages

In the last few years I have been thinking about publishing at least some articles in English from time to time. So far, however, the technical implementation was too cumbersome for me. But with Hugo it’s done pretty easy, as I noticed today.

The first thing to do is to add the following entries to the configuration file config.toml.

DefaultContentLanguage = "de"

    [languages]
    [languages.de]
        languageName = "Deutsche Version"
        weight = 1
    [languages.en]
        languageName = "English version"
        weight = 2

The first line specifies that de is the default language. The rest defines the existing languages, gives them a name and a weighting (the lower the more important).

Now we create a new article. Let’s just call the file manche-artikel-nun-englisch.md. Let’s assume that the article is important enough to be published in English as well. So we create another article and name the file manche-artikel-nun-englisch.en.md. As you can see the title has been extended by a .en. Hugo now automatically understands that the article with .en is the English version and the other one is the German version.

But what if someone opens the German version but doesn’t understand German? There should be a kind of switch available. For this one can use for example the following code.

{{ if .IsTranslated }}
    {{ range .Translations }} | {{ .Language.LanguageName }} {{ end}}
{{ end }}

This checks whether the article is also available in another language. If so, a link to the corresponding versions will be shown. If not, nothing is displayed. I put this code on fryboyter.de under the main heading of all articles. If the link “English version” appears at an article, I wrote the article in English as well. Currently one will find this link only at this article for the moment. Gradually there will be more articles in German and English (also older ones) available.

OSBN | General

Update Hugo

Hugo is not installed on my webspace by default. Therefore I have to install new versions manually. To make this easier I built a small script.

#!/bin/bash
BIN_DIR=$HOME/bin
CUR_VERSION="$("$BIN_DIR"/hugo version 2>/dev/null | cut -d'v' -f2 | cut -c 1-6)"
NEW_VERSION=$(curl --silent "https://api.github.com/repos/gohugoio/hugo/tags" | jq -r '.[0].name' | tr -d v)

echo "Hugo:  Current Version: $CUR_VERSION => New Version: $NEW_VERSION"

if ! [ "$NEW_VERSION" = "$CUR_VERSION" ]; then

  curl -L --output hugo.tar.gz "https://github.com/gohugoio/hugo/releases/download/v${NEW_VERSION}/hugo_${NEW_VERSION}_Linux-64bit.tar.gz"
  tar -C "${BIN_DIR}" -xvzf hugo.tar.gz hugo
  rm hugo.tar.gz
else
    echo "Die current Version of Hugo ist allready installed"
fi

This checks whether the current version offered on Github is more current than the one available on the web space. If so, the current version of Github will be downloaded and installed on the webspace.

OSBN | General

Insert date in Sublime Text

If you create an article with Hugo you have to enter the date in the markdown file in the form of 2019-04-27T22:18:13+0200. In contrast to Jörg this annoys me quite a bit.

So I thought about how I could automate the input. For my preferred editor Sublime Text there is the plugin InsertDate available. After the installation you choose the correct time zone. After that, it’s best to create a shortcut to insert the date. To do this, select “Key Bindings” from the “Preferences” menu. In the right window enter the following code between the square brackets and save the file.

{ "keys": ["shift+f5"], "command": "insert_date", "args": {"format": "%Y-%m-%dT%H:%M:%S%z"} },

Instead of shift+f5 you can also use another key combination. From now on you can insert the current date including time and the difference to UTC in ISO 8601 format with the defined shortcut.

OSBN | General

Switched to Complete Feed

A few days ago I was asked if I could change the OSBN feed to show the complete article. The wish is my command.

To do this, I simply created the file rss.xml in the layouts/_default subdirectory of Hugo’s theme directory with the following content (this corresponds to the standard template of the feed).

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
    <link>{{ .Permalink }}</link>
    <description>Recent content {{ if ne  .Title  .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
    <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
    <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
    <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
    <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
    <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
    {{ with .OutputFormats.Get "RSS" }}
        {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    {{ end }}
    {{ range .Pages }}
    <item>
      <title>{{ .Title }}</title>
      <link>{{ .Permalink }}</link>
      <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
      {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | html }}</description>
    </item>
    {{ end }}
  </channel>
</rss>

I changed the line <description>{ .Summary | html }}</description> to <description>{ .Content | html }}</description> to display the complete article. That’s it. The change applies to the entire page, not just the OSBN category.

OSBN | General