Automatically split file into chapters and sections

Is there a tool that would automatically split a larger text file into chapters and sections? I’d like to use it with the Learn theme for documentation and legal documents.

For example, I have software documentation or a city ordinance in a single markdown document. Chapter and section or article headings are properly marked. I would now like to create separate folders and Markdown files for each chapter and section.

Preferably, I’d also like to be able to view the whole document or select chapter or sections only. After I have separated the document into chapters and sections, I would like to retain the option to view the whole document on one page. I particularly wish to view a complete chapter containing all sections. This makes research much more efficient.

Thank you,

See also: Grav and Markdown for Legal Documents