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" >}}
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" >}}
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:
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.