SSG Hugo Setup

SSG Hugo Setup

Hugo Config

Print the site configuration, both default and custom settings.

~] hugo config
archetypedir = 'archetypes'
assetdir = 'static'
baseurl = 'http://www.pipelines.cz/'
cachedir = '/tmp/hugo_cache_rrastik/'
contentdir = 'content'
datadir = 'data'
defaultcontentlanguage = 'en'
...

List all parameters from .Params

{{with .Params }}{{printf "%#v " .}}{{end}}

List of all Chroma syntax highlighter lexers

Chroma is a general purpose syntax highlighter in pure Go programming language.

sources:
https://github.com/alecthomas/chroma
Chroma style gallery
Chroma on GitHub
List of all Chroma lexers

Supported languages

Prefix Language
A ABNF, ActionScript, ActionScript 3, Ada, Angular2, ANTLR, ApacheConf, APL, AppleScript, Awk
B Ballerina, Base Makefile, Bash, Batchfile, BlitzBasic, BNF, Brainfuck
C C, C#, C++, Cassandra CQL, CFEngine3, cfstatement/ColdFusion, CMake, COBOL, CSS, Cap'n Proto, Ceylon, ChaiScript, Cheetah, Clojure, CoffeeScript, Common Lisp, Coq, Crystal, Cython
D Dart, Diff, Django/Jinja, Docker, DTD
E EBNF, Elixir, Elm, EmacsLisp, Erlang
F Factor, Fish, Forth, Fortran, FSharp
G GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, Groovy
H Handlebars, Haskell, Haxe, Hexdump, HTML, HTTP, Hy
I Idris, INI, Io
J Java, JavaScript, JSON, Jsx, Julia, Jungle
K Kotlin
L Lighttpd configuration file, LLVM, Lua
M Mako, Markdown, Mason, Mathematica, MiniZinc, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL
N NASM, Newspeak, Nginx configuration file, Nim, Nix
O Objective-C, OCaml, Octave, OpenSCAD, Org Mode
P PacmanConf, Perl, PHP, Pig, PkgConfig, Plaintext, PL/pgSQL, PostgreSQL SQL dialect, PostScript, POVRay, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3
Q QBasic
R R, Racket, Ragel, reg, reStructuredText, Rexx, Ruby, Rust
S Sass, Scala, Scheme, Scilab, SCSS, Smalltalk, Smarty, Snobol, Solidity, SPARQL, SQL, SquidConf, Swift, systemd, Systemverilog
T TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, TeX, Thrift, TOML, TradingView, Transact-SQL, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData
V verilog, VHDL, VimL
W WDTE
X XML, Xorg
Y YAML

Aliases

For people migrating existing published content to Hugo static site generator, there’s a good chance you need a mechanism to handle redirecting old URLs. Luckily, redirects can be handled easily with aliases in Hugo.

Let’s assume you create a new piece of content at content/posts/my-new-blog-post.md with url www.yourwebname.com/posts/my-new-blog-post/. The content is a revision of your previous post at content/posts/my-old-blog-post.md with url www.yourwebname.com/posts/my-old-blog-post/. You can create an aliases field in the front matter of your new my-new-blog-post.md where you can add previous url paths. The following examples show how to create this filed in YAML front matter.

YAML Front Matter of our content/posts/my-new-blog-post.md

---
title: My New post
aliases: ["/posts/my-old-blog-post/", "/post/another-my-old-post-url/"]
---

How Hugo Aliases Work

When aliases are specified, Hugo creates a directory to match the alias entry. Inside the directory, Hugo creates an .html file specifying the canonical URL for the page and the new redirect target.

For example, a content file at posts/my-new-blog-post.md with the following in the front matter:

---
title: My New post
aliases: ["/posts/my-old-blog-post/"]
---

Assuming a baseURL of example.com, the contents of the auto-generated alias .html found at https://example.com/posts/my-old-blog-post/ will contain the following:

<!DOCTYPE html>
<html>
  <head>
    <title>https://example.com/posts/my-new-blog-post/</title>
    <link rel="canonical" href="https://example.com/posts/my-new-blog-post/"/>
    <meta name="robots" content="noindex">
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta http-equiv="refresh" content="0; url=https://example.com/posts/my-new-blog-post/"/>
  </head>
</html>

The http-equiv="refresh" line is what performs the redirect, in 0 seconds in this case. If an end user of your website goes to https://example.com/posts/my-old-blog-post/, they will now be automatically redirected to the newer, correct URL. The addition of <meta name="robots" content="noindex"> lets search engine bots know they they should not crawl and index your new alias page.

Comments

In order to keep your templates organized and share information throughout your team, you may want to add comments to your templates. There are two ways to do that with Hugo.

Go Templates comments

Go Templates support {{/* and */}} to open and close a comment block. Nothing within that block will be rendered.

For example:

Bonsoir, {{/* {{ add 0 + 2 }} */}}Eliott.

Will render Bonsoir, Eliott., and not care about the syntax error (add 0 + 2) in the comment block.

HTML comments

You can add html comments by piping a string HTML code comment to safeHTML.

{{ "<!-- This is an HTML comment -->" | safeHTML }}

If you need variables to construct such HTML comments, just pipe printf to safeHTML.

{{ printf "<!-- Our website is named: %s -->" .Site.Title | safeHTML }}

HTML comments containing Go Templates

HTML comments are by default stripped, but their content is still evaluated. That means that although the HTML comment will never render any content to the final HTML pages, code contained within the comment may fail the build process.

<!-- {{ $author := "Emma Goldman" }} was a great woman. -->
{{ $author }}

The templating engine will strip the content within the HTML comment, but will first evaluate any Go Template code if present within. So the above example will render Emma Goldman, as the $author variable got evaluated in the HTML comment. But the build would have failed if that code in the HTML comment had an error.

source: gohugo.io

Whitespace

Go 1.6 includes the ability to trim the whitespace from either side of a Go tag by including a hyphen (-) and space immediately beside the corresponding {{ or }} delimiter.

For instance, the following Go Template will include the newlines and horizontal tab in its HTML output:

<div>
  {{ .Title }}
</div>

Which will output:

<div>
  Hello, World!
</div>

Leveraging the - in the following example will remove the extra white space surrounding the .Title variable and remove the newline:

<div>
  {{- .Title -}}
</div>

Which then outputs:

<div>Hello, World!</div>

Go considers the following characters whitespace:

  • space
  • horizontal tab
  • carriage return
  • newline

source: gohugo.io

Check for .isPost?

How do i check if a post is a post and not a page?

{{if eq .Section "post"}} does the trick.

{{ if eq .Section "post" }}
YOUR STUFF HERE IF
{{ else }}
YOUR STUFF HERE IF ELSE
{{ end }}

source: discourse.gohugo.io

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus