Skip to content

Commit c2baa2d

Browse files
authored
Add path resolution function to handle root-relative and regular relative links in repo transformations (#75)
Signed-off-by: Pete Cheslock <[email protected]>
1 parent 678c245 commit c2baa2d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

remote-content/remote-sources/repo-transforms.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ function applyBasicMdxFixes(content) {
4848
.replace(/<summary[^>]*>/gi, '<summary>');
4949
}
5050

51+
/**
52+
* Resolve a path based on whether it's root-relative or regular relative
53+
*/
54+
function resolvePath(path, sourceDir, repoUrl, branch) {
55+
const cleanPath = path.replace(/^\.\//, '');
56+
57+
// Handle root-relative paths (starting with /) - these are relative to repo root
58+
if (cleanPath.startsWith('/')) {
59+
const rootPath = cleanPath.substring(1); // Remove leading slash
60+
return `${repoUrl}/blob/${branch}/${rootPath}`;
61+
}
62+
63+
// Handle regular relative paths - these are relative to the source file's directory
64+
const fullPath = sourceDir ? `${sourceDir}/${cleanPath}` : cleanPath;
65+
return `${repoUrl}/blob/${branch}/${fullPath}`;
66+
}
67+
5168
/**
5269
* Fix all images to point to GitHub raw URLs
5370
*/
@@ -78,17 +95,14 @@ export function transformRepo(content, { repoUrl, branch, sourcePath = '' }) {
7895
return fixImages(applyBasicMdxFixes(content), repoUrl, branch, sourceDir)
7996
// All relative links go to source repository (inline format)
8097
.replace(/\]\((?!http|https|#|mailto:)([^)]+)\)/g, (match, path) => {
81-
const cleanPath = path.replace(/^\]\(/, '').replace(/^\.\//, '');
82-
// Resolve relative path relative to the source file's directory
83-
const fullPath = sourceDir ? `${sourceDir}/${cleanPath}` : cleanPath;
84-
return `](${repoUrl}/blob/${branch}/${fullPath})`;
98+
const cleanPath = path.replace(/^\]\(/, '');
99+
const resolvedUrl = resolvePath(cleanPath, sourceDir, repoUrl, branch);
100+
return `](${resolvedUrl})`;
85101
})
86102
// All relative links go to source repository (reference format)
87103
.replace(/^\[([^\]]+)\]:(?!http|https|#|mailto:)([^\s]+)/gm, (match, label, path) => {
88-
const cleanPath = path.replace(/^\.\//, '');
89-
// Resolve relative path relative to the source file's directory
90-
const fullPath = sourceDir ? `${sourceDir}/${cleanPath}` : cleanPath;
91-
return `[${label}]:${repoUrl}/blob/${branch}/${fullPath}`;
104+
const resolvedUrl = resolvePath(path, sourceDir, repoUrl, branch);
105+
return `[${label}]:${resolvedUrl}`;
92106
});
93107
}
94108

0 commit comments

Comments
 (0)