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

Code before:
  if (pathname) {
    pathname = pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
    const filename = path.basename(pathname)
    fs.writeFile(pathname, content, 'utf-8', err => {
      if (err) return console.log('save as file failed')
      // not export
      if (extension === '.md' && e) e.sender.send('AGANI::set-pathname', { pathname, filename })
    })
    // watchAndReload(pathname, win)
  }
}

const forceClose = win => {
  if (windows.has(win.id)) {
    windows.delete(win.id)
  }
  win.destroy() // if use win.close(), it will cause a endless loop.
  if (windows.size === 0) {
    app.quit()
  }
}
// handle the response from render process.
const handleResponseForExport = (e, { type, content, filename, pathname }) => {
  const win = BrowserWindow.fromWebContents(e.sender)
  const extension = EXTENSION_HASN[type]
  const dirname = pathname ? path.dirname(pathname) : getHomePath()
  const nakedFilename = pathname ? path.basename(pathname, '.md') : 'untitled'
  const defaultPath = `${dirname}/${nakedFilename}${extension}`
  const filePath = dialog.showSaveDialog(win, {
    defaultPath
  })

  if (!content && type === 'pdf') {
    win.webContents.printToPDF({ printBackground: true }, (err, data) => {
      if (err) throw err
      writeFile(filePath, data, extension, win, e)
    })
  } else {
    writeFile(filePath, content, extension, win, e)
  }
}

const handleResponseForSave = (e, { markdown, pathname }) => {
  const win = BrowserWindow.fromWebContents(e.sender)
  if (pathname) {
    fs.writeFile(pathname, markdown, 'utf-8', err => {
      if (err) console.log('save file failed')
    })
  } else {
    const filePath = dialog.showSaveDialog(win, {
      defaultPath: getHomePath() + '/Untitled.md'

Review comment (suggestion):
I think the directory of `documents` will be much better to store the default export or save files.

What do you think?

```javascript
-  const dirname = pathname ? path.dirname(pathname) : '~'
+  const dirname = pathname ? path.dirname(pathname) : getPath('documents')
```

Code after:
  if (pathname) {
    pathname = pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
    const filename = path.basename(pathname)
    fs.writeFile(pathname, content, 'utf-8', err => {
      if (err) return console.log('save as file failed')
      // not export
      if (extension === '.md' && e) e.sender.send('AGANI::set-pathname', { pathname, filename })
    })
    // watchAndReload(pathname, win)
  }
}

const forceClose = win => {
  if (windows.has(win.id)) {
    windows.delete(win.id)
  }
  win.destroy() // if use win.close(), it will cause a endless loop.
  if (windows.size === 0) {
    app.quit()
  }
}
// handle the response from render process.
const handleResponseForExport = (e, { type, content, filename, 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 (!content && type === 'pdf') {
    win.webContents.printToPDF({ printBackground: true }, (err, data) => {
      if (err) throw err
      writeFile(filePath, data, extension, win, e)
    })
  } else {
    writeFile(filePath, content, extension, win, e)
  }
}

const handleResponseForSave = (e, { markdown, pathname }) => {
  const win = BrowserWindow.fromWebContents(e.sender)
  if (pathname) {
    fs.writeFile(pathname, markdown, 'utf-8', err => {
      if (err) console.log('save file failed')
    })
  } else {
    const filePath = dialog.showSaveDialog(win, {
      defaultPath: getPath('documents') + '/Untitled.md'