Skip to content

Commit 799223c

Browse files
ManuelColomboclaude
andcommitted
Fix: auto-fix internal links in markdown content for staging
- Add Eleventy transform to automatically prefix internal links with pathPrefix - Fixes href and src attributes that start with / in HTML output - Links in markdown files now work correctly in both staging (/preview) and production Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3109817 commit 799223c

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

.eleventy.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ module.exports = function(eleventyConfig) {
1111
eleventyConfig.addGlobalData("isStaging", process.env.PATH_PREFIX === "/preview");
1212
eleventyConfig.addGlobalData("pathPrefix", process.env.PATH_PREFIX || "/");
1313

14+
// Transform: fix internal links in markdown content to work with pathPrefix
15+
const pathPrefix = process.env.PATH_PREFIX || "/";
16+
if (pathPrefix !== "/") {
17+
eleventyConfig.addTransform("fixInternalLinks", function(content) {
18+
if (this.page.outputPath && this.page.outputPath.endsWith(".html")) {
19+
// Fix href="/..." links (but not href="http...", href="#...", etc)
20+
content = content.replace(/href="(\/[^"]*?)"/g, (match, path) => {
21+
// Skip external links, anchors, and already prefixed paths
22+
if (path.startsWith('http') || path.startsWith('#') || path.startsWith(pathPrefix)) {
23+
return match;
24+
}
25+
return `href="${pathPrefix}${path}"`;
26+
});
27+
28+
// Fix src="/..." for images and scripts
29+
content = content.replace(/src="(\/[^"]*?)"/g, (match, path) => {
30+
// Skip external resources and already prefixed paths
31+
if (path.startsWith('http') || path.startsWith(pathPrefix)) {
32+
return match;
33+
}
34+
return `src="${pathPrefix}${path}"`;
35+
});
36+
}
37+
return content;
38+
});
39+
}
40+
1441
return {
1542
dir: {
1643
input: ".",

0 commit comments

Comments
 (0)