Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/common/filesystem/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import fs from 'fs'
import path from 'path'
import { isFile, isFile2, isSymbolicLink } from './index'
import { minimatch } from 'minimatch'
import { shell } from 'electron'

const isOsx = process.platform === 'darwin'
const isWindows = process.platform === 'win32'

export const MARKDOWN_EXTENSIONS = Object.freeze([
'markdown',
Expand All @@ -30,7 +32,7 @@ export const IMAGE_EXTENSIONS = Object.freeze(['jpeg', 'jpg', 'png', 'gif', 'svg
*/
export const hasMarkdownExtension = (filename) => {
if (!filename || typeof filename !== 'string') return false
return MARKDOWN_EXTENSIONS.some((ext) => filename.toLowerCase().endsWith(`.${ext}`))
return MARKDOWN_EXTENSIONS.some((ext) => resolveShortcut(filename).toLowerCase().endsWith(`.${ext}`))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasMarkdownExtension now calls resolveShortcut(filename) on every invocation. On Windows this causes shell.readShortcutLink to run for non-.lnk paths as well (including simple names like "foo" from the renderer), which can be a hot path (watcher filtering, quick open, etc.) and may rely on exceptions for control flow. Consider making resolveShortcut a no-op unless the path ends with .lnk (and ideally exists/is a file) so that normal markdown extension checks stay cheap.

Suggested change
return MARKDOWN_EXTENSIONS.some((ext) => resolveShortcut(filename).toLowerCase().endsWith(`.${ext}`))
const normalizedFilename = filename.toLowerCase()
const candidate = isWindows && normalizedFilename.endsWith('.lnk')
? resolveShortcut(filename)
: filename
return MARKDOWN_EXTENSIONS.some((ext) => candidate.toLowerCase().endsWith(`.${ext}`))

Copilot uses AI. Check for mistakes.
}

/**
Expand Down Expand Up @@ -133,3 +135,19 @@ export const checkPathExcludePattern = (pathname, patterns) => {
}
return false
}

/**
* Returns the actual path pointed to by a shortcut file, or the original path if resolution fails.
* @param {string} The absolute path of the shortcut file.
*/
export const resolveShortcut = (shortcutPath) => {
try {
if (isWindows) {
return shell.readShortcutLink(shortcutPath)?.target || shortcutPath
Comment thread
Tkaixiang marked this conversation as resolved.
} else {
return shortcutPath
}
} catch {
return shortcutPath
}
}
4 changes: 2 additions & 2 deletions src/main/filesystem/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import log from 'electron-log'
import iconv from 'iconv-lite'
import { LINE_ENDING_REG, LF_LINE_ENDING_REG, CRLF_LINE_ENDING_REG } from '../config'
import { isDirectory2 } from 'common/filesystem'
import { isMarkdownFile } from 'common/filesystem/paths'
import { isMarkdownFile, resolveShortcut } from 'common/filesystem/paths'
import { normalizeAndResolvePath, writeFile } from '../filesystem'
import { guessEncoding } from './encoding'

Expand Down Expand Up @@ -87,7 +87,7 @@ export const loadMarkdownFile = async (
// TODO: Use streams to not buffer the file multiple times and only guess
// encoding on the first 256/512 bytes.

let buffer = await fsPromises.readFile(path.resolve(pathname))
let buffer = await fsPromises.readFile(path.resolve(resolveShortcut(pathname)))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadMarkdownFile now reads from resolveShortcut(pathname) but still treats pathname/filename as the original input later in the function. If a user opens a .lnk, the tab will keep the .lnk pathname and saving will write back to the shortcut path (potentially corrupting the .lnk) instead of saving to the target markdown file. Consider resolving once up-front (after path.resolve) and using the resolved target path consistently for reading and for the returned pathname/filename (optionally also preserving the original shortcut path separately if needed).

Copilot uses AI. Check for mistakes.

const encoding = guessEncoding(buffer, autoGuessEncoding)
const supported = iconv.encodingExists(encoding.encoding)
Expand Down