Skip to content

Instantly share code, notes, and snippets.

@jamesporter
Created March 26, 2023 20:35
Show Gist options
  • Save jamesporter/fc7902e481c32a12c3f310e762daaf73 to your computer and use it in GitHub Desktop.
Save jamesporter/fc7902e481c32a12c3f310e762daaf73 to your computer and use it in GitHub Desktop.
Convert Sonic Pi Tutorial to epub
  • Download Sonic Pi repo
  • Add above to /sonic-pi/etc/doc/tutorial
  • Install pandoc, e.g. brew install pandoc
  • Run node build.js
  • Run pandoc -o sonic-pi-tutorial.epub tutorial.md
const fs = require("fs");
const path = require("path");
const mds = fs.readdirSync("./").filter((f) => f.includes(".md"));
let contents = `
---
title: Sonic Pi Tutorial
author: Sam Aaron
...
`;
const titleRe = /^#{1,6}.*/;
mds.forEach((md) => {
const fileContents = String(fs.readFileSync(md));
const processed = fileContents
.split("\n")
.map((line, i) => {
if (i === 0) {
return "# " + line;
} else if (titleRe.test(line)) {
return "#" + line;
} else {
return line;
}
})
.join("\n");
contents += processed;
contents += "\n\n";
});
fs.writeFileSync("tutorial.md", contents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment