I came here to say the same thing. Laying content out for a book comes with way more issues than "just" converting between document formats. Leanpub does it well out of the box.
If you want to look down the path of implementing book layout yourself, here are two breadcrumbs from my bookmarks:
https://journal.stuffwithstuff.com/2014/11/03/bringing-my-we...
https://iangmcdowell.com/blog/posts/laying-out-a-book-with-c...
For leanpub, I started using it for the layout. And kept using it for the marketing.
That's one thing I learned, having written a couple of books. Even though writing is tough, it is easier in many wyas than the marketing.
Thanks! Didn't know Leanpub has its own markdown too. Yes I do want to keep everything local.
Won't work if you want to keep everything local, though.