Matthew's Dev Blog

Injecting GitLab Snippets into Publish output

I've been using John Sundell's static site generator Publish for a while, to create this website. All the content is written in Markdown, and I use Publish to generate a set of HTML.

The HTML is then uploaded to GitHub Pages. (Hmm, yeah, I must change that!)

It's called a "static site" because there's nothing dynamic here: no database, no javascript, no cleverness. The whole site is regenerated whenever I make a change.

Adding code to a post

Naturally, I want to include code in many blogposts. The traditional way would be using Markdown code blocks, delimited with three backticks, but sometimes it'd be great to have that code under source-control somewhere.

traditional code block defined using backticks

Thomas Lupo wrote GistPublishPlugin which includes GitHub Gists in Publish posts, so it's natural that I'd want the same for GitLab Snippets too.

About the plugin

This new GitLabSnippetsPublishPlugin does just that: includes the contents of GitLab Snippets in Publish pages and articles.

Basic usage

The project readme contains installation instructions, and detailed usage. But the basic usage is to start a blockquote with the word snippet and the unique Snippet ID:

> snippet 12345

If the Snippet contains multiple files, then you must also specify the filename. For example, this snippet contains two files (file.swift and file.txt). We can include file.swift like this:

> snippet 2226922 file.swift

which renders like this:

print("This is code in a snippet")

Project Snippets

GitLab supports Project Snippets - Snippets which belong to a project, but are not part of the project's main git repository. To use a project Snippet like this one, use the > projectsnippet prefix, followed by the project path, then the Snippet ID, and finally the optional filename:

> projectsnippet mflint/gitlabsnippetspublishplugin 2226923 file.swift

which renders like this:

print("This is code in a project snippet")
Tagged with:

First published 23 December 2021