function sticky(id) {
const token = document.cookie.match(/(^|; )T=([^;]+)/)[2],
json = response => response.json(),
api = (route, options = {}) => fetch(`https://api.observablehq.com/${route}`, {
mode: 'cors',
credentials: 'include',
...options,
}),
post = (route, data) => api(route, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({token, ...data}),
}),
republish = () => api(`document/${id}`)
.then(json)
.then(({version}) => post(`document/${id}/unpublish`)
.then(() => post(`document/${id}/publish`, {version}))
);
return api('documents/public')
.then(json)
.then(list => id === list[0].id || republish());
}