Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created April 30, 2020 21:11
Show Gist options
  • Save kmelve/c970011f79182ea80ed8e272b7d2b932 to your computer and use it in GitHub Desktop.
Save kmelve/c970011f79182ea80ed8e272b7d2b932 to your computer and use it in GitHub Desktop.
Initial Value Templates Commynity Digest Example for Sanity.io
import T from '@sanity/base/initial-value-template-builder'
import client from 'part:@sanity/base/client'
const getNextDigestTitle = async () => {
const titles = await client.fetch(`*[
_type == "post" &&
title match "Community Digest*"
].title
`)
const highestNumber = titles
.map(title => title.match(/#(\d+)/)[1])
.sort((a, b) => b - a)[0]
return `Community Digest #${Number(highestNumber) + 1}`
}
export default [
...T.defaults(),
T.template({
id: 'communityDigestPost',
title: 'New community digest',
schemaType: 'post',
value: async () => {
const title = await getNextDigestTitle()
return {
title,
shortTitle: title,
authors: [
{
// Knut Melvær
_ref: '14165126-a0fd-4e5e-96af-686d36f408e0',
_type: 'reference'
}
],
text: [
{
_type: 'block',
style: 'h2',
markDefs: [],
children: [
{
_type: 'span',
text: 'Community Hightlights',
marks: []
}
]
},
{
style: 'h2',
_type: 'block',
markDefs: [],
children: [
{
_type: 'span',
text: 'Built with Sanity',
marks: []
}
]
}
]
}
}
})
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment