Skip to content

TagSpaces is an offline, open source, document manager with tagging support

feat: added resolving for relative links

Ilian Sapundshiev06b9e39e1788
diff --git a/src/renderer/components/md/utils.ts b/src/renderer/components/md/utils.ts
index e6ff08b54..4a5bb37a6 100644
--- a/src/renderer/components/md/utils.ts
+++ b/src/renderer/components/md/utils.ts
@@ -5,6 +5,33 @@ import { editorViewCtx, editorViewOptionsCtx } from '@milkdown/kit/core';
import { trailing } from '@milkdown/kit/plugin/trailing';
import { remarkPreserveEmptyLinePlugin } from '@milkdown/preset-commonmark';
+/**
+ * Resolves a relative path against a base folder path.
+ * Handles '../' and './' traversal.
+ */
+function resolveRelativePath(
+ baseFolderPath: string,
+ relativePath: string,
+): string {
+ const separator = baseFolderPath.includes('\\') ? '\\' : '/';
+ const baseParts = baseFolderPath
+ .replace(/\\/g, '/')
+ .split('/')
+ .filter(Boolean);
+ const relParts = relativePath.replace(/\\/g, '/').split('/');
+
+ for (const part of relParts) {
+ if (part === '..') {
+ baseParts.pop();
+ } else if (part !== '.') {
+ baseParts.push(part);
+ }
+ }
+
+ const joined = baseParts.join(separator);
+ return baseFolderPath.startsWith('/') ? '/' + joined : joined;
+}
+
export function createCrepeEditor(
root: HTMLElement,
defaultContent: string,
@@ -72,7 +99,17 @@ export function createCrepeEditor(
const href = (target as HTMLAnchorElement).getAttribute('href');
if (href && !href.startsWith('#')) {
event.preventDefault();
- openLink(href, { fullWidth: false });
+ let resolvedHref = href;
+ if (
+ currentFolder &&
+ !href.includes('://') &&
+ !href.startsWith('/')
+ ) {
+ const absolutePath = resolveRelativePath(currentFolder, href);
+ resolvedHref =
+ 'ts://?cmdopen=' + encodeURIComponent(absolutePath);
+ }
+ openLink(resolvedHref, { fullWidth: false });
return true;
}
}