Repository: marktext/marktext
File: src/main/actions/file.js (JavaScript)

Code before:
import fs from 'fs'
// import chokidar from 'chokidar'
import path from 'path'
import { BrowserWindow, dialog, ipcMain } from 'electron'
import appWindow from '../window'
import { EXTENSION_HASN, EXTENSIONS } from '../config'
import { writeFile, writeMarkdownFile } from '../utils/filesystem'
import appMenu from '../menu'
import { getPath, isMarkdownFile, log, isFile, isDirectory } from '../utils'
import userPreference from '../preference'

// handle the response from render process.
const handleResponseForExport = (e, { type, content, pathname }) => {
  const win = BrowserWindow.fromWebContents(e.sender)
  const extension = EXTENSION_HASN[type]
  const dirname = pathname ? path.dirname(pathname) : getPath('documents')
  const nakedFilename = pathname ? path.basename(pathname, '.md') : 'untitled'
  const defaultPath = `${dirname}/${nakedFilename}${extension}`
  const filePath = dialog.showSaveDialog(win, {
    defaultPath
  })

  if (pathname) {
    // If export PDF, the content will be undefined.
    if (!content && type === 'pdf') {
      win.webContents.printToPDF({ printBackground: true }, (err, data) => {
        if (err) log(err)
        else {
          writeFile(filePath, data, extension)
            .then(() => {
              win.webContents.send('AGANI::export-success', { type, filePath })
            })
            .catch(log)
        }
      })
    } else {
      writeFile(filePath, content, extension)
        .then(() => {
          win.webContents.send('AGANI::export-success', { type, filePath })
        })
        .catch(log)
    }
  }
}

const handleResponseForSave = (e, { id, markdown, pathname, options }) => {
  const win = BrowserWindow.fromWebContents(e.sender)


Review comment (suggestion):
You have to check for `filePath`, because `pathname` may be empty or `undefined` if the file is not saved.

Code after:
import fs from 'fs'
// import chokidar from 'chokidar'
import path from 'path'
import { BrowserWindow, dialog, ipcMain } from 'electron'
import appWindow from '../window'
import { EXTENSION_HASN, EXTENSIONS } from '../config'
import { writeFile, writeMarkdownFile } from '../utils/filesystem'
import appMenu from '../menu'
import { getPath, isMarkdownFile, log, isFile, isDirectory } from '../utils'
import userPreference from '../preference'

// handle the response from render process.
const handleResponseForExport = (e, { type, content, pathname }) => {
  const win = BrowserWindow.fromWebContents(e.sender)
  const extension = EXTENSION_HASN[type]
  const dirname = pathname ? path.dirname(pathname) : getPath('documents')
  const nakedFilename = pathname ? path.basename(pathname, '.md') : 'untitled'
  const defaultPath = `${dirname}/${nakedFilename}${extension}`
  const filePath = dialog.showSaveDialog(win, {
    defaultPath
  })

  if (filePath) {
    // If export PDF, the content will be undefined.
    if (!content && type === 'pdf') {
      win.webContents.printToPDF({ printBackground: true }, (err, data) => {
        if (err) log(err)
        else {
          writeFile(filePath, data, extension)
            .then(() => {
              win.webContents.send('AGANI::export-success', { type, filePath })
            })
            .catch(log)
        }
      })
    } else {
      writeFile(filePath, content, extension)
        .then(() => {
          win.webContents.send('AGANI::export-success', { type, filePath })
        })
        .catch(log)
    }
  }
}

const handleResponseForSave = (e, { id, markdown, pathname, options }) => {
  const win = BrowserWindow.fromWebContents(e.sender)