I write my books in markdown and use pandoc to generate PDF and EPUB.
markdown -> docx (pandoc)
docx -> kindle (calibre)
I use both pandoc and calibre as offline CLIs, and the resulting kindle book has a nicely formatted table of contents with correct chapter markers, etc.