Saving the markdown text to the local drive

In this section, we are going to save the markdown into a temporary file. Follow these steps to do so:

  1. Import the os and path objects from Node.js:
const os = require('os');
const path = require('path');
  1. Now, import the file generation function. For the sake of simplicity, we're going to build a very minimalistic happy path function without validation or safety checks. Append the following code to the menu.js file:
function writeTempFile(contents, callback) {
const tempPath = path.join(os.tmpdir(), 'markdown');

fs.mkdtemp(tempPath, (err, folderName) => {
const filePath = path.join(folderName, 'markdown.md');

fs.writeFile(filePath, contents, 'utf8', () => {
callback(filePath);
});
});
}
The preceding code is for demonstration purposes only. You may want to improve this function later on with some error handling and error checks.
  1. Update the generateHTML code so that it looks as follows:
function generateHTML(contents) {
writeTempFile(contents, fileName => {
console.log('converting', fileName);
});
}

  1. Run the application and press Cmd + Alt + H for macOS or Ctrl + Alt + H for other platforms. We are just testing that the function works correctly. The console's output should look similar to the following:
converting /var/folders/6r/_zpzk77x67x5kg4h9dq8fb2w0000gp/T/markdownl9bsMt/markdown.md

You can even navigate to that file and see its contents. It should be the same text that you entered in the editor.

Now is an excellent time to learn how to invoke a child process from within Node.js. Running child processes allows you to execute external commands, including console scripts and other applications.

  1. Import the exec function from the child_process namespace, as well as the shell object from electron, as shown in the following code:
const { exec } = require('child_process');
const { shell } = require('electron');

We need the shell object to invoke the file in the default browser. For more details, please refer to the official documentation: https://electronjs.org/docs/api/shell#shell.

  1. Next, update the implementation of the generateHTML function according to the following code:
function generateHTML(contents) {
writeTempFile(contents, fileName => {
const name = 'markdown';
const filePath = path.dirname(fileName);
const command = `docker run -v ${filePath}:/source denysvuika/pandoc -f markdown -t html5 ${name}.md -o ${name}.html`;
exec(command, () => {
const outputPath = path.join(filePath, `${name}.html`);
shell.openItem(outputPath);
}).stderr.pipe(process.stderr);
});
}

You may be wondering what the code in the generateHTML function does. Let's go over this now:

  1. First, it generates a markdown.md file, along with its contents, in a temporary folder.
  2. Then, it generates a shell command to execute the docker command. This takes the markdown.md file and produces the markdown.html file in the same temporary folder.
  3. After that, the code runs the exec function to execute the command. As soon as the command finishes running, the code executes a second command to open the resulting markdown.html file with a default program that handles .html files. Typically, this action triggers your default web browser.

In some cases, you may see the following error in the console's output:

The path /var/folders/6r/_zpzk77x67x5kg4h9dq8fb2w0000gp/T/markdownAQbH2t
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.

This means that your default temporary folder is not present in Docker's settings.

  1. You can add any extra folders in the File Sharing configuration, as shown in the following screenshot:

Now it's time to test the whole workflow:

  1. Run the Electron version of the application with the npm run electron command.
  2. Press Cmd + Alt + H for macOS or Ctrl + Alt + H for other platforms. You should see the browser open with the HTML version of your markdown:

Congratulations on achieving another milestone. Now you can save and convert markdown files. You now know how to invoke external applications and execute shell commands from the Node.js process.

Let's see what it takes to generate a PDF version of our markdown content.

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

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