I’m unable to set public previews via the Admin API for HTML posts.
It appears that this is set via the ‘<!--members-only-->’ HTML comment, however this is being ignored despite being correctly formatted in the request.
The parser you’ve linked to is for the beta editor, that’s not currently being used for the ?source=html route for creating posts but will be once the new editor comes out of beta.
The parsers for the original editor are in the kg-parser-plugins package which doesn’t have a paywall card parser. Happy to accept a PR for that
Btw I’ve discovered another minor issue - the html parser seems incapable of handling independent <li> tags. Have used a workaround on our end to parse and wrap those <li> tags into a <ul> tag.
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);
}
From Ghost 5.62.0 and later if you have the beta editor enabled then ?source=html will pick up the <!--members-only--> comment and convert to a paywall card.
Thanks Kevin, but I’m not getting this to work. I use Ghost Pro and have enabled the beta editor (I see post_revisions and the lexical content when requesting posts now).
My intention is to insert the paywall after the first paragraph of a post. get the html of an existing post, add the comment after the first </p>, and then update the post.
I’m doing an update by using PUT and including ?source=html at the end of the url. But although other changes are accepted, the <!--members-only--> comment is still ignored. I have the same problem when POSTing a new post.