Search code examples
markdown

How do I split a markdown file into separate files at the heading


I have a book in markdown format. I want to split it into separate files at the chapter headings. How can I do this?


Solution

  • have a book in markdown format. I want to split it into separate files at the chapter headings. How can I do this?

    If you are using Pandoc, you can convert your Markdown file to EPUB, unzip the EPUB file and convert the HTML files into Markdown. Not the perfect solution but you can accomplish it with a few lines of bash script like

    pandoc -f markdown -t epub -o my-book.epub my-book.md
    unzip my-book.epub
    for chapter in *.html
    do
    pandoc -f html -t markdown -o ${chapter/html/md} ${chapter}
    done
    

    You need to fix the path to the HTML files.

    If you want to program something and you have some experience, shouldn't be hard to write a Python/... script to split the file.