in website/components/GitHubReadme.js [25:105]
export default function GitHubReadme({
children,
owner = "apache",
repo = "opendal",
branch = "main",
basePath = "",
components = {},
}) {
const createGitHubUrl = (path) => {
const cleanPath = path.replace(/^\.\//, "");
return `https://github.com/${owner}/${repo}/blob/${branch}/${basePath}${cleanPath}`;
};
const CustomLink = (props) => {
const { href, ...restProps } = props;
if (href && href.startsWith("./")) {
return <a {...restProps} href={createGitHubUrl(href)} />;
}
return <a {...props} />;
};
const CustomParagraph = (props) => {
const { children } = props;
if (typeof children === "string") {
const pattern = /\[(.*?)\]:\s*(\.\/[^\s]+)/g;
if (pattern.test(children)) {
const processedText = children.replace(
pattern,
(match, label, path) => {
return `[${label}]: ${createGitHubUrl(path)}`;
},
);
return <p>{processedText}</p>;
}
}
return <p {...props} />;
};
const mergedComponents = {
...components,
a: CustomLink,
p: CustomParagraph,
};
const processRawMarkdown = (content) => {
if (typeof content !== "string") return content;
let processed = content.replace(
/\[(.*?)\]\((\.\/[^)]+)\)/g,
(match, text, path) => `[${text}](${createGitHubUrl(path)})`,
);
processed = processed.replace(
/\[(.*?)\]:\s*(\.\/[^\s]+)/g,
(match, label, path) => `[${label}]: ${createGitHubUrl(path)}`,
);
return processed;
};
if (typeof children === "string") {
return processRawMarkdown(children);
}
if (React.isValidElement(children)) {
return React.cloneElement(children, {
components: {
...(children.props.components || {}),
...mergedComponents,
},
});
}
return children;
}