Generating ePub books

In this section, we are going to create a book in ePub format from the markdown content. The implementation steps should already be familiar to you now that you've gone through the HTML and PDF conversions.

On the browser side, let's slightly optimize the function so that we can run the conversion. Previously, we created separate functions, that is, generateHTML and generatePDF, to send a message to the generate channel.

If you take a look at the implementations of these functions carefully, you should notice that they only differ in terms of the format field. Instead of creating a third function called generateEPUB, I would suggest making the code more reusable.

Let's refactor our code and introduce the generateOutput function:

  1. Create the generateOutput function, which covers every generation scenario:
const generateOutput = (format, text) => {
if (window.require) {
const electron = window.require('electron');
const ipcRenderer = electron.ipcRenderer;

ipcRenderer.send('generate', {
format,
text
});
}
};

  1. Update the keyboard handlers so that we can reuse the universal function we have just introduced:
editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_H,
() => generateOutput(editor, 'html')
);

editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_P,
() => generateOutput(editor, 'pdf')
);
  1. At this point, we can remove the generateHTML and generatePDF functions as we no longer need them. Now, adding support for a new format conversion process is very trivial.
  2. Add the Cmd + Alt + E combination (Ctrl + Alt + E) so that we can invoke ePub generation:
editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_E,
() => generateOutput(editor, 'epub')
);
  1. Now, switch to the main.js file and append the generateEPUB function to the code:
function generateEPUB(contents) {
writeTempFile(contents, fileName => {
const name = 'markdown';
const filePath = path.dirname(fileName);
const command = `docker run -v ${filePath}:/source denysvuika/pandoc -f markdown ${name}.md -o ${name}.epub`;
exec(command, () => {
const outputPath = path.join(filePath, `${name}.epub`);
shell.openItem(outputPath);
}).stderr.pipe(process.stderr);
});
}

  1. Next, update the channel listener code so that it takes the epub format into account and triggers the correct function:
ipcMain.on('generate', (_, payload) => {
if (payload && payload.format) {
switch (payload.format) {
case 'html':
generateHTML(payload.text);
break;
case 'pdf':
generatePDF(payload.text);
break;
case 'epub':
generateEPUB(payload.text);
break;
default:
break;
}
}
});
You can find out more about the eBook generation process at https://pandoc.org/epub.html.
  1. Start your Electron application to test the resulting code.
  2. Pandoc requires a document to include some specific metadata so that it can generate the eBook correctly. The bare minimum is the title value. Update the text in the editor and provide some title metadata, as shown in the following code:
---
title: My First eBook
---

# hello world
  1. Press Cmd + Alt + E if you are on macOS or Ctrl + Alt + E for other platforms.
  1. This should make your operating system invoke the default application so that we can view epub files. In my case, I am using macOS and have the Books application running and displaying the resulting book:

Well done, and congratulations on finishing this minimalistic book generator implementation!

There are still many features you can introduce yourself if you wish to take on a practical exercise. Try adding export functionality so that you can export your files to a particular place while utilizing Save Dialog. Optionally, you can display generation errors on the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.243