Prevent single-slash paths from being parsed as filepaths (#6966)

* fix: prevent single-slash paths from being parsed as filepaths

* add release notes

* [autofix.ci] apply automated fixes

* test: add tests related to filepath

* additonal test from coderabbit

---------

Co-authored-by: Pratik Silwal <pratiksilwal@Pratiks-MacBook-Air.local>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Pratik Silwal
2026-02-20 06:38:29 -06:00
committed by GitHub
parent 37ad0ed563
commit bf1947a119
3 changed files with 95 additions and 1 deletions

View File

@@ -108,5 +108,93 @@ describe('linkParser', () => {
expect(lastSegment).toEqual({ type: 'text', content: ')' });
});
});
describe('file path detection', () => {
describe('Unix paths', () => {
it('should detect multi-segment absolute paths', () => {
const result = parseNotes('Check /home/user/file.txt for details');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/home/user/file.txt',
displayText: '/home/user/file.txt',
url: '/home/user/file.txt',
isFilePath: true,
});
});
it('should detect two-segment paths', () => {
const result = parseNotes('See /etc/nginx.conf');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/etc/nginx.conf',
displayText: '/etc/nginx.conf',
url: '/etc/nginx.conf',
isFilePath: true,
});
});
it('should NOT detect single-segment paths', () => {
const result = parseNotes('Navigate to /transaction page');
const linkSegments = result.filter(s => s.type === 'link');
expect(linkSegments).toHaveLength(0);
});
it('should handle paths with trailing slashes', () => {
const result = parseNotes('Look in /usr/bin/ directory');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/usr/bin/',
displayText: '/usr/bin/',
url: '/usr/bin/',
isFilePath: true,
});
});
it('should detect deep nested paths', () => {
const result = parseNotes('File at /var/log/nginx/access.log');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/var/log/nginx/access.log',
displayText: '/var/log/nginx/access.log',
url: '/var/log/nginx/access.log',
isFilePath: true,
});
});
it('should NOT match paths with spaces', () => {
const result = parseNotes('Not a path: /this has spaces/file');
const linkSegments = result.filter(s => s.type === 'link');
expect(linkSegments).toHaveLength(0);
});
it('should detect single-segment paths with trailing slash', () => {
const result = parseNotes('Navigate to /transaction/ page');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/transaction/',
displayText: '/transaction/',
url: '/transaction/',
isFilePath: true,
});
});
it('should detect paths with dashes and underscores', () => {
const result = parseNotes('See /opt/my-app/config_file.yml');
const linkSegment = result.find(s => s.type === 'link');
expect(linkSegment).toEqual({
type: 'link',
content: '/opt/my-app/config_file.yml',
displayText: '/opt/my-app/config_file.yml',
url: '/opt/my-app/config_file.yml',
isFilePath: true,
});
});
});
});
});
});

View File

@@ -13,7 +13,7 @@ export type ParsedSegment =
const MARKDOWN_LINK_REGEX = /\[([^\]]+)\]\(([^)]+)\)/;
const FULL_URL_REGEX = /https?:\/\/[^\s]+/;
const WWW_URL_REGEX = /www\.[^\s]+/;
const UNIX_PATH_REGEX = /^\/(?:[^\s/]+\/)*[^\s/]+$/;
const UNIX_PATH_REGEX = /^\/[^\s/]+\/[^\s]*$/;
const WINDOWS_PATH_REGEX = /^[A-Z]:\\(?:[^\s\\]+\\)*[^\s\\]+$/i;
// Common trailing punctuation that should not be part of URLs

View File

@@ -0,0 +1,6 @@
---
category: Bugfixes
authors: [PratikSilwal5]
---
Prevent single-slash paths from being parsed as filepaths