Markdown

Your posts can be formatted using markdown syntax. It’s critical to know how it works. It’s super simple.

Frontmatter

  • You can add tags: [swift, json, network call] and it will then add the tags to your post.
  • Add showToc: true and will show a table of contents for your post.
  • Hugo automatically takes the first 70 words of your content as its summary and stores it into the .Summary variable
  • The date of the field affects the order of the post. So if you drafted the markdown of your file 2yrs ago, but published it today, then make sure you adjust the date. Otherwise it will be buried under the newer posts.
    • Instead, you can manually define where the summary ends with a “!–more–” (Notes: must be wrapped inside angle brackets) divider.
    • Alternatively, you can add a summary to the front matter if you don’t want your summary to be the beginning of your post
  • Customize Description. The value of this field is used as an abstract in your front page and link previews.
  • Add two spaces to create new line after a line end. Otherwise hugo will just continue the line. This is something hard to grasp. Because of this I usually manually review my rendered post with hugo server -D

Syntax Formatting

  • Highlighting individual code lines. Example:
class Foo {
    var name = "Jack"
}

or I can add line numbers:

1
2
3
4
rvm use ruby-2.5.1 || rvm install ruby-2.5.1
gem install bundler -v "2.3.3" 
bundle install
pod lib lint

Syntax Highlighting

Not sure if this applies to all themes, but for the PaperMod theme I used, the instructions were as such:

  1. Find your own custom css from here
  2. Add its css link into you extend_head.html, so it’s passed down into head of every page.

You can find Hugo docs on configuring your own highlighter. But I wasn’t able to make it work…

Resource management

  • With images, overtime your static folder will turn into a big graveyard. So it’s best that you re-structure your posts. Example:
   - Bad way:
        - Posts
            - post1
            - post2
        - static
            - imageA
            - imageB
    - Good way:
      - Posts
            - post1
                - index.md
                - images
                    - imageA
            - post2
                - index.md
                - images
                    - imageB
  • context-aware is the more technical term that implies that .Title has a different meaning for each post or at the global level vs post level. At the post level, .Title is the title of the post. At the global level, .Title is the title of the website. See here for more.
  • Have expandable section. You can default it to open. See here. Example:
<details>
  <summary>See answer</summary>
  - in-order traversal for all will be: `[1,2,3,4]`
  - level-order would be different for each. 
</details>

Making changes to your theme

I used to think that if I wanted to change a shortcode or template, I had to fork the entire theme.

That’s exactly what I did with PaperMod:

  1. I forked PaperMod.
  2. I made my shortcode, layout, and CSS changes inside the fork.
  3. I committed and pushed those changes to the theme repo.
  4. I went back to my blog repo and committed the new submodule SHA.

It worked. But now I had two repos to maintain for one website.

Updating PaperMod meant merging its latest changes with my fork. Even a small shortcode fix required a commit in the theme repo, followed by another commit in the blog repo. And if I wanted to try a different theme, first I had to figure out which pieces of my fork were actually my customizations.

What am I doing now?

Hugo has a much better override system. It looks inside the website’s own layouts directory first. If it doesn’t find a matching template there, then it falls back to the theme’s layouts directory.

Example:

themes/paperMod/layouts/_shortcodes/audio.html
layouts/_shortcodes/audio.html

If both files exist, Hugo uses the one from my blog’s layouts directory.

So now I use the official PaperMod repo without changing anything inside it. My customizations live in my main blog repo:

layouts/_shortcodes/audio.html
layouts/_shortcodes/details.html
layouts/_shortcodes/mytable.html
layouts/_markup/render-image.html
layouts/_partials/extend_post_content.html
layouts/list.html
assets/css/extended/custom.css

The shortcodes are mine. The image caption behavior is mine. The extra text at the end of a post is mine. PaperMod remains PaperMod.

Why is this sooooo much better?

  • I can update PaperMod without merging its changes into my personal fork.
  • A shortcode change only needs one commit in my blog repo.
  • I can delete an override and immediately fall back to the theme’s original behavior.
  • Moving to another theme is easier because my reusable shortcodes and image rendering aren’t buried inside PaperMod.
  • It’s obvious which code belongs to my website and which code belongs to the theme.

💡 Your website’s layouts directory can override theme templates without changing the theme itself.

The one caveat is that some overrides are still theme-specific. My shortcodes are fairly portable, but layouts/list.html and some CSS use PaperMod’s structure and class names. A full template override also means I won’t automatically get future changes to that template from PaperMod. After a major theme update, I should compare my override with the latest theme version.

Pro Tip: If you ever wanted to see how your post looks like on an iPhone then on Safari » Develop » Open Page with » iPhone 15 Pro (or whatever device you’d like to pick):

alt text
To quickly inspect the layout on a simulated iPhone from your macOS

Shortcodes

It’s a way for you to do custom HTML within markdown. Think of writing the html customization in shortcode. Then naming that customization. Then invoking it in markdown. Example see here:

Usage of shortcode

“shortcode usage”
I purposefully used a screenshot otherwise Hugo would have rendered the shortcode and not shown the syntax.

Shortcode definition

<audio controls>
  <source src="/{{.Get "audio-name"}}.{{.Get "audio-type"}}" type="audio/{{.Get "audio-type"}}"> Your browser does
  not support the audio element.
</audio>

Shortcode visualization

“shortcode visualization”
Fidelity Account List. The list remains there for some screens.