Skip to content

Instantly share code, notes, and snippets.

@sjelfull
Forked from kmelve/initialValueTemplates.js
Last active May 5, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjelfull/a06c5f31594708a409bb5e901fc9d6b6 to your computer and use it in GitHub Desktop.
Save sjelfull/a06c5f31594708a409bb5e901fc9d6b6 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