API Post HTML as HTML itself into the post's body

Hey!

I am trying to have a script create a new post with some html elements in the post. Ex. want to build a table in the script, and have it send that table to the post as a html table.

var table = '<table><tr><td>1</td><td>2</td></tr><tr><td>child1</td><td>child2</td></tr></table>'

api.posts
    .add(
        { title: 'some title', html: table },
        { source: 'html' }
    ).catch(err => console.log(err));

I hoped that it would actually drop the html into the post, but instead it comes out like this:
Screen Shot 2020-01-10 at 9.26.49 AM

Is there a way to tell the api to actually shove this html in?

Thanks!

@DavidB as you’ve found the ?source=html is for converting HTML to a rich-text (mobiledoc) document but this is a lossy operation.

However, for HTML cards we do have a workaround :slight_smile: You can use the same HTML comments that we use when generating our HTML source code from rich-text documents to wrap a HTML card and have the source preserved as-is, eg…

var table = `
<!--kg-card-begin: html-->
<table><tr><td>1</td><td>2</td></tr><tr><td>child1</td><td>child2</td></tr></table>
<!--kg-card-end: html-->
`;

api.posts
    .add(
        { title: 'some title', html: table },
        { source: 'html' }
    ).catch(err => console.log(err));

Beautiful! Thank you @Kevin!!