Skip to content

Commit 7b52dbe

Browse files
committed
wip
1 parent 1662562 commit 7b52dbe

1 file changed

Lines changed: 39 additions & 20 deletions

File tree

apps/obsidian/src/utils/importNodes.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,7 @@ const updateMarkdownAssetLinks = ({
487487
app: App;
488488
originalNodePath?: string;
489489
}): string => {
490-
if (oldPathToNewPath.size === 0) {
491-
return content;
492-
}
493-
494-
// Create a set of all new paths for quick lookup (used by findImportedAssetFile)
490+
// Create a set of all new paths for quick lookup (used by findImportedAssetFile when pathMapping has entries)
495491
const newPaths = new Set(oldPathToNewPath.values());
496492

497493
let updatedContent = content;
@@ -500,6 +496,13 @@ const updateMarkdownAssetLinks = ({
500496
? targetFile.path.replace(/\/[^/]*$/, "")
501497
: "";
502498

499+
// When the note is under import/{spaceName}/, only treat wiki links as resolved if the target is in this folder (not some other vault file).
500+
const pathParts = targetFile.path.split("/");
501+
const importFolder =
502+
pathParts[0] === "import" && pathParts.length >= 2
503+
? pathParts.slice(0, 2).join("/")
504+
: null;
505+
503506
/** Path of targetFile relative to the current note, for use in links. Obsidian resolves relative links from the note's directory. */
504507
const getRelativeLinkPath = (assetPath: string): string => {
505508
const noteParts = noteDir ? noteDir.split("/").filter(Boolean) : [];
@@ -644,6 +647,25 @@ const updateMarkdownAssetLinks = ({
644647
}
645648
}
646649

650+
// Only resolve to files under import/{spaceName}/ so we don't point at the wrong vault's files; leave other links unchanged so they resolve from this folder when the target is created
651+
const resolvedFile = app.metadataCache.getFirstLinkpathDest(
652+
linkPath,
653+
targetFile.path,
654+
);
655+
const isInImportFolder =
656+
importFolder &&
657+
resolvedFile &&
658+
(resolvedFile.path === importFolder ||
659+
resolvedFile.path.startsWith(importFolder + "/"));
660+
if (isInImportFolder && resolvedFile) {
661+
const linkText = getRelativeLinkPath(resolvedFile.path);
662+
if (alias) {
663+
return `[[${linkText}|${alias}]]`;
664+
}
665+
return `[[${linkText}]]`;
666+
}
667+
668+
// No resolved file in import folder: keep link as-is. Using ./ prefix breaks Obsidian (getParentPrefix null, "Folder already exists"); where new notes are created is controlled by app settings.
647669
return match;
648670
},
649671
);
@@ -1250,21 +1272,18 @@ export const importSelectedNodes = async ({
12501272
originalNodePath,
12511273
});
12521274

1253-
// Update markdown content with new asset paths if assets were imported
1254-
if (assetImportResult.pathMapping.size > 0) {
1255-
const currentContent = await plugin.app.vault.read(processedFile);
1256-
const updatedContent = updateMarkdownAssetLinks({
1257-
content: currentContent,
1258-
oldPathToNewPath: assetImportResult.pathMapping,
1259-
targetFile: processedFile,
1260-
app: plugin.app,
1261-
originalNodePath,
1262-
});
1263-
1264-
// Only update if content changed
1265-
if (updatedContent !== currentContent) {
1266-
await plugin.app.vault.modify(processedFile, updatedContent);
1267-
}
1275+
// Update markdown content: rewrite asset paths from pathMapping and normalize all wiki links to relative paths
1276+
const currentContent = await plugin.app.vault.read(processedFile);
1277+
const updatedContent = updateMarkdownAssetLinks({
1278+
content: currentContent,
1279+
oldPathToNewPath: assetImportResult.pathMapping,
1280+
targetFile: processedFile,
1281+
app: plugin.app,
1282+
originalNodePath,
1283+
});
1284+
1285+
if (updatedContent !== currentContent) {
1286+
await plugin.app.vault.modify(processedFile, updatedContent);
12681287
}
12691288

12701289
// Log asset import errors if any

0 commit comments

Comments
 (0)