Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transform.PortableText #13432

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

bep
Copy link
Member

@bep bep commented Feb 21, 2025

Using this in a content adapter could look like this:

{{ $projectID := "<myprojectid>" }}
{{ $url := printf "https://%s.api.sanity.io/v2021-06-07/data/query/production?query=*[0]"  $projectID }}
{{ $cacheKey := print $url (now.Format "2006-01-02T15:04") }}
{{ $opts := dict "key" $cacheKey }}
{{ $r := resources.GetRemote $url $opts }}
{{ $m := $r | transform.Unmarshal }}
{{ $result := $m.result }}
{{ $markdown := transform.PortableText $result.body }}
{{ $content := dict
  "mediaType" "text/markdown"
  "value" $markdown
}}
{{ $page := dict
  "content" $content
  "kind" "page"
  "path" $result.slug.current
  "title" $result.title
}}
{{ .AddPage $page }}

Note that this is still very rough around the edges, but I was pleasantly surprised that I could get something working right out of the gate.

Converting PortableText to Markdown and then back to HTML may seem sub optimal, but it fits right into Hugo's render hook etc. setup.

@bep bep force-pushed the feat/portabletext branch from 745c840 to 181bc64 Compare February 21, 2025 15:45
Using this in a content adapter could look like this:

```handlebars
{{ $projectID := "<myprojectid>" }}
{{ $url := printf "https://%s.api.sanity.io/v2021-06-07/data/query/production?query=*[0]"  $projectID }}
{{ $cacheKey := print $url (now.Format "2006-01-02T15:04") }}
{{ $opts := dict "key" $cacheKey }}
{{ $r := resources.GetRemote $url $opts }}
{{ $m := $r | transform.Unmarshal }}
{{ $result := $m.result }}
{{ $markdown := transform.PortableText $result.body }}
{{ $content := dict
  "mediaType" "text/markdown"
  "value" $markdown
}}
{{ $page := dict
  "content" $content
  "kind" "page"
  "path" $result.slug.current
  "title" $result.title
}}
{{ .AddPage $page }}
```

Note that this is still very rough around the edges, but I was pleasantly surprised that I could get something working right out of the gate.

Converting PortableText to Markdown and then back to HTML may seem sub optimal, but it fits right into Hugo's render hook etc. setup.
@bep bep force-pushed the feat/portabletext branch from 181bc64 to 2ad0a3a Compare February 22, 2025 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant