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...