-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix relative MD links when embedding
This fixes the issue of having relative links (like `../services/database`) in the README.md documents we are embedding in docs. Now we first process the relative links and turn them into absolute links to full github.com URLs.
- Loading branch information
Showing
5 changed files
with
59 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,42 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { parse } from "marked"; | ||
|
||
export default function EmbedReadme({ url }) { | ||
export default function EmbedReadme({ repo, branch, readmePath, rawBaseUrl, pageBaseUrl }) { | ||
const [content, setContent] = useState(); | ||
|
||
useEffect(() => { | ||
fetch(url) | ||
fetch(rawBaseUrl + "/" + repo + "/" + branch + readmePath) | ||
.then((response) => response.text()) | ||
.then((text) => setContent(parse(text))); | ||
.then((text) => { | ||
// Replace relative markdown links with absolute ones | ||
// Matches markdown links that don't start with "http" | ||
const processedText = text.replace(/\[([^\]]+)\]\((?!http)([^)]+)\)/g, (match, title, path) => { | ||
// Handle anchor links | ||
if (path.startsWith("#")) { | ||
return `[${title}](${pageBaseUrl}/${repo}/tree/${branch}${readmePath}${path})`; | ||
} | ||
|
||
// Resolve relative paths (including ../) | ||
const currentPath = readmePath.split("/").slice(0, -1); | ||
const pathParts = path.split("/"); | ||
|
||
let resolvedPath = [...currentPath]; | ||
for (const part of pathParts) { | ||
if (part === "..") { | ||
resolvedPath.pop(); | ||
} else { | ||
resolvedPath.push(part); | ||
} | ||
} | ||
|
||
return `[${title}](${pageBaseUrl}/${repo}/tree/${branch}/${resolvedPath.join("/")})`; | ||
}); | ||
setContent(parse(processedText)); | ||
}); | ||
}, []); | ||
|
||
if (!content) return "Loading from " + url + "..."; | ||
console.log(rawBaseUrl); | ||
|
||
if (!content) return "Loading from " + rawBaseUrl + readmePath + "..."; | ||
return <div dangerouslySetInnerHTML={{ __html: content }} />; | ||
} |