Format options
Keystatic is capable to store your data in multiple formats: YAML
, JSON
and Markdoc
.
By default, entries will be stored in a YAML
file, with the exception of document
fields, which will generate a sperate .mdoc
file.
The name and directory structure of entries is also affected by the path
settings on a given collection or singleton. See the Content organisation page for more details.
Example (default format options)
blog: collection({
label: 'Blog',
slugField: 'title',
schema: {
title: fields.slug({ name: { label: 'Title' } })
publishedDate: fields.date({ label: 'Published date' }),
body: fields.document({
label: 'Body',
formatting: true,
links: true
})
}
})
With the above config, creating a new blog
entry with a what-a-day slug
will generate the following files:
blog/what-a-day
├── index.yaml
└── body.mdoc
The index.yaml
file will look like so:
title: What a day
publishedDate: 2023-07-27
The body.mdoc
file will look like so:
What a **beautiful** day!
## Let's go to the beach
I say we pack our swimmers and towels and head to the beach.
Who's with me?
Example with JSON data
We can specifically ask for json
data instead of yaml
with the format.data
option in our blog
collection:
blog: collection({
label: 'Blog',
slugField: 'title',
+ format: { data: 'json' },
schema: { //... }
})
The files generated will now look like this:
blog/what-a-day
├── index.json
└── body.mdoc
The index.json
file will look like so:
{
"title": "What a day",
"publishedDate": "2023-07-27"
}
The body.mdoc
file will look like so:
What a **beautiful** day!
## Let's go to the beach
I say we pack our swimmers and towels and head to the beach.
Who's with me?
Example with single file output
Quite often, you may want Keystatic to output all the fields within the same file, with the "metadata" fields placed in a frontmatter on top of the file.
The format.contentField
option lets you do that.
You need to reference a document
field from the schema as the contentField
value for it to work:
blog: collection({
label: 'Blog',
slugField: 'title',
+ format: { contentField: 'body' },
schema: {
title: fields.slug({ name: { label: 'Title' } })
publishedDate: fields.date({ label: 'Published date' }),
body: fields.document({
label: 'Body',
formatting: true,
links: true
})
}
})
Instead of outputting the data for the body
field in a separate file, Keystatic will now output everything inside a single index.mdoc
file:
blog/what-a-day
└── index.mdoc
The index.mdoc
file will look like this:
---
title: What a day
publishedDate: 2023-07-27
---
What a **beautiful** day!
## Let's go to the beach
I say we pack our swimmers and towels and head to the beach.
Who's with me?
Type signature
Find the latest version of the Format
type signature at: https://docsmill.dev/npm/@keystatic/core@latest#/.Format