If you're building your own script, here is the general logic used by most open-source tools:
: Fetch your blog articles. You can use a tool like JSZip to start bundling files together.
If you're not looking to write a custom script today, there are excellent existing tools to bridge the gap: Generating an EPUB File in the Browser - Jim Nielsen's Blog