Created
April 30, 2020 21:11
-
-
Save kmelve/c970011f79182ea80ed8e272b7d2b932 to your computer and use it in GitHub Desktop.
Initial Value Templates Commynity Digest Example for Sanity.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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