Skip to content

Instantly share code, notes, and snippets.

@noelbundick
Created January 13, 2019 05:13
Show Gist options
  • Save noelbundick/0f3da3d92b024a6d87617dcfc8fcdc49 to your computer and use it in GitHub Desktop.
Save noelbundick/0f3da3d92b024a6d87617dcfc8fcdc49 to your computer and use it in GitHub Desktop.
Gists as a content management system

GitHub Gists as a content management system

I often use GitHub Gists to jot down quick snippets to share. Lately, I've also tried to make sure an add a proper README (and a LICENSE!) - partially so that readers have some context on what they're looking at, but also so I can remember what the heck I was doing when I wrote whatever code I've slung out there.

Gists are horrible for discoverability, and I always forget the neat things I've hacked together. I wanted a section on my website with at minimum links, but hopefully the actual content from some of my "better" gists - with better defined as:

  • is a gist that I've starred
  • is one of my own gists
  • has a README.md
  • is newer than 2017-01-01

I spent a little bit of time today hacking this together with bash, curl, and jq! I think it qualifies as just hacky enough to use as an example to validate this insane workflow :)

How it works

All the work happens in pull-gists.sh. A brief walkthrough:

  • Hit the GitHub API to look for my starred gists
  • (ab)use jq to filter down to gists where I'm the owner, it's newer than a given date, and has a README.md
  • Iterate over the gists & extract a bunch of properties
  • Write out some frontmatter (I'm using Hugo for my blog) to a file per gist / post
  • Download the README as the post content
  • A Hugo template adds embed script tags for each interesting file. (The README has already been added, and the LICENSE is just spam on a blog post)

On the backend, I'm currently hosted on Azure Storage + an Azure Function proxy as my frontend. The Hugo site gets built in Azure DevOps per-commit to master for regular posts. And I now have a daily build trigger to pick up new starred gists.

Putting it all together, this hacked together mess means I have a way to draft blog posts on https://gist.github.com/, then star them when I'm ready for them to go out. I'm pretty happy with this - I hope it sparks some interesting ideas for you as well!

{{ define "main" }}
<article class="post">
<header>
<h1>
<a href="{{ .Params.gist_url }}">{{ .Title }}</a>
</h1>
<h2 class="subtitle">{{ .Description | markdownify }}</h2>
<h2 class="subtitle">
<a href="{{ .Params.gist_url }}">view gist on github</a>
</h2>
<h2 class="headline">
{{ .Date.Format "January 2, 2006" }}
<br>
{{ with .Params.tags }}
{{ if ge (len .) 1 }}
{{ range . }}
<a href="{{ printf "tags/%s" (. | urlize) | absURL }}">{{ . }}</a>
{{ end }}
{{ end }}
{{ end}}
</h2>
</header>
<section id="post-body">
{{ .Content }}
{{ range .Params.gist_embed_files }}
<h3>{{ . }}</h3>
<script src="{{ $.Params.gist_url }}.js?file={{ . }}"></script>
{{ end }}
</section>
{{ end }}
MIT License
Copyright (c) 2019 Noel Bundick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/bash
USER=$1
PAT=$2
if [[ -z $USER || -z $PAT ]]; then
echo "Usage: pull-gists.sh <githubUsername> <githubPAT>"
exit 1
fi
GISTS=$(curl -s -u $USER:$PAT \-H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/gists/starred' | jq -rc --arg login $USER '.[] | select(.owner.login == $login and (.created_at | fromdateiso8601) > ("2017-01-01T00:00:00Z" | fromdateiso8601) and .files["README.md"].size > 0) | @base64')
for gist in $GISTS; do
_jq() {
echo $gist | base64 -d | jq "$@"
}
ID=$(_jq -r '.id')
NAME=$(_jq -r '.description')
HTML_URL=$(_jq -r '.html_url')
RAW_URL=$(_jq -r '.files["README.md"].raw_url')
POST_DATE=$(_jq -r '.created_at | fromdate | strftime("%Y-%m-%d")')
FILES=$(_jq -c '[ .files[] | select((.filename | contains("README.md") | not) and (.filename | contains("LICENSE") | not)) | .filename ]')
FILENAME=$(echo $NAME | sed 's/[^[:alnum:]]/-/g')
POST="site/content/gists/$FILENAME.md"
cat <<EOF > $POST
---
title: $NAME
tags:
- gist
date: $POST_DATE
gist_url: $HTML_URL
gist_embed_files: $FILES
---
EOF
echo "Creating post for [$POST_DATE] $NAME ($HTML_URL)"
curl -s $RAW_URL >> $POST
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment