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

Code before:
  if (needSave) {
    ensureDir(getPath('userData'))
    const json = JSON.stringify(recentDocuments, null, 2)
    fs.writeFileSync(recentlyUsedDocumentsPath, json, 'utf-8')
  }
}

export const clearRecentlyUsedDocuments = () => {
  if (isOsxOrWindows) app.clearRecentDocuments()
  if (process.platform === 'darwin') return

  const recentDocuments = []
  updateApplicationMenu(recentDocuments)
  const json = JSON.stringify(recentDocuments, null, 2)
  ensureDir(getPath('userData'))
  fs.writeFileSync(recentlyUsedDocumentsPath, json, 'utf-8')
}

export const getRecentlyUsedDocuments = () => {
  if (!isFile(recentlyUsedDocumentsPath)) {
    return []
  }

  try {
    let recentDocuments = JSON.parse(fs.readFileSync(recentlyUsedDocumentsPath, 'utf-8'))
    for (const item of recentDocuments) {
      if (!item || !isFile(item)) {
        recentDocuments.splice(recentDocuments.indexOf(item), 1)
      }
    }
    if (recentDocuments.length > MAX_RECENTLY_USED_DOCUMENTS) {
      recentDocuments.splice(MAX_RECENTLY_USED_DOCUMENTS, recentDocuments.length - MAX_RECENTLY_USED_DOCUMENTS)
    }
    return recentDocuments
  } catch (err) {
    log(err)
    return []
  }
}

export const updateApplicationMenu = (recentUsedDocuments) => {
  if (!recentUsedDocuments) {
    recentUsedDocuments = getRecentlyUsedDocuments()
  }

  // "we don't support changing menu object after calling setMenu, the behavior
  // is undefined if user does that." That means we have to recreate the
  // application menu each time.

  const menu = Menu.buildFromTemplate(configureMenu(recentUsedDocuments))
  Menu.setApplicationMenu(menu)

Review comment (suggestion):
maybe `Array.prototype.filter` is a better method to to this.

Code after:

  if (needSave) {
    ensureDir(getPath('userData'))
    const json = JSON.stringify(recentDocuments, null, 2)
    fs.writeFileSync(recentlyUsedDocumentsPath, json, 'utf-8')
  }
}

export const clearRecentlyUsedDocuments = () => {
  if (isOsxOrWindows) app.clearRecentDocuments()
  if (process.platform === 'darwin') return

  const recentDocuments = []
  updateApplicationMenu(recentDocuments)
  const json = JSON.stringify(recentDocuments, null, 2)
  ensureDir(getPath('userData'))
  fs.writeFileSync(recentlyUsedDocumentsPath, json, 'utf-8')
}

export const getRecentlyUsedDocuments = () => {
  if (!isFile(recentlyUsedDocumentsPath)) {
    return []
  }

  try {
    let recentDocuments = JSON.parse(fs.readFileSync(recentlyUsedDocumentsPath, 'utf-8'))
      .filter(f => f && isFile(f))
    if (recentDocuments.length > MAX_RECENTLY_USED_DOCUMENTS) {
      recentDocuments.splice(MAX_RECENTLY_USED_DOCUMENTS, recentDocuments.length - MAX_RECENTLY_USED_DOCUMENTS)
    }
    return recentDocuments
  } catch (err) {
    log(err)
    return []
  }
}

export const updateApplicationMenu = (recentUsedDocuments) => {
  if (!recentUsedDocuments) {
    recentUsedDocuments = getRecentlyUsedDocuments()
  }

  // "we don't support changing menu object after calling setMenu, the behavior
  // is undefined if user does that." That means we have to recreate the
  // application menu each time.

  const menu = Menu.buildFromTemplate(configureMenu(recentUsedDocuments))
  Menu.setApplicationMenu(menu)
  if (!initMacDock && process.platform === 'darwin') {
    // app.dock is only for macosx
    app.dock.setMenu(dockMenu)