Skip to content

Instantly share code, notes, and snippets.

@kenold
Last active February 11, 2017 00:46
Show Gist options
  • Save kenold/9fca7b6d7243f68ff75dabb9b9df68ce to your computer and use it in GitHub Desktop.
Save kenold/9fca7b6d7243f68ff75dabb9b9df68ce to your computer and use it in GitHub Desktop.
Generate YAML Front Matter from Pocket JSON API in Hugo Static Site Generator
<!-- created by Kenold Beauplan // Twitter @kenoldb -->
{{ $url := "/data/tutorials.json" }}
{{ $data := getJSON $url}}
<!-- "list-unstyled" is a Bootstrap calss that removes the bullet points -->
<ul class="list-unstyled">
{{ range $data.list }}
{{ $item := . }}
<li>---</li>
<li>title:
<!-- use resolve_title if it's not null -->
{{ with $item.resolved_title }}
<!-- replace colon with dash or Hugo will give error because YAML front matter uses colons -->
{{ $item.resolved_title | replaceRE ":" " -" }}
{{else}}
<!-- otherwise, use given_title -->
{{ $item.given_title }}
{{ end }}</li>
<li>source_url: <a href="{{ $item.given_url }}" target="_blank">{{ $item.given_url }}</a></li>
<li>time_added: {{ $item.time_added }}</li>
<li>time_updated: {{ $item.time_updated }}</li>
<li>excerpt: {{ $item.excerpt | replaceRE ":" " -" }}</li>
<li>tags:<br>
{{range $item.tags }}
&nbsp;&nbsp;- {{ .tag }}<br>
{{ end }}</li>
<li>authors: <br>
{{range $item.authors }}
&nbsp;&nbsp;- {{ .name }}<br>
{{ end }}
</li>
<li>---</li>
{{ end }}
</ul>
// This is JSON results from Pocket API - https://getpocket.com/developer/docs/v3/retrieve
{
"status": 1,
"complete": 1,
"list": {
"685346565": {
"item_id": "685346565",
"resolved_id": "685346565",
"given_url": "https://npf.io/2014/08/hugo-beyond-the-defaults/",
"given_title": "Hugo: Beyond the Default",
"favorite": "0",
"status": "0",
"time_added": "1486656011",
"time_updated": "1486656039",
"time_read": "0",
"time_favorited": "0",
"sort_id": 5,
"resolved_title": "",
"resolved_url": "https://npf.io/2014/08/hugo-beyond-the-defaults/",
"excerpt": "",
"is_article": "0",
"is_index": "0",
"has_video": "0",
"has_image": "0",
"word_count": "0",
"tags": {
"hugo aliases": {
"item_id": "685346565",
"tag": "hugo aliases"
},
"hugo permalinks": {
"item_id": "685346565",
"tag": "hugo permalinks"
},
"hugo tutorial": {
"item_id": "685346565",
"tag": "hugo tutorial"
}
},
"authors": {
"17456480": {
"item_id": "685346565",
"author_id": "17456480",
"name": "Nate Finch",
"url": ""
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment