/var/log

A blog about UNIX and programming.

ho.stna.me : ~ ❯ ls

blog foo.txt windowFrame.html windowFrame.refactored.html windowFrame.refactored2.html

ho.stna.me : ~ ❯ cd blog

ho.stna.me : ~/blog ❯ cat mkblog-source.md

mkblog Can Now Embed Source Files

mkblog already knew how to embed local terminal recordings. Now it can do the same sort of thing for source files.

The new source shortcode reads from content/snippets/, applies syntax highlighting at build time, and lets a post pull in either a whole file or a specific line range. That keeps examples local to the repo instead of pasting stale copies into Markdown by hand.

The storage convention is simple: put a file under content/snippets/ and reference it with a /snippets/... path.

Basic usage looks like this:

{{< source "/snippets/mkblog-source-demo.go" >}}

And a ranged embed looks like this:

{{< source "/snippets/mkblog-source-demo.go" lines="5-9" >}}

There are a few optional knobs as well:

{{< source "/snippets/mkblog-source-demo.go" lines="5-11" title="summarize helper" hl="7-9" >}}
{{< source "/snippets/mkblog-source-demo.go" lang=go >}}
{{< source "/snippets/mkblog-source-demo.go" lines="8" >}}

The examples above are written with a space between the first two braces so they render as literal text in this post. Remove that space in a real post.

Here is the whole demo file embedded, with highlighting :

{{< source "/snippets/mkblog-source-demo.go" hl="7-9" >}}
/snippets/mkblog-source-demo.go
 1package demo
 2
 3import "strings"
 4
 5// summarize joins the provided labels into a compact line.
 6func summarize(labels []string) string {
 7	if len(labels) == 0 {
 8		return "none"
 9	}
10	return strings.Join(labels, ", ")
11}

And here is the function body as a range, with highlighting:

{{< source "/snippets/mkblog-source-demo.go" lines="5-11" title="summarize helper" hl="7-9" >}}
summarize helper
 5// summarize joins the provided labels into a compact line.
 6func summarize(labels []string) string {
 7	if len(labels) == 0 {
 8		return "none"
 9	}
10	return strings.Join(labels, ", ")
11}

Single-line embeds work too, which is useful when a post wants to point at one exact return path instead of a full block:

single line example
10	return strings.Join(labels, ", ")

That gives mkblog a second local-first embed path: casts for terminal sessions, snippets for source. Both keep the interesting material in the site tree, where it can be versioned, reviewed, and updated without chasing copies across old posts.

EOF

ho.stna.me : ~/blog ❯ ls -lta

drwxr-xr-x 4 user staff 13 16:23 .
drwxr-xr-x 3 user staff 5 12:01 ..
drwxr-xr-x 3 user staff 3 16:23 archives
drwxr-xr-x 10 user staff 10 16:23 tags
-rw-r--r-- 1 user staff 5967 16:23 collaboration.md
-rw-r--r-- 1 user staff 7349 16:23 fossil-agent-dev.md
-rw-r--r-- 1 user staff 9680 16:23 codex-freebsd.md
-rw-r--r-- 1 user staff 6873 16:23 immich-migrate.md
-rw-r--r-- 1 user staff 2139 16:23 mkblog-source.md
-rw-r--r-- 1 user staff 4195 16:23 mkblog-intro.md
-rw-r--r-- 1 user staff 3985 01:04 authoring-posts.md
-rw-r--r-- 1 user staff 328 00:24 terminal-demo.md
-rw-r--r-- 1 user staff 118 16:23 hello-world.md

ho.stna.me : ~/blog ❯