diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2025-02-21 16:22:01 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2025-02-25 19:35:47 +0100 |
commit | 04f21b4d806d0b65793183de62aa1cd56688f35d (patch) | |
tree | 75f9dae4f301dc451af693e5f1b3462bea7c1f9f /go.mod | |
parent | ab9e5457603b954e91c6b1a701d8153b62bd1144 (diff) | |
download | hugo-04f21b4d806d0b65793183de62aa1cd56688f35d.tar.gz hugo-04f21b4d806d0b65793183de62aa1cd56688f35d.zip |
Add transform.PortableText
Using this in a content adapter could look like this:
```handlebars
{{ $projectID := "<myproject>" }}
{{ $useCached := true }}
{{ $api := "api" }}
{{ if $useCached }}
{{/* See https://www.sanity.io/docs/api-cdn */}}
{{ $api = "apicdn" }}
{{ end }}
{{ $url := printf "https://%s.%s.sanity.io/v2021-06-07/data/query/production" $projectID $api }}
{{/* prettier-ignore-start */ -}}
{{ $q := `*[_type == 'post']{
title, publishedAt, summary, slug, body[]{
...,
_type == "image" => {
...,
asset->{
_id,
path,
url,
altText,
title,
description,
metadata {
dimensions {
aspectRatio,
width,
height
}
}
}
}
},
}`
}}
{{/* prettier-ignore-end */ -}}
{{ $body := dict "query" $q | jsonify }}
{{ $opts := dict "method" "post" "body" $body }}
{{ $t := debug.Timer "sanity.get" }}
{{ $r := resources.GetRemote $url $opts }}
{{ $t.Stop }}
{{ $m := $r | transform.Unmarshal }}
{{ $result := $m.result }}
{{ $t := debug.Timer "sanity.parse" }}
{{ range $result }}
{{ if not .slug }}
{{ continue }}
{{ end }}
{{ $markdown := transform.PortableText .body }}
{{ $t.Stop }}
{{ $content := dict
"mediaType" "text/markdown"
"value" $markdown
}}
{{ $params := dict
"portabletext" (.body | jsonify (dict "indent" " "))
}}
{{ $page := dict
"content" $content
"kind" "page"
"path" .slug.current
"title" .title
"date" (.publishedAt | time )
"summary" .summary
"params" $params
}}
{{ $.AddPage $page }}
{{ end }}
```
Diffstat (limited to 'go.mod')
-rw-r--r-- | go.mod | 1 |
1 files changed, 1 insertions, 0 deletions
@@ -12,6 +12,7 @@ require ( github.com/bep/goat v0.5.0 github.com/bep/godartsass/v2 v2.3.2 github.com/bep/golibsass v1.2.0 + github.com/bep/goportabletext v0.1.0 github.com/bep/gowebp v0.3.0 github.com/bep/helpers v0.5.0 github.com/bep/imagemeta v0.8.4 |