| 1 |
--- |
| 2 |
title: transform.PortableText |
| 3 |
description: Converts Portable Text to Markdown. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [transform.PortableText MAP] |
| 10 |
--- |
| 11 |
|
| 12 |
{{< new-in 0.145.0 />}} |
| 13 |
|
| 14 |
[Portable Text][] is a JSON structure that represents rich text content in the [Sanity][] CMS. In Hugo, this function is typically used in a [content adapter][] that creates pages from Sanity data. |
| 15 |
|
| 16 |
## Types supported |
| 17 |
|
| 18 |
- `block` and `span` |
| 19 |
- `image`. Note that the image handling is currently basic; we link to the `asset.url` using `asset.altText` as the image alt text and `asset.title` as the title. For more fine-grained control you may want to process the images in an [image render hook][]. |
| 20 |
- `code` (see the [code-input][] plugin). Code will be rendered as fenced code blocks with any file name provided passed as a Markdown attribute. |
| 21 |
|
| 22 |
> [!NOTE] |
| 23 |
> Since the Portable Text gets converted to Markdown before it gets passed to Hugo, rendering of links, headings, images and code blocks can be controlled with [render hooks][]. |
| 24 |
|
| 25 |
## Example |
| 26 |
|
| 27 |
### Content Adapter |
| 28 |
|
| 29 |
```go-html-template {file="content/_content.gotmpl" copy=true} |
| 30 |
{{ $projectID := "mysanityprojectid" }} |
| 31 |
{{ $useCached := true }} |
| 32 |
{{ $api := "api" }} |
| 33 |
{{ if $useCached }} |
| 34 |
{{/* See https://www.sanity.io/docs/api-cdn */}} |
| 35 |
{{ $api = "apicdn" }} |
| 36 |
{{ end }} |
| 37 |
{{ $url := printf "https://%s.%s.sanity.io/v2021-06-07/data/query/production" $projectID $api }} |
| 38 |
|
| 39 |
{{ $q := `*[_type == 'post']{ |
| 40 |
title, publishedAt, summary, slug, body[]{ |
| 41 |
..., |
| 42 |
_type == "image" => { |
| 43 |
..., |
| 44 |
asset->{ |
| 45 |
_id, |
| 46 |
path, |
| 47 |
url, |
| 48 |
altText, |
| 49 |
title, |
| 50 |
description, |
| 51 |
metadata { |
| 52 |
dimensions { |
| 53 |
aspectRatio, |
| 54 |
width, |
| 55 |
height |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
}, |
| 61 |
}` |
| 62 |
}} |
| 63 |
{{ $body := dict "query" $q | jsonify }} |
| 64 |
{{ $opts := dict "method" "post" "body" $body }} |
| 65 |
{{ $r := resources.GetRemote $url $opts }} |
| 66 |
{{ $m := $r | transform.Unmarshal }} |
| 67 |
{{ $result := $m.result }} |
| 68 |
{{ range $result }} |
| 69 |
{{ if not .slug }} |
| 70 |
{{ continue }} |
| 71 |
{{ end }} |
| 72 |
{{ $markdown := transform.PortableText .body }} |
| 73 |
{{ $content := dict |
| 74 |
"mediaType" "text/markdown" |
| 75 |
"value" $markdown |
| 76 |
}} |
| 77 |
{{ $params := dict |
| 78 |
"portabletext" (.body | jsonify (dict "indent" " ")) |
| 79 |
}} |
| 80 |
{{ $page := dict |
| 81 |
"content" $content |
| 82 |
"kind" "page" |
| 83 |
"path" .slug.current |
| 84 |
"title" .title |
| 85 |
"date" (.publishedAt | time ) |
| 86 |
"summary" .summary |
| 87 |
"params" $params |
| 88 |
}} |
| 89 |
{{ $.AddPage $page }} |
| 90 |
{{ end }} |
| 91 |
``` |
| 92 |
|
| 93 |
### Sanity setup |
| 94 |
|
| 95 |
The following outlines a suitable Sanity studio setup for the above example. |
| 96 |
|
| 97 |
```ts {file="sanity.config.ts" copy=true} |
| 98 |
import {defineConfig} from 'sanity' |
| 99 |
import {structureTool} from 'sanity/structure' |
| 100 |
import {visionTool} from '@sanity/vision' |
| 101 |
import {schemaTypes} from './schemaTypes' |
| 102 |
import {media} from 'sanity-plugin-media' |
| 103 |
import {codeInput} from '@sanity/code-input' |
| 104 |
|
| 105 |
export default defineConfig({ |
| 106 |
name: 'default', |
| 107 |
title: 'my-sanity-project', |
| 108 |
|
| 109 |
projectId: 'mysanityprojectid', |
| 110 |
dataset: 'production', |
| 111 |
|
| 112 |
plugins: [structureTool(), visionTool(), media(),codeInput()], |
| 113 |
|
| 114 |
schema: { |
| 115 |
types: schemaTypes, |
| 116 |
}, |
| 117 |
}) |
| 118 |
``` |
| 119 |
|
| 120 |
Type/schema definition: |
| 121 |
|
| 122 |
```ts {file="schemaTypes/postType.ts" copy=true} |
| 123 |
import {defineField, defineType} from 'sanity' |
| 124 |
|
| 125 |
export const postType = defineType({ |
| 126 |
name: 'post', |
| 127 |
title: 'Post', |
| 128 |
type: 'document', |
| 129 |
fields: [ |
| 130 |
defineField({ |
| 131 |
name: 'title', |
| 132 |
type: 'string', |
| 133 |
validation: (rule) => rule.required(), |
| 134 |
}), |
| 135 |
defineField({ |
| 136 |
name: 'summary', |
| 137 |
type: 'string', |
| 138 |
validation: (rule) => rule.required(), |
| 139 |
}), |
| 140 |
defineField({ |
| 141 |
name: 'slug', |
| 142 |
type: 'slug', |
| 143 |
options: {source: 'title'}, |
| 144 |
validation: (rule) => rule.required(), |
| 145 |
}), |
| 146 |
defineField({ |
| 147 |
name: 'publishedAt', |
| 148 |
type: 'datetime', |
| 149 |
initialValue: () => new Date().toISOString(), |
| 150 |
validation: (rule) => rule.required(), |
| 151 |
}), |
| 152 |
defineField({ |
| 153 |
name: 'body', |
| 154 |
type: 'array', |
| 155 |
of: [ |
| 156 |
{ |
| 157 |
type: 'block', |
| 158 |
}, |
| 159 |
{ |
| 160 |
type: 'image' |
| 161 |
}, |
| 162 |
{ |
| 163 |
type: 'code', |
| 164 |
options: { |
| 165 |
language: 'css', |
| 166 |
languageAlternatives: [ |
| 167 |
{title: 'HTML', value: 'html'}, |
| 168 |
{title: 'CSS', value: 'css'}, |
| 169 |
], |
| 170 |
withFilename: true, |
| 171 |
}, |
| 172 |
}, |
| 173 |
], |
| 174 |
}), |
| 175 |
], |
| 176 |
}) |
| 177 |
``` |
| 178 |
|
| 179 |
Note that the above requires some additional plugins to be installed: |
| 180 |
|
| 181 |
```sh |
| 182 |
npm i sanity-plugin-media @sanity/code-input |
| 183 |
``` |
| 184 |
|
| 185 |
```ts {file="schemaTypes/index.ts" copy=true} |
| 186 |
import {postType} from './postType' |
| 187 |
|
| 188 |
export const schemaTypes = [postType] |
| 189 |
``` |
| 190 |
|
| 191 |
## Server setup |
| 192 |
|
| 193 |
Unfortunately, Sanity's API does not support [RFC 7234][] and their output changes even if the data has not. A recommended setup is therefore to use their cached `apicdn` endpoint (see above) and then set up a reasonable polling and file cache strategy in your Hugo configuration, e.g: |
| 194 |
|
| 195 |
<!-- markdownlint-disable MD049 --> |
| 196 |
{{< code-toggle file=hugo >}} |
| 197 |
[HTTPCache] |
| 198 |
[[HTTPCache.polls]] |
| 199 |
disable = false |
| 200 |
low = '30s' |
| 201 |
high = '3m' |
| 202 |
[HTTPCache.polls.for] |
| 203 |
includes = ['https://*.*.sanity.io/**'] |
| 204 |
|
| 205 |
[caches.getresource] |
| 206 |
dir = ':cacheDir/:project' |
| 207 |
maxAge = "5m" |
| 208 |
{{< /code-toggle >}} |
| 209 |
<!-- markdownlint-enable MD049 --> |
| 210 |
|
| 211 |
The polling above will be used when running the server/watch mode and rebuilds when you push new content to Sanity. |
| 212 |
|
| 213 |
See [Caching in resources.GetRemote][] for more fine-grained control. |
| 214 |
|
| 215 |
[Caching in resources.GetRemote]: /functions/resources/getremote/#caching |
| 216 |
[Portable Text]: https://www.portabletext.org/ |
| 217 |
[RFC 7234]: https://tools.ietf.org/html/rfc7234 |
| 218 |
[Sanity]: https://www.sanity.io/ |
| 219 |
[code-input]: https://www.sanity.io/plugins/code-input |
| 220 |
[content adapter]: /content-management/content-adapters/ |
| 221 |
[image render hook]: /render-hooks/images/ |
| 222 |
[render hooks]: /render-hooks/ |