formax68_memoChron/src/utils/pathUtils.ts
formax68 acf46265b9 refactor(08-02): fix no-case-declarations and no-useless-escape (DIR-09)
- pathUtils.ts:47 — wrap case PathType.FILE_URL body in { ... } block scope
  (the let normalized declaration leaked across cases without braces)
- viewRenderers.ts:370 — drop unnecessary backslash inside regex character
  class: [-\/] -> [-/] (slash does not need escaping inside [...])

Nullish-coalescing audit: grep -rnE '\b(null|undefined|"")\s*\?\?' src/
returned only the documentation-comment hit at SettingsTab.ts:262. No
executable violations.
2026-05-17 09:34:58 +03:00

98 lines
No EOL
2.4 KiB
TypeScript

import { normalizePath } from "obsidian";
export enum PathType {
HTTP_URL = "http_url",
FILE_URL = "file_url",
VAULT_RELATIVE = "vault_relative",
ABSOLUTE_PATH = "absolute_path",
}
export interface PathInfo {
type: PathType;
originalPath: string;
normalizedPath: string;
}
export function detectPathType(path: string): PathType {
if (!path) {
return PathType.VAULT_RELATIVE;
}
// Check for HTTP(S) URLs
if (path.startsWith("http://") || path.startsWith("https://")) {
return PathType.HTTP_URL;
}
// Check for file:// URLs
if (path.startsWith("file://")) {
return PathType.FILE_URL;
}
// Check for absolute paths
// Windows: C:\, D:\, C:/, D:/, etc.
// Unix: /
if (
(path.length >= 3 && path[1] === ":" && (path[2] === "\\" || path[2] === "/")) ||
path.startsWith("/")
) {
return PathType.ABSOLUTE_PATH;
}
// Everything else is considered vault-relative
return PathType.VAULT_RELATIVE;
}
export function normalizeFilePath(path: string, type: PathType): string {
switch (type) {
case PathType.FILE_URL: {
// Remove file:// or file:/// prefix and decode URI components
// Windows often uses file:///C:/path, Unix uses file:///path
let normalized = decodeURIComponent(path.replace(/^file:\/\/\/?/, ""));
// On Windows, if path starts with a drive letter without slash, it's already correct
// On Unix, we need to ensure the leading slash is preserved
if (!normalized.startsWith("/") && !normalized.match(/^[A-Za-z]:/)) {
normalized = "/" + normalized;
}
return normalized;
}
case PathType.VAULT_RELATIVE:
// Normalize path for Obsidian
return normalizePath(path);
case PathType.ABSOLUTE_PATH:
// Return as-is for absolute paths
return path;
case PathType.HTTP_URL:
// Return as-is for HTTP URLs
return path;
default:
return path;
}
}
export function getPathInfo(path: string): PathInfo {
const type = detectPathType(path);
const normalizedPath = normalizeFilePath(path, type);
return {
type,
originalPath: path,
normalizedPath,
};
}
export function isLocalPath(pathInfo: PathInfo): boolean {
return (
pathInfo.type === PathType.FILE_URL ||
pathInfo.type === PathType.VAULT_RELATIVE ||
pathInfo.type === PathType.ABSOLUTE_PATH
);
}
export function isRemoteUrl(pathInfo: PathInfo): boolean {
return pathInfo.type === PathType.HTTP_URL;
}