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

Code before:
  return endOfLine === 'crlf' || isWin ? 'crlf' : 'lf'
}

const getLineEnding = lineEnding => {
  if (lineEnding === 'lf') {
    return '\n'
  } else if (lineEnding === 'crlf') {
    return '\r\n'
  }
  return getOsLineEndingName() === 'crlf' ? '\r\n' : '\n'
}

export const writeFile = (pathname, content, extension, e, callback = null) => {
  if (pathname) {
    pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
    fs.writeFile(pathname, content, 'utf-8', err => {
      if (err) log(err)
      if (callback) callback(err, pathname)
    })
  } else {
    log('[ERROR] Cannot save file without path.')
  }
}

export const writeMarkdownFile = (pathname, content, extension, options, win, e, quitAfterSave = false) => {
  if (options.isUtf8BomEncoded) {
    content = '\uFEFF' + content
  }

  if (options.adjustLineEndingOnSave) {
    content = convertLineEndings(content, options.lineEnding)
  }

  writeFile(pathname, content, extension, e, (err, filePath) => {
    if (!err) e.sender.send('AGANI::file-saved-successfully')
    const filename = path.basename(filePath)
    if (e && filePath) e.sender.send('AGANI::set-pathname', { pathname: filePath, filename })
    if (!err && quitAfterSave) forceClose(win)
  })
}

export const loadMarkdownFile = (win, pathname) => {
  fs.readFile(path.resolve(pathname), 'utf-8', (err, file) => {
    if (err) {
      log(err)
      return
    }

    // Check UTF-8 BOM (EF BB BF) encoding
    const isUtf8BomEncoded = file.length >= 1 && file.charCodeAt(0) === 0xFEFF
    if (isUtf8BomEncoded) {

Review comment (suggestion):
Prefer deconstruction

```javascript
const { isUtf8BomEncoded, adjustLineEndingOnSave, lineEnding } = options
```

Code after:
  return endOfLine === 'crlf' || isWin ? 'crlf' : 'lf'
}

const getLineEnding = lineEnding => {
  if (lineEnding === 'lf') {
    return '\n'
  } else if (lineEnding === 'crlf') {
    return '\r\n'
  }
  return getOsLineEndingName() === 'crlf' ? '\r\n' : '\n'
}

export const writeFile = (pathname, content, extension, e, callback = null) => {
  if (pathname) {
    pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
    fs.writeFile(pathname, content, 'utf-8', err => {
      if (err) log(err)
      if (callback) callback(err, pathname)
    })
  } else {
    log('[ERROR] Cannot save file without path.')
  }
}

export const writeMarkdownFile = (pathname, content, extension, options, win, e, quitAfterSave = false) => {
  const { adjustLineEndingOnSave, isUtf8BomEncoded, lineEnding } = options
  if (isUtf8BomEncoded) {
    content = '\uFEFF' + content
  }

  if (adjustLineEndingOnSave) {
    content = convertLineEndings(content, lineEnding)
  }

  writeFile(pathname, content, extension, e, (err, filePath) => {
    if (!err) e.sender.send('AGANI::file-saved-successfully')
    const filename = path.basename(filePath)
    if (e && filePath) e.sender.send('AGANI::set-pathname', { pathname: filePath, filename })
    if (!err && quitAfterSave) forceClose(win)
  })
}

export const loadMarkdownFile = (win, pathname) => {
  fs.readFile(path.resolve(pathname), 'utf-8', (err, file) => {
    if (err) {
      log(err)
      return
    }

    // Check UTF-8 BOM (EF BB BF) encoding
    const isUtf8BomEncoded = file.length >= 1 && file.charCodeAt(0) === 0xFEFF