Hi! I also ran into the issue of setting the public preview. I resolved it by creating the post as ?source=html
, getting the mobiledoc from ghost, adding the paywall marker as a card and inserting it before the right section, and then updating the mobiledoc.
After creating the post (from html), I call this function to make the first paragraph a preview. Sorry if this is a bit messy, but hope it helps!
function addPaywallAfterFirstParagraph(post, authorization) {
let md = JSON.parse(post.mobiledoc);
// Verify existence of, or add, a 'paywall' card:
console.log(`Cards were: ${md.cards}.`)
let i = md.cards.findIndex(card => card[0] == 'paywall');
if (i == -1) {
md.cards.push(['paywall', {}]);
console.log(`Cards are now: ${md.cards}.`)
i = md.cards.length - 1;
}
console.log(`Paywall card index is ${i}.`);
// If there is already a paywall section, stop.
if (md.sections.findIndex(item => (item[0] == 10 && item[1] == i)) != -1) {
console.log(`There is already a paywall section marker in post ${post.id}.`);
return;
}
// We will add the card before the first index.
let sectionIndex = 1;
// Add paywall
md.sections.splice(sectionIndex, 0, [10,i]);
console.log(md.sections[24]);
post.mobiledoc = JSON.stringify(md);
return updatePost(post, authorization);
}