Got Hugo working with Directus Headless CMS

{{ $authUrl         := os.Getenv "HUGO_API_AUTH_URL" }}
{{ $gqlUrl          := os.Getenv "HUGO_GQL_API_URL"}}
{{ $gqlUser         := os.Getenv "HUGO_API_GQL_USER"}}
{{ $gqlPass         := os.Getenv "HUGO_API_GQL_PASSWORD"}}
{{ $assetsUrl       := os.Getenv "HUGO_API_ASSETS_URL"}}
{{ $token           := "" }}

{{/* Auth payload */}}
{{ $authPayload := (dict 
    "method" "POST"
    "body" ( dict 
        "email" $gqlUser
        "password" $gqlPass
        | jsonify 
    )
    "headers" (dict 
        "Content-Type" "application/json"
    )
)
}}

{{/* Get authToken */}}
{{ with resources.GetRemote $authUrl $authPayload }}
  {{ with .Err }}
    {{ warnf "%s" . }}
  {{ else }}
    {{ $resp := . | transform.Unmarshal}}
    {{ $token = $resp.data.access_token }}
  {{ end }}
{{ end }}

{{/* Posts payload */}}
{{ $bearer := printf "Bearer %s" $token }}
{{ $postsPayload := ( dict
    "method" "POST"
    "body" ( dict
        "query" `
            {
                posts {
                    title
                    user_created {
                        email
                        first_name
                        last_name
                    }
                    date_created
                    date_updated
                    draft
                    tags
                    featured_image {
                        id
                        filename_download
                    }
                    content
                }
            }
        ` | jsonify
    )
    "headers" (dict 
        "Content-Type" "application/json"
        "X-REQUEST-TYPE" "GraphQL"
        "Authorization" $bearer
    )
) }}

{{/* Get posts */}}
{{ with resources.GetRemote $gqlUrl $postsPayload }}
    {{ with .Err }}
        {{ warnf " %s" . }}
    {{ else }}
        {{ $rawData     := . | transform.Unmarshal }}
        {{ $posts       := $rawData.data.posts }}
        
        {{ range $post := $posts }}
            {{ $pTitle      := printf "\"%s\"" $post.title }}
            {{ $pAuthor     := printf "\"%s %s\"" $post.user_created.first_name $post.user_created.last_name }}
            {{ $pCreated    := $post.date_created }}
            {{ $pUpdated    := $post.date_updated }}
            {{ $pDraft      := $post.draft }}

            {{ $sl := slice }}
            {{ range $post.tags }}
                {{ $tag := printf "\"%s\"," . }}
                {{ $sl = $sl | append $tag }}
            {{ end }}

            {{ $pTags       := $sl }}

            {{ $pContent    := $post.content }}

            {{ $templateString := printf "+++ \ntitle = %s\nauthor = %s\ndate = %s\nupdated = %s\ndraft = %t\ntags = %s\n+++ \n\n%s" $pTitle $pAuthor $pCreated $pUpdated $pDraft $pTags $pContent }}
            {{ $unixTS := ($pCreated | time.AsTime).Unix }}
            {{ $mdFile := printf "posts/%d-%s/index.md" $unixTS (urlize $pTitle) }}
            {{ $resource := resources.FromString $mdFile $templateString }}
            {{ $file := $resource.RelPermalink }}

            {{/*  Download featured image  */}}
            {{ $imgId           := $post.featured_image.id }}
            {{ $imgFileName     := $post.featured_image.filename_download }}
            {{ $temp            := (split $imgFileName ".") }}
            {{ $newImgFileName  := printf "%s-feat.%s" ( $imgId ) ( index $temp 1) }}
            {{ $imgUrl          := printf "%s/%s" $assetsUrl $imgId }}
            {{ $assetPayload    := ( dict
                    "method" "GET"
                    "headers" ( dict
                        "Authorization" $bearer
                    )
                )
            }}

            {{ with resources.GetRemote $imgUrl $assetPayload }}
                {{ with .Err }}
                    {{ warnf "%s" . }}
                {{ else }}
                    {{ $imgFile := printf "posts/%d-%s/images/%s" $unixTS (urlize $pTitle) $newImgFileName }}
                    {{ $imgResource := resources.FromString $imgFile .Content }}
                    {{ $imgFilePerm := $imgResource.RelPermalink }}
                {{ end }}
            {{ end }}

        {{ end }}

        
    {{ end }}
{{ end }}

Thanks @regis for the article, it helped me a lot

Thanks @jhvanderschee for pointing me to the Hugo conference https://hugoconf.io/

4 Likes

Very nice! Glad to see such an interesting use case shared here!

2 Likes

@amysheldon We have been loving Directus. How has your experience been? I am grateful for this snippet. I intend to try it out in the coming days. Would love to hear your insights about Directus+Hugo since you posted this. We are considering it for adoption at quite a meaningful scale.

Any result now? love to hear your experience on Directus +Hugo :slight_smile: