hugo/tpl/transform
Bjørn Erik Pedersen 04f21b4d80 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 }}
```
2025-02-25 19:35:47 +01:00
..
testdata Improve Katex error handling and fix handling of large expressions 2024-08-12 13:50:18 +02:00
init.go tpl/transform: Add transform.XMLEscape template function 2023-11-27 10:52:33 +01:00
remarshal.go all: Run gofumpt -l -w . 2024-01-28 23:14:09 +01:00
remarshal_test.go deps: Update github.com/pelletier/go-toml/v2 v2.0.2 => v2.0.4 2022-08-26 18:30:46 +02:00
transform.go Add transform.PortableText 2025-02-25 19:35:47 +01:00
transform_integration_test.go Add transform.PortableText 2025-02-25 19:35:47 +01:00
transform_test.go tpl/transform: Make Plainify and ToMath return template.HTML 2024-08-11 15:16:16 +02:00
unmarshal.go tpl/transform: Revert unmarshal whitespace removal 2024-10-22 09:42:40 +02:00
unmarshal_test.go tpl/transform: Don't fail on "no data to transform" 2024-10-18 10:30:36 +02:00